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

Top 3 per category in a pivot table

Hello!

 

I am trying to group the top 3 stores for a metric by their district in the same pivot table. Example below...

 

Screenshot 2022-08-27 235841.png

 

I would like to show the top 3 in each district. Any help is appreciated!!!

 

1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

TOP_N = 
VAR __topn =
    TOPN(
        MAX( N[Value] ),
        CALCULATETABLE(
            SUMMARIZE( Data, Data[District], Data[Store] ),
            ALLSELECTED( Data[Store] )
        ),
        [Total]
    )
RETURN
    CALCULATE( [Total], KEEPFILTERS( __topn ) )

Record_2022_08_28_10_27_43_341.gif


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

3 REPLIES 3
CNENFRNL
Community Champion
Community Champion

TOP_N = 
VAR __topn =
    TOPN(
        MAX( N[Value] ),
        CALCULATETABLE(
            SUMMARIZE( Data, Data[District], Data[Store] ),
            ALLSELECTED( Data[Store] )
        ),
        [Total]
    )
RETURN
    CALCULATE( [Total], KEEPFILTERS( __topn ) )

Record_2022_08_28_10_27_43_341.gif


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Jihwan_Kim
Super User
Super User

Hi,

In case you want to create a table, please check the below picture and the attached pbix file.

I tried to create a sample pbix file like below.

 

Picture1.png

 

Top three per district table =
VAR _topthreestorelist =
    SUMMARIZE (
        GENERATE (
            VALUES ( Data[District] ),
            SUMMARIZE (
                TOPN (
                    3,
                    FILTER ( Data, Data[District] = EARLIER ( Data[District] ) ),
                    Data[Sales], DESC
                ),
                Data[Store]
            )
        ),
        Data[Store]
    )
RETURN
    CALCULATETABLE ( Data, Data[Store] IN _topthreestorelist )

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.
daXtreme
Solution Sage
Solution Sage

Create a helper measure that will return 1 for any top 3 stores in any of the districts and then filter the visual by this value on this very measure. This will then hide the other stores and you'll only see what you want.

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.