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
Anonymous
Not applicable

Dynamic TopN with RankX

Hello everyone,

 

I have a base with a lot of products, and i want to show the sales of each product along the time in a line chart.  To make the chart more clean, i want to make a dynamic top filter. I used this tutorial : Dynamic TopN made easy

 

This is the current formula:

 
TopN_Sales = 
VAR SelectedTop = SELECTEDVALUE('TopN'[TopN]) // 'TopN'[TopN] is the table with the slicer
RETURN
IF(RANKX(ALL(base[product]);[Tot_Sales])<=SelectedTop;[Tot_Sales];BLANK()) // Tot_Sales just sum all sales
 
The way it is working, it filters the TopN of each month.  If i filter Top 4, the chart will show 5 products and ll have some break between the lines.
 

Image 1.jpg

 

If the topN are the same across the whole period, the dynamic filter works fine.

 

 

Image 2.jpg

 

 

I want to make a dynamic topN filter that works in the same way as the TopN filter in visualization. Is there a way to rank by the sum of total sales?

 

 Here is a pbix : Example.pbix

 

Thank you!

1 ACCEPTED SOLUTION
Anonymous
Not applicable

It is unclear on what you mean with "make it work different".

 

Are you trying to show the 4 items with the highest total sales over a year? or just during the month?

 

EDIT: 

If took your example file and created the following measure:

SubTotal = CALCULATE(SUM(Base[Valor Total]);ALLEXCEPT(Base;Base[marca]))

I then changed your TopN Measure to:

TopN_total_sales = 
VAR SelectedTop = SELECTEDVALUE('TopN'[TopN])
var RankMarca = RANKX(ALL(Base[marca]);[SubTotal])

RETURN
IF(RANKX(ALL(Base[marca]);[SubTotal])<=SelectedTop;[total_sales];BLANK())

 

I'll get this as a result:

ResultResult

I hope that this solves your problem!

 

Kind Regards.

 

PS: If it does solve your problem please mark this as answer.

View solution in original post

8 REPLIES 8
Anonymous
Not applicable

Hi @Anonymous,

 

The Visual you've created does exactly what it needs to be done.

It shows the top from that month that's why you'll get blank rows in between because Blue is ranked 4th on may june july and august and orange is ranked 4th on the other months.

Anonymous
Not applicable

Yes.. I know how it is working, but the question is how to make it work different.

Anonymous
Not applicable

It is unclear on what you mean with "make it work different".

 

Are you trying to show the 4 items with the highest total sales over a year? or just during the month?

 

EDIT: 

If took your example file and created the following measure:

SubTotal = CALCULATE(SUM(Base[Valor Total]);ALLEXCEPT(Base;Base[marca]))

I then changed your TopN Measure to:

TopN_total_sales = 
VAR SelectedTop = SELECTEDVALUE('TopN'[TopN])
var RankMarca = RANKX(ALL(Base[marca]);[SubTotal])

RETURN
IF(RANKX(ALL(Base[marca]);[SubTotal])<=SelectedTop;[total_sales];BLANK())

 

I'll get this as a result:

ResultResult

I hope that this solves your problem!

 

Kind Regards.

 

PS: If it does solve your problem please mark this as answer.

Anonymous
Not applicable

Thank you , it worked perfectly!

 

In the TopN Measure, the var RankMarca is not used, so i deleted it. And [GrandTotal] should be [SubTotal], right?

 

Thanks

 

v-yulgu-msft
Microsoft Employee
Microsoft Employee

Hi @Anonymous,

 

Why is the highlighted value different?

1.PNG

 

How is the data in source table like? What is the formula of [Tol_Sales]? You said "Tol Sales" sums all sales, but why did you compare it with TopN selection?

 

Best regards,

Yuliana Gu

Community Support Team _ Yuliana Gu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Sry, it was a typo. I added the original post some prints and a pbix file.

themistoklis
Community Champion
Community Champion

@Anonymous

 

Have you tried adding the month, the product and measure in a matrix as is and then on the filter pane select the top2.

Measure = tablename[Total Sales]

See image below:

image.png

Anonymous
Not applicable

Yes, i want to make it work just like the TopN filter, but i want to make it dynamic.

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.