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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
mangoose
Frequent Visitor

More Complexity/Recalculating with RANKX

I have read all over, and havent found something useful.

 

I have a dataset with 5 levels of hierarchy, and I am trying to create a leaderboard dashboard at different levels using the same slicer: when slicing for region, i need it to rank all levels of the hierarchy, not just the lowest level (store).

 

When trying to rank the "district" level (1 up from store), Im able to get a rank which works when unfiltered, and i end up with 1,2,3,4,5,...etc.

but when filtered, it doesnt rerank, and instead cuts out the filtered rows so I end up with 4,8,15,...etc.

 

Is there a way to filter and recalculate rank?

 

Here is the measure I created:

Rank5 = 
IF (
    FIRSTNONBLANK ( Store_Data[District], Store_Data[District] ) <> BLANK (),
    RANKX (
        FILTER ( allselected ( Store_Data[District] ), Store_Data[District] <> BLANK () ),
        calculate(sum(Store_Data[Total Volume]),allexcept(Store_Data,Store_Data[District])),,,SKIP
    )
)

 

thanks! 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

HI @mangoose,

 

You can try to use below measure formula to get dynamic rank:

Rank District A =
IF (
    FIRSTNONBLANK ( Store_Data[District], Store_Data[District] ) <> BLANK (),
    RANKX (
        FILTER ( ALLSELECTED ( Store_Data ), Store_Data[District] <> BLANK () ),
        CALCULATE (
            SUM ( Store_Data[Total Volume] ),
            ALLEXCEPT ( Store_Data, Store_Data[District] )
        ),,,DENSE
    )
)


Rank District B =
RANKX (
    ALLSELECTED ( Store_Data ),
    CALCULATE (
        SUM ( Store_Data[Total Volume] ),
        ALLEXCEPT ( Store_Data, Store_Data[District] )
    ),,,DENSE
)

10.PNG

 

Regards.
Xiaoxin Sheng

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hi @mangoose,

 

Can you please share some sample data to test and coding formula? It is hard to clearly your scenario with any sample data.

 

BTW, it is hard to achieve dynamic ranking on hierarchy level. In my opinion, maybe you can write multiple calculation formulas for each level, then use if statement to check current level and return correspond calculation result from matched calculation formula.


Regards,

Xiaoxin Sheng

Hi Xiaoxin, happy to provide a demo. Thanks for your reply. I built a .pbix file here: Sample PBIX

 

Here is as simple as I can explain: 

 

My dataset is as follows:
Dataset.PNG

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

I created a measure summing up Total Volume, called "TotVol":

TotVol = sum(Store_Data[Total Volume])

 

Then I made a simple dashboard with both a store and a district leaderboard using rank functions:

Dashboard Unfiltered.PNG

 

 

 

 

 

 

 

 

 

As you can see, rankings work when unfiltered for both Store and District - highest volume = #1, second highest #2 etc.

 

However, when I filter for the "North" region, here is what it looks like:Dashboard Filtered.PNG

 

 

 

 

 

 

 

 

As you can see, the store ranking is now correct, Store 2591 is now #1 (instead of #5 when unfiltered), but District NB D6 is first on the list, but still displays #5 in both of my rank measures. I want District NB D6 to display "1", and District EA D1 to display "2" etc.

 

Here are the rank functions (store works well, but neither district rank measures work)

 

 

Rank Total Volume Store = 
IF (
    FIRSTNONBLANK ( Store_Data[Store], Store_Data[Store] ) <> BLANK (),
    RANKX (
        FILTER ( allselected ( Store_Data ), Store_Data[Store] <> BLANK () ),
        CALCULATE ( sum(Store_Data[Total Volume]))
    )
)
Rank District A = 
IF (
    FIRSTNONBLANK ( Store_Data[District], Store_Data[District] ) <> BLANK (),
    RANKX (
        FILTER ( allselected ( Store_Data[District] ), Store_Data[District] <> BLANK () ),
        calculate(sum(Store_Data[Total Volume]),allexcept(Store_Data,Store_Data[District]))
    )
)
Rank District B = rankx(ALLSELECTED(Store_Data[District]),calculate(sum(Store_Data[Total Volume]),allexcept(Store_Data,Store_Data[District])))

 

Anonymous
Not applicable

HI @mangoose,

 

You can try to use below measure formula to get dynamic rank:

Rank District A =
IF (
    FIRSTNONBLANK ( Store_Data[District], Store_Data[District] ) <> BLANK (),
    RANKX (
        FILTER ( ALLSELECTED ( Store_Data ), Store_Data[District] <> BLANK () ),
        CALCULATE (
            SUM ( Store_Data[Total Volume] ),
            ALLEXCEPT ( Store_Data, Store_Data[District] )
        ),,,DENSE
    )
)


Rank District B =
RANKX (
    ALLSELECTED ( Store_Data ),
    CALCULATE (
        SUM ( Store_Data[Total Volume] ),
        ALLEXCEPT ( Store_Data, Store_Data[District] )
    ),,,DENSE
)

10.PNG

 

Regards.
Xiaoxin Sheng

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.