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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
eliasayyy
Memorable Member
Memorable Member

dynamic top n

hello i have 2 tables topn

annonymous1999_0-1687510142215.png


and sales which has 2 columns product and sales

i want to make a slicer that has topn values in it and i want to make a bar chart for product and sale

i want when i choose top 10 , to see top 10 products when i select all , i want to see all products


PS. Exclude products whos sum is 0 or blank

 

1 ACCEPTED SOLUTION
ValtteriN
Super User
Super User

Hi,

Here is a simplified example on how to do this. Data:

ValtteriN_0-1687512597488.pngValtteriN_1-1687512638444.png

Measure:

Dynamic top N =

var _sel =MAX('Table (37)'[id]) //selected top N
var _rank = RANKX(ALLSELECTED('Table (36)'),CALCULATE(SUM('Table (36)'[Sales]))) //calculate sales rank CALCULATE is used to force row context
return


SWITCH(_sel,
1,IF(_rank<=5,1,0), //top 5
2,IF(_rank<=10,1,0), //top 10
3,IF(_rank<=15,1,0), //top 15
4, 1) //all

Place this measure as a filter:
ValtteriN_2-1687513465739.png

 

Now the filter will work:
ValtteriN_3-1687513482668.png
To remove blanks and 0 just use this as a filter:

ValtteriN_4-1687513619053.png

 

I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!

My LinkedIn: https://www.linkedin.com/in/n%C3%A4ttiahov-00001/




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

1 REPLY 1
ValtteriN
Super User
Super User

Hi,

Here is a simplified example on how to do this. Data:

ValtteriN_0-1687512597488.pngValtteriN_1-1687512638444.png

Measure:

Dynamic top N =

var _sel =MAX('Table (37)'[id]) //selected top N
var _rank = RANKX(ALLSELECTED('Table (36)'),CALCULATE(SUM('Table (36)'[Sales]))) //calculate sales rank CALCULATE is used to force row context
return


SWITCH(_sel,
1,IF(_rank<=5,1,0), //top 5
2,IF(_rank<=10,1,0), //top 10
3,IF(_rank<=15,1,0), //top 15
4, 1) //all

Place this measure as a filter:
ValtteriN_2-1687513465739.png

 

Now the filter will work:
ValtteriN_3-1687513482668.png
To remove blanks and 0 just use this as a filter:

ValtteriN_4-1687513619053.png

 

I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!

My LinkedIn: https://www.linkedin.com/in/n%C3%A4ttiahov-00001/




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.