Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

GilbertQ

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?

 

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

Hello @GilbertQ ,

i'm just new in BI and learned it from watching videos. I am currently doing visualizations to show dynamic graphs and needs help to link. first matrix shows the branches of the pharmacy showing sales and growth. 2nd, when u click a particular branch it will show u the sales per product range. Lastly, when product range is click, 3rd matrix will show the top 10 sku of the selected range for a particular branch.

 

Currently what happening is when i click a particular branch in matrix 1, it will show the sales per product range in my matrix 2. But when i tried to click a particular range it table/matrix 2, yes it showing the top 10 sku in my matrix 3 but not anymore connected to matrix 1. Will u be able to help me on this.

 

appreciate much. Thank u

Hi @Nei 

 

Have you tried to do the CRTL + Click when moving across the matrix, that should keep the existing selections?

Hi @GilbertQ ,

 

Awesome.: It's working fine now.

but may i ask your expertise  on the following as well

 1. how to arrange chronological the months in my slicer? i tried to go to formatting but its diabled.( i have 1 appended table for the pharmacy and separte table for dates. )

2. Why the data is not showing in my matrix table and my file source are complete.

3. for the meaures Ranking to show top 10 pharmacy branches, why ranking apeears multiple 1 where their values are different ( i used Rankx( ALL

 

Thanky you and appreciate it so much.

Hi @Nei 

 

Glad that it is working

 

1. You can use the Sort By column in order to sort your months in the right order.

2. It is possible that something is either being filtered out in the Power Query Editor, or a filter on your Power BI Report?

3. It will be because the ranking is defined for a particular column on a table. If you need it for another column you will need to create another measure.

hi @GilbertQ 

1. i tried the sort  by column in order but its not working. 😞

2. i found the mistake. thank u 🙂

3. i ranked it using the column units, where my pharmacy needs to be ranked according to total units sold and i dont know why it's giving the same ranking, total sales are different from each other.

 

 

in addition, i was doing a chart to show my top 10 using the visual fileter advance fileter, top 10. it's working but how to i want to show the sales of the full customers and not only 10.

 

Thank you.

@GilbertQ 

 

Hello Gilber

 

May i know how why the CTRL + Click when moving across the matrix is not working in the reports shared to colleagues. How can i do that please.

Hi @Nei 

 

This is by design when using the Matrix visual.

Anonymous

How can I use this to keep a Top 5 Pie chart constant as I move the slicer around

In order to keep top 5 when you adjust a slicer you would want to change the interactions on the page.  Click on the Slicer, Then The Format Ribbon should appear.  On the format ribbon click edit interactions.  Then change the interaction on the pie chart to None. This will disable the ability for the slicer to change the filter context of the slicer. 

 

If this helped your answer, Please Kudo the response.

Mike Carlo

PowerBI.Tips

Anonymous

Thanks Mike,

That will make the chart static and I want to make it dynamic. Top have different top 5 based on where the date slicer is 

Ah sorry, I miss-understood.  I think then what you are looking for is an Advanced filter property on the PipeChart.  You can add items to the PieChart and then specifiy the TopN property in the filter.  Then you will have to add a measure to the TopN filter so it knows what are the Top Five Items... 

 

 

Hi Gillbert,

This is exactly what we where looking for!

 

Along with TopN Cities user also would like to see rest of the Cities which not in TopN tagged as Others and shows the aggregated Sales value of rest of the cities and show against Others. Can you please help us on this whenever you get the chance.

 

Rest all,

If you have already done with this can you please share the solution with me.

Hi @pbiforum123 

 

Thanks for letting me know.

 

The only way this currently can be done is by looking at this blog post from the awesome people at SQLBI.COM

 

https://www.sqlbi.com/articles/showing-the-top-5-products-and-others-row/

Yes I have already got that link earlier but it was not working in mine, not sure what I was missing. I have got little with other work so couldn't debug it yet. I will start debugging it once I am free again.

 

Thanks a lot for your help, will keep you posted on this.

Hi,

 

Thank you for your exemple and this trick !

 

Maybe I am the first but I don't understand this part in the function : 

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

 

Why do we compare COUNTROWS ( VALUES ( 'Orders'[City] ) ) = 1; ?

 

Thank you,

Regards