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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
yassine_mendy59
Frequent Visitor

RANKING in a Matrix with hierarchies with data from 2 tables

Objective: Create a rank calculation in a matrix based on the number of customers.

So, with a hierarchy:

Region

    Store

         Department

We have table A, which contains the store ID, department, and number of customers.

We have table B, which contains the store ID, store label, and associated region label for each store ID.

 

The desired matrix would be:

Region (table B)

     Store (table B)

             Department (Table A)

                       SUM Number of customers (Measure)

                                   Associated rank (Measure)

 

The rank should therefore be calculated for each of the hierarchies based on the number of customers.

At the service level, I have no problem with the calculation, but at the other levels (store and region), I do have some issues.

At the store level, I have a calculation, but strangely, I will have the same rank for stores with 645, 636, and 623 customers, respectively. This is illogical.

At the region level, I only have 1s.


Actual measure :

Adhesions_Rank =
VAR rank_region =
RANKX(
ALL(B[region_label]),
CALCULATE([NB_CUSTOMERS]),
,
DESC,
DENSE
)

VAR rank_magasin =
RANKX(
ALL(B[entity_label]),
CALCULATE([[NB_CUSTOMERS]]),
,
DESC,
DENSE
)

VAR rank_service =
RANKX(
ALL(A[service_label]),
CALCULATE([NB_CUSTOMERS]),
,
DESC,
DENSE
)

RETURN
SWITCH(
TRUE(),
ISINSCOPE(A[service_label]), rank_service,
ISINSCOPE(B[entity_label]), rank_magasin,
ISINSCOPE(B[region_label]), rank_region
)

 

1 ACCEPTED SOLUTION
techies
Super User
Super User

Hi @yassine_mendy59 please try this

 

Rank =
VAR rank_region =
    RANKX (
        ALL ( 'Table B'[region_label] ),
        [NB_CUSTOMERS],
        ,
        DESC,
        DENSE
    )
VAR rank_store =
    RANKX (
        ALLSELECTED ( 'Table B'[store_label] ),
        [NB_CUSTOMERS],
        ,
        DESC,
        DENSE
    )
VAR rank_service =
    RANKX (
        ALLSELECTED ( 'Table A'[department] ),
        [NB_CUSTOMERS],
        ,
        DESC,
        DENSE
    )
RETURN
SWITCH (
    TRUE (),
    ISINSCOPE ( 'Table A'[department] ), rank_service,
    ISINSCOPE ( 'Table B'[store_label] ), rank_store,
    ISINSCOPE ( 'Table B'[region_label] ), rank_region
)
 
 
 
 
― Power BI | Microsoft Fabric | PL-300 | DP-600 | Blog: medium.com/@cseprs_54978

View solution in original post

11 REPLIES 11
v-mdharahman
Community Support
Community Support

Hi @yassine_mendy59,

Thanks for reaching out to the Microsoft fabric community forum. It looks like your ranking expression is not evaluating a store/region-specific aggregation when RANKX iterates, so several store rows end up returning the same value to RANKX. Kindly go through the responses given by @techies and @bhanu_gautam and check if your issue can be resolved.

 

I would also take a moment to thank @techies, @Gabry, @Shahid12523 and @bhanu_gautam, for actively participating in the community forum and for the solutions you’ve been sharing in the community forum. Your contributions make a real difference

 

If I misunderstand your needs or you still have problems on it, please feel free to let us know.  

Best Regards,
Hammad.

Hi @yassine_mendy59,

As we haven’t heard back from you, so just following up to our previous message. I'd like to confirm if you've successfully resolved this issue or if you need further help.

If yes, you are welcome to share your workaround so that other users can benefit as well.  And if you're still looking for guidance, feel free to give us an update, we’re here for you.

 

Best Regards,

Hammad.

Hi @yassine_mendy59,
Hope everything’s going smoothly on your end. As we haven’t heard back from you, so I wanted to check if the issue got sorted.
Still stuck? No worries just drop us a message and we can jump back in on the issue.

 

Best Regards,

Hammad.

techies
Super User
Super User

Hi @yassine_mendy59 please try this

 

Rank =
VAR rank_region =
    RANKX (
        ALL ( 'Table B'[region_label] ),
        [NB_CUSTOMERS],
        ,
        DESC,
        DENSE
    )
VAR rank_store =
    RANKX (
        ALLSELECTED ( 'Table B'[store_label] ),
        [NB_CUSTOMERS],
        ,
        DESC,
        DENSE
    )
VAR rank_service =
    RANKX (
        ALLSELECTED ( 'Table A'[department] ),
        [NB_CUSTOMERS],
        ,
        DESC,
        DENSE
    )
RETURN
SWITCH (
    TRUE (),
    ISINSCOPE ( 'Table A'[department] ), rank_service,
    ISINSCOPE ( 'Table B'[store_label] ), rank_store,
    ISINSCOPE ( 'Table B'[region_label] ), rank_region
)
 
 
 
 
― Power BI | Microsoft Fabric | PL-300 | DP-600 | Blog: medium.com/@cseprs_54978
Gabry
Super User
Super User

Hey,

could you share the pbix with sample data?

My pbi is linked to GCP tables so i can't give you some data.

 

Shahid12523
Community Champion
Community Champion

Use ALLSELECTED instead of ALL so the rank respects the parent level:

 

Adhesions_Rank =
SWITCH(
TRUE(),

// Rank inside Department (service) for its store
ISINSCOPE(A[service_label]),
RANKX(
ALLSELECTED(A[service_label]),
[NB_CUSTOMERS],
,
DESC,
DENSE
),

// Rank inside Store for its Region
ISINSCOPE(B[entity_label]),
RANKX(
ALLSELECTED(B[entity_label]),
[NB_CUSTOMERS],
,
DESC,
DENSE
),

// Rank inside Regions (overall)
ISINSCOPE(B[region_label]),
RANKX(
ALLSELECTED(B[region_label]),
[NB_CUSTOMERS],
,
DESC,
DENSE
)
)


👉 This way:

Departments rank within their store

Stores rank within their region

Regions rank globally

Shahed Shaikh

No changeing, always the same problem with the ranking

bhanu_gautam
Super User
Super User

@yassine_mendy59 , Try using

dax
Adhesions_Rank =
VAR rank_region =
RANKX(
ALL(B[region_label]),
CALCULATE(SUM(A[NB_CUSTOMERS])),
,
DESC,
DENSE
)

VAR rank_magasin =
RANKX(
ALL(B[store_label]),
CALCULATE(SUM(A[NB_CUSTOMERS])),
,
DESC,
DENSE
)

VAR rank_service =
RANKX(
ALL(A[department]),
CALCULATE(SUM(A[NB_CUSTOMERS])),
,
DESC,
DENSE
)

RETURN
SWITCH(
TRUE(),
ISINSCOPE(A[department]), rank_service,
ISINSCOPE(B[store_label]), rank_magasin,
ISINSCOPE(B[region_label]), rank_region
)




Did I answer your question? Mark my post as a solution! And Kudos are appreciated

Proud to be a Super User!




LinkedIn






Poojara_D12
Super User
Super User

Hi @yassine_mendy59 

Can you try :

Adhesions_Rank =
SWITCH(
    TRUE(),
    
    -- Department level (rank within a store)
    ISINSCOPE(A[service_label]),
        RANKX(
            ALLSELECTED(A[service_label]),
            CALCULATE([NB_CUSTOMERS]),
            ,
            DESC,
            DENSE
        ),
    
    -- Store level (rank within a region)
    ISINSCOPE(B[entity_label]),
        RANKX(
            ALLSELECTED(B[entity_label]),
            CALCULATE([NB_CUSTOMERS]),
            ,
            DESC,
            DENSE
        ),
    
    -- Region level (rank across all regions)
    ISINSCOPE(B[region_label]),
        RANKX(
            ALLSELECTED(B[region_label]),
            CALCULATE([NB_CUSTOMERS]),
            ,
            DESC,
            DENSE
        )
)

 

 

Did I answer your question? Mark my post as a solution, this will help others!
If my response(s) assisted you in any way, don't forget to drop me a "Kudos"

Kind Regards,
Poojara - Proud to be a Super User
Data Analyst | MSBI Developer | Power BI Consultant
Consider Subscribing my YouTube for Beginners/Advance Concepts: https://youtube.com/@biconcepts?si=04iw9SYI2HN80HKS

Not working either 😥

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

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.