Forum Discussion
RANKX Formula Returning Duplicate Ranks
- 8 months ago
Hi SanketSk,
It is hard to solve when there is no Sample data but here is some approaches you can try ☺️❤️
So First let's clarify the problem....The problem is not with your formula's logic per se but with its context transition when you add more columns
Let me Make it simpler for you to understand:
- When you only have Part Code in your visual the formula works because:
Each row context has only one Part Code
- CALCULATE(SUM(Quantity)) calculates the total quantity for that specific part code
- ALL(Part Code) ensures you're ranking against all part codes
So when you add Description and 9NC the row context now includes the combination of (Part Code + Description + 9NC) Your formula is now:
- Calculating the sum of quantity for each combination of Part Code + Description + 9NC
- But still ranking against all Part Codes (not the combinations)
What dou you need to do?
You need to modify your formula to handle the multi column context ; Here are three approaches:
- First Approach: Rank by Part Code Only (I recommend you this)
- If you want the rank to be based solely on the Part Code total quantity (regardless of Description/9NC):
Rank_Spare_by_Quantity = RANKX( ALL(Spare_Consumption_Final_File[Part Code]), CALCULATE( SUM(Spare_Consumption_Final_File[Quantity]), ALLEXCEPT(Spare_Consumption_Final_File, Spare_Consumption_Final_File[Part Code]) ), , DESC, DENSE )- Second Approach: Rank by Part Code + Description + 9NC (Combination)
- If you want unique ranks for each combination:
Rank_Spare_by_Quantity = VAR CurrentPartCode = MAX(Spare_Consumption_Final_File[Part Code]) VAR CurrentDescription = MAX(Spare_Consumption_Final_File[Description]) VAR Current9NC = MAX(Spare_Consumption_Final_File[9NC]) RETURN RANKX( ALL( Spare_Consumption_Final_File[Part Code], Spare_Consumption_Final_File[Description], Spare_Consumption_Final_File[9NC] ), CALCULATE(SUM(Spare_Consumption_Final_File[Quantity])), , DESC, DENSE )- Third Approach: Using SUMMARIZE (Alternative approach)
Rank_Spare_by_Quantity = VAR SummaryTable = SUMMARIZE( Spare_Consumption_Final_File, Spare_Consumption_Final_File[Part Code], "TotalQty", SUM(Spare_Consumption_Final_File[Quantity]) ) RETURN RANKX( SummaryTable, [TotalQty], , DESC, DENSE )Note: Try First Approach First it should give you consistent ranks where all rows for the same Part Code have the same rank value even when Description and 9NC columns are visible.
if this post helps, then I would appreciate a thumbs up and mark it as the solution to help the other members find it more quickly. - When you only have Part Code in your visual the formula works because:
Hi SanketSk ,
If I understood, your measure works correctly when only the column Part Code is present in the visual. However, when you add Description and 9NC, the ranking becomes inconsistent or duplicated?
This happens because RANKX is sensitive to the context in which it is evaluated.
When you add more columns to the visual, the row context changes — Power BI evaluates the measure for each unique combination of Part Code, Description, and 9NC. If these combinations are not unique per Part Code, the ranking becomes ambiguous or duplicated.
To ensure consistent ranking based only on Part Code, you should remove all filters from the visual except for Part Code. You can do this by using ALLSELECTED or REMOVEFILTERS depending on your needs.
Here’s a revised version using REMOVEFILTERS
Rank_Spare_by_Quantity =
RANKX(
REMOVEFILTERS(Spare_Consumption_Final_File[Description], Spare_Consumption_Final_File[9NC]),
CALCULATE(SUM(Spare_Consumption_Final_File[Quantity])),
,
DESC
)
Alternatively, if you want to rank within the current selection but ignore the extra columns
Rank_Spare_by_Quantity =
RANKX(
ALLSELECTED(Spare_Consumption_Final_File[Part Code]),
CALCULATE(SUM(Spare_Consumption_Final_File[Quantity])),
,
DESC
)
✅ If this response resolved your issue, please mark it as correct to assist other members of the community.