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.

Reply
ScotchCat
Regular Visitor

Group by a Category and then Rank

Hi - I need assistance with the following - I'm rather new to Power BI so please be gentle! 

I have the following table of data that I would like to Group by the Category column and then create a rank based on the number of occurrences (Accounts) within each Category.   To go further - I will need to create a Pie Chart that shows the Top 3 Categories with the most Accounts.   All help is appreciated! 

Data table is named - CoolSports 

ACCOUNTCATEGORYNEED RANKING
123WaterSki1
357WaterSki1
741WaterSki1
713WaterSki1
157WaterSki1
627WaterSki1
987SnowSki2
159SnowSki2
963SnowSki2
379SnowSki2
564Biking3
753Biking3
618Biking3
852Hiking4
279Hiking4
159Walk5
1 ACCEPTED SOLUTION

This is certainly a factor to keep in mind but can often result in a less efficient query. In this simple case, the query engine seems to optimize well enough that the following is only marginally faster (tested on a 500k row table), although in general, this sort of approach can make a huge difference.

Rank =
RANKX (
    VALUES ( CoolSports[CATEGORY] ),
    CALCULATE (
        COUNTROWS ( CoolSports ),
        ALLEXCEPT ( CoolSports, CoolSports[CATEGORY] )
    ),
    ,
    DESC
)

 

View solution in original post

2 REPLIES 2
CNENFRNL
Community Champion
Community Champion

I always avoid using CALCULATE/CALCULATETABLE in a calculated column in order to avoid complexity of context transition.

RANK = 
RANKX(
    CoolSports,
    COUNTROWS(
        FILTER(
            CoolSports,
            CoolSports[CATEGORY] = EARLIER( CoolSports[CATEGORY] )
        )
    ), , ,
    DENSE
)

Screenshot 2021-06-30 220900.png


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!

This is certainly a factor to keep in mind but can often result in a less efficient query. In this simple case, the query engine seems to optimize well enough that the following is only marginally faster (tested on a 500k row table), although in general, this sort of approach can make a huge difference.

Rank =
RANKX (
    VALUES ( CoolSports[CATEGORY] ),
    CALCULATE (
        COUNTROWS ( CoolSports ),
        ALLEXCEPT ( CoolSports, CoolSports[CATEGORY] )
    ),
    ,
    DESC
)

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors