cancel
Showing results for
Did you mean:

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

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
Community Support

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
)
```

Regards.
Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
3 REPLIES 3
Community Support

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

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
Frequent Visitor

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:

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:

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:

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])))`

Community Support

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
)
```

Regards.
Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.