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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
AlexanderPrime
Solution Supplier
Solution Supplier

TOPN calculations not working when multiple categories selected but fine for single category

Hi all.

 

Have a table with data somewhat like this. Shortened for quick reading. Table just called Table1

 

WeekYearCategorySales Value
12020Type A100
12020Type B200
12020

Type C

300
22020Type A100
22020Type B200
22020

Type C

400
32020Type A85
32020Type B200
32020

Type C

300
12021Type A100
12021Type B200
12021

Type C

300

 

I have a duo of measures to go on to different Card Visuals to show the "best" and "worst" performing sales week with the below code (Will just do Best Week as I know we can swap DESC at the end with ASC to get the worst. 

Sales Value Best Week =
CALCULATE( SELECTEDVALUE(Table1[Week]), TOPN(1,Table1,Table1[Sales Value], DESC) )

There are a combo of filters on the page, one for YEAR and another for CATEGORY, Year is a hard "One item only" so it'll always be either 2020 or 2021. 
Another is for Category, multi select enabled.

The measure works fine if a single Category is selected so for the filter combo of "2020" and "Type C" it'll correctly show the result as 2.

However things get a bit messy when more than one category is selected and it starts giving random results. I can't seem to find a way for it to consider multiple categories, in this, am I missing something obvious again?




Please note - I am not affiliated with Microsoft, I'm just an end user like yourself.
Just a regular guy doin' Data Science.

If my post has helped you, please don't forget to thumbs up or click "Accept as solution" if it solved your problem!
1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@AlexanderPrime , You topN for each category this should work, measure

 

CALCULATE( max(Table1[Week]), TOPN(1,allselected(Table1[Week]),sum(Table1[Sales Value]), DESC) )

 

or

 

CALCULATE( max(Table1[Week]), TOPN(1,allselected(Table1[Week]),sum(Table1[Sales Value]), DESC) , values(Table1[Week]))

or use TOPN from visual level filter

 

or create a Rank like this and filter at the visual level

refer sub-category rank

 

For Rank Refer these links
https://radacad.com/how-to-use-rankx-in-dax-part-2-of-3-calculated-measures
https://radacad.com/how-to-use-rankx-in-dax-part-1-of-3-calculated-columns

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

View solution in original post

2 REPLIES 2
amitchandak
Super User
Super User

@AlexanderPrime , You topN for each category this should work, measure

 

CALCULATE( max(Table1[Week]), TOPN(1,allselected(Table1[Week]),sum(Table1[Sales Value]), DESC) )

 

or

 

CALCULATE( max(Table1[Week]), TOPN(1,allselected(Table1[Week]),sum(Table1[Sales Value]), DESC) , values(Table1[Week]))

or use TOPN from visual level filter

 

or create a Rank like this and filter at the visual level

refer sub-category rank

 

For Rank Refer these links
https://radacad.com/how-to-use-rankx-in-dax-part-2-of-3-calculated-measures
https://radacad.com/how-to-use-rankx-in-dax-part-1-of-3-calculated-columns

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Thank you!

Seems I was having a case of the Wednesdays and didn't think to use the TOPN visual filter, for some reason I was convinced it was only at Measure level, the TOPN Visual filter method worked like a charm. 


Please note - I am not affiliated with Microsoft, I'm just an end user like yourself.
Just a regular guy doin' Data Science.

If my post has helped you, please don't forget to thumbs up or click "Accept as solution" if it solved your problem!

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors
Top Kudoed Authors