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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Ahsan_
Regular Visitor

Rankx - Top 5 values

Hello there!
I have used the below dax expression for Top 5 Managers by Commision, which worked,

Top 5 managers = (
    VAR ranking = RANKX(ALL(Store[Manager Name]),[Sales Commission],,DESC)
RETURN
    IF(ranking <=5,[Sales Commission]))

Screenshot 2024-03-31 021101.png







Above is the result, which is fine, but there are two problems:

  1. Total: It shows the total for the whole column, not for the top 5 values. How do I fix this?
  2. I want to add another column for the store name, which shows the store name of each manager. However, when I add the column, it starts showing the data for all the managers instead of the top 5 managers. How can I add the store name column without ruining the top 5 rank?


Screenshot 2024-03-31 022151.png

 

1 ACCEPTED SOLUTION
kriscoupe
Solution Supplier
Solution Supplier

Hi @Ahsan_ ,

 

1. The reason you are getting the full total in the total row is because there is no concept of a manager filtering this value so there is nothing to rank. To remedy this you can create a new measure that iterates over the all the values of manager and adds them up.

 

Top 5 Managers Enhanced =
SUMX(
    VALUES( Store[Manager Name] ),
    [Top 5 managers]
)

 

2. When you add Store onto the axis that adds store to the filter context of the rank. All managers will be the top ranked for their store (assuming one manager per store) so they all show. I don't know the intricacies of the model but you could extend the above to state

 

 

Top 5 Managers Enhanced =
CALCULATE(
    SUMX(
        VALUES( Store[Manager Name] ),
        [Top 5 managers]
    ),
    ALL( Store[Store Name] )
)

 

I've assumed there that Store Name is in the same Store table as manager.

 

 

If you want it all in the same measure you could copy paste your original code in but make sure to wrap it in a CALCULATE so that context transition takes place

 

Top 5 Managers Enhanced =
CALCULATE(
    SUMX(
        VALUES( Store[Manager Name] ),
        CALCULATE(
            VAR ranking = RANKX(ALL(Store[Manager Name]),[Sales Commission],,DESC)
            RETURN
            IF(ranking <=5,[Sales Commission])
        )
    ),
    ALL( Store[Store Name] )
)

 

Hope it helps,

Kris

 

 

 

View solution in original post

2 REPLIES 2
Ahsan_
Regular Visitor

It worked like magic! Thanks

kriscoupe
Solution Supplier
Solution Supplier

Hi @Ahsan_ ,

 

1. The reason you are getting the full total in the total row is because there is no concept of a manager filtering this value so there is nothing to rank. To remedy this you can create a new measure that iterates over the all the values of manager and adds them up.

 

Top 5 Managers Enhanced =
SUMX(
    VALUES( Store[Manager Name] ),
    [Top 5 managers]
)

 

2. When you add Store onto the axis that adds store to the filter context of the rank. All managers will be the top ranked for their store (assuming one manager per store) so they all show. I don't know the intricacies of the model but you could extend the above to state

 

 

Top 5 Managers Enhanced =
CALCULATE(
    SUMX(
        VALUES( Store[Manager Name] ),
        [Top 5 managers]
    ),
    ALL( Store[Store Name] )
)

 

I've assumed there that Store Name is in the same Store table as manager.

 

 

If you want it all in the same measure you could copy paste your original code in but make sure to wrap it in a CALCULATE so that context transition takes place

 

Top 5 Managers Enhanced =
CALCULATE(
    SUMX(
        VALUES( Store[Manager Name] ),
        CALCULATE(
            VAR ranking = RANKX(ALL(Store[Manager Name]),[Sales Commission],,DESC)
            RETURN
            IF(ranking <=5,[Sales Commission])
        )
    ),
    ALL( Store[Store Name] )
)

 

Hope it helps,

Kris

 

 

 

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

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

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.