Showing results for 
Search instead for 
Did you mean: 

Dynamic TopN made easy with What If Parameter

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?



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])
    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.  



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.

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.

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?

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. 


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?


Oded Dror



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!

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.

Hi @Anonymous


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.

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.




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



Oded Dror

Hi @Anonymous


It would depend on the value of the measures




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



Oded Dror

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


Nice job 


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

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?

Hi GilbertQ, Thank you for the reply, Please see the image in below link: 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.