Dynamic TopN made easy with What If Parameter

by Super User ‎02-28-2018 04:37 PM - edited ‎02-28-2018 04:39 PM

In this post I am going to demonstrate how to create a Dynamic TopN slicer using a What-If Parameter. This will allow your user to simply use the Slicer/Slider to view the TopN values and as a bonus if the user slides it to zero, it will display everything! Who doesn't like something that is easy to create, but makes it so much easier for the user to gain insights into their own data easily and quickly?

 

Example

For this example, I am going to be using a [Sales Amount] measure from my Orders table. Next for the TopN, I am going to be looking for the TopN by City. This is a key component when identifying what you want your TopN to be based on. NOTE: If the explanation of the TopN Measure can be a bit complex, you can either copy the code and modify it for your requirements or you can view the animated GIF further below so you can see it working. 

 

Creating the TopN Slicer

  • In Power BI Desktop, I went to the Modeling tab and then clicked on New Parameter
  • I then gave it the following properties as shown below.
  • One thing to NOTE is that I set the Minimum to 0 (zero)
  • When you are creating this TopN What-if parameter, you can change anything for the Maximum, Increment and Default.
  • I could then see the table created on the right-hand side.
  • As well as the Slicer on my reporting canvas, which I formatted

Creating the TopN Measure

  • Below is the TopN measure, which I will go into detail how it works below.
TopN City = 
VAR SelectedTop = SELECTEDVALUE('TopN'[TopN])
RETURN
SWITCH(TRUE(),
    SelectedTop = 0, [Sales Amount],
    RANKX ( 
            ALLSELECTED(  'Orders'[City] ), 
            [Sales Amount]
                )
                  <= SelectedTop,
        [Sales Amount]
)

 

  • Line 2, is where I created the Only Variable called SelectedTop
    • This is getting the selected value from the Slicer.
    • If it is slid to 5, this variable SelectedTop will store 5.
  • Line 4 is where I used the SWITCH(TRUE()
    • What this does, is it enables me to pass multiple statements to evaluate in one DAX function.
    • NOTE: You could possibly do this with an IF statement, but I prefer doing it this way, because quite often the requirement changes to have more than 2 conditions, so doing in this way it is easy for me to add another condition.
  • Line 5 is my first condition, where I have said if the TopN Slicer (SelectedTop)= 0 (zero) then display all the [Sales Amount]
    • It will do this because there is no filter context being applied on the [Sales Amount]
  • Lines 6 – 9 is where the Magic happens and uses the values from the TopN slicer.
    • This is also the second condition for the SWITCH(TRUE() DAX expression.
    • Even though this is for TopN values, I use the RANKX to achieve the desired result from Line 6
    • Next, I am using the ALLSELECTED, because in my table I want to select the TopN for the City values.
    • Line 7 is where I am selecting from my table Orders and the column called City ALLSELECTED( 'Orders'[City] ),
    • Line 8, is where I am specifying my measure for the RANKX which is the [Sales Amount]
    • Line 9, is where I am closing off the RANKX function.
  • Line 10 is where I am now comparing it to be less than equal to the selected slicer value SelectedTop
    • If this evaluates to TRUE, then display the RANKX up to and including the selected slicer value.
  • Line 11 is the ELSE condition for the SWITCH(TRUE()
  • Line 12 is closing off the SWITCH DAX Function

TopN Slicer in Action

Please watch the following animation below in which I will demonstrate by using the Slider I can to from Top 10, to Top 5. And then by sliding it to 0 (zero) it will show all the cities.  

                             

Conclusion

As I have shown, by using the What-If Parameter and some DAX there is now a way to easily create a dynamic TopN that is easy for your users to use within their reports. As always, if there are any questions or comments, please leave them in the section below.

Comments
by anktaggrwl Regular Visitor
on ‎03-01-2018 07:56 PM

This is great! and amazingly arrived just in time for me to impement in something I happened to be working through this week to create a dynamic slicer like this.

 

What's the best way to just show the sum of the TopN cities in your example? I'd love to show the TopN cities, their total, and how much of the total not filtered value the topN is.

by Super User
on ‎03-01-2018 08:55 PM

hi @anktaggrwl

 

To show the total with the TopN, you could just take the measure and put that in a card or any visual.

 

With regards to the values not filtered you could possibly do it by getting the entire total and then subtracting it from the TopN total, which would then give you the difference?

by anktaggrwl Regular Visitor
on ‎03-01-2018 11:17 PM

Maybe I'm missing something, but even in your gif above, when you change the TopN slider, while the list of cities adjusts to show the top 5, 10, 20 cities and their amounts individually, the sum in your table still calculates and produces a total based on the full list of cities regardless of the parameter value. 

 

The same therefore is happening in a card visual. 

by odeddror Frequent Visitor
on ‎03-02-2018 12:44 PM

Hi Guavaq,

Is there any way you can email me the sample I've try to do that but it dosent seems to recognized the column I want to filter on - where do you please the measure on the TopN table or on the Order table?


Thanks

Oded Dror

 

 

by shockian Visitor
on ‎03-02-2018 04:03 PM

Hi!  I have a very large matrix that has been built, and the dynamic TopN slicer will only filter based on the value (in my scenario it is this year revenue and SKU), but will not work with the other values in the matrix.  Is there a way to do the TopN based on SKU and revenue, for example, but filter across the whole table?  Thanks!

by Super User
on ‎03-02-2018 04:20 PM

Hi @anktaggrwl

 

You are indeed correct, what I need to do is update this blog post with the measure below which will then display the right values in the total

 

TopN City Matrix = 
IF (
    COUNTROWS ( VALUES ( 'Orders'[City] ) ) = 1,
    [TopN City]
    ,
    SUMX (
        VALUES ( 'Orders'[City] ),
        [TopN City]
    )
)

If you put that into the matrix it will then work as expected.

by Super User
on ‎03-02-2018 04:22 PM

Hi @odeddror

 

Here is a link to where you can download the sample data.

 

Dynamic TopN made easy with What If Parameter.pbix

 

If you still get stuck please let me know.

by Super User
on ‎03-02-2018 04:23 PM

Hi @shockian

 

Would you be able to provide some sample data, as this is often a lot easier for it to work out how to get it working for you.

by odeddror Frequent Visitor
on ‎03-02-2018 04:34 PM

guavaq,

 

Thank you, one more question the total per TopN showing the Grand Total and not what the Top N is

For example if I have 100 rows with total sales of 1,000 and I pick top 5 it should show what the top 5 are

But it showed 1,000

 

Thanks,

Oded Dror

by Super User
on ‎03-02-2018 04:50 PM

Hi @odeddror

 

It would depend on the value of the measures

by odeddror Frequent Visitor
on ‎03-02-2018 04:57 PM

Guavaq,

 

Thank you for the quick responed. I made it work based on your sample.

 

Thanks

Oded Dror

by Mike_Carlo
on ‎03-14-2018 07:41 PM

Here is a full blown tutorial on a similar topic with slicers, table filtering and dynamic column selection:

 

https://powerbi.tips/2018/02/crazy-table-gymnastics-part-1-dynamic-column-categories/

https://powerbi.tips/2018/02/crazy-table-gymnastics-part-2-build-support-materials/

 

Nice job 

Guavaq

by ONCKdata Frequent Visitor
on ‎06-01-2018 01:18 AM
Hi GilbertQ, Thank you very much for the brilliant post. It working flawlessly in a matrix visual, but I also have graph which shows the customer revenue over month, when I use this method, it is showing topN customers in each month, is there is way I can change it to show the top N customers with respect to grand total? Thanks
by Super User
on ‎06-03-2018 06:13 PM

Hi @ONCKdata

 

When you say you want to change it to show the TopN Customers for the grand total, could you possibly elaborate on that?

 

I am trying to understand, because the way I see it, the grand total is always the total of all the values selected?

by ONCKdata Frequent Visitor
on ‎06-04-2018 02:37 AM
Hi GilbertQ, Thank you for the reply, Please see the image in below link: https://www.dropbox.com/s/j4qbqhai80dy30d/TopN.PNG?dl=0 When I select top 3, I am getting the result for top 3 in a month. Kindly let me know if my understanding is wrong. Thank You.
by ONCKdata Frequent Visitor
on ‎06-04-2018 02:56 AM
Hi Gilbert, I have added an graphical representation of of desired and actual output, please find the below link. https://www.dropbox.com/s/r7thwmzbcbd6lko/Dynamic%20TopN%20made%20easy%20with%20What%20If%20Paramete... Looking forward to your help. Thanks.
by yash14vashistha Frequent Visitor
on ‎08-20-2018 05:24 AM

Hi GilbertQ,

In addendum to the below query, my matrix doesn't filter to show only those cities (in my case clients). It does show the calculated top 10 amount in the grand total, but also shows other clients showing their values as blanks.

 

Hi @@anktaggrw

 

You are indeed correct, what I need to do is update this blog post with the measure below which will then display the right values in the total

 

TopN City Matrix = 
IF (
    COUNTROWS ( VALUES ( 'Orders'[City] ) ) = 1,
    [TopN City]
    ,
    SUMX (
        VALUES ( 'Orders'[City] ),
        [TopN City]
    )
)

If you put that into the matrix it will then work as expected.

by Super User
on ‎08-20-2018 02:22 PM

Hi @yash14vashistha

 

If you could please provide more details.

 

From my example above it is showing only the TopN for my Sales Amount Measure.

 

I would suggest ensuring that your measure that you are using is correct?

by yash14vashistha Frequent Visitor
on ‎08-20-2018 10:44 PM

GilbertQ

 

I fixed that, turned out I had other measures and I had to build a similar calculation for them. Now I am in another fix.

 

I have another matrix where I am applying a visual level filter. This above calculation is not, I think, recognising that filter and giving me blanks because it is rankin the total fees irrespective of any visual level filter. My guess is that ALLSELECTED has to something with it. Can you tell me what I could do in this case? Maybe use a different function than ALLSELECTED?

by Super User
on ‎08-20-2018 11:53 PM

Hi there you could try the ALLEXCEPT which will change the filter context to select everything except what you select 

by yash14vashistha Frequent Visitor
on ‎08-21-2018 12:40 AM

Okay. Fixed that.

 

Now, there is this. This function is calculating the sum for even the Percentages and rates. I want the overall average, but when I use AVERAGEX, the total is not correct.

 

TopN City Matrix = 
IF (
    COUNTROWS ( VALUES ( 'Orders'[City] ) ) = 1,
    [TopN City]
    ,
    SUMX (
        VALUES ( 'Orders'[City] ),
        [TopN City]
    )
)

 

 

 

by Super User
on ‎08-21-2018 12:53 AM

Hi @yash14vashistha I would suggest you post this to the Power BI Community with some sample data where myself or someone else could provide you the solution.

by yash14vashistha Frequent Visitor
on ‎08-21-2018 02:10 AM

Well, I kind of fixed that. by not using the SUMX. So now I am using something like

TopN City Matrix = 
IF (
    COUNTROWS ( VALUES ( 'Orders'[City] ) ) = 1,
    [TopN City]
    ,
        [TopN City]
    )

 this. So I am not concerend about this right now.

 

I have something else!! I'm sorry for coming back to you again and again. The new problem is that on the matrix with the visual level filter, the calucation takes about a minute to filter and show the results, which is pretty slow!

by yash14vashistha Frequent Visitor
on ‎08-21-2018 02:19 AM

I just measured it and it took about 2 min 25 sec to filter top15 results

by Aishwar04 Regular Visitor
‎09-01-2018 08:27 AM - edited ‎09-01-2018 08:51 AM

Hi 

@GilbertQ @ONCKdata

Suppose I rank the customers by Total Sales and it works fine. But when I use Month as legend in a bar chart or a column in a matrix instead of showing the individual sales breakdown its somehow showing wrong values. 

by Nancyjain Frequent Visitor
on ‎09-13-2018 09:19 PM

Hi Gilbert,

This is amazing . I have a requirement of showing a slicers which have Top N and Button N  . When I click on Top N all my charts should show Top N values and when I click on  Bottom N values all charts should change to Bottom N values.

 

Any help will highly appreciated.

Thank You