Reputation: 393
I am learning DAX and confused about the RANKX() in PowerBI. Here is my data:
And here is my measure:
Rank = RANKX(
ALL(RankDemo[Sub Category]),
CALCULATE(SUM(RankDemo[My Value])))
Here is my visual:
The RANKX() works fine, but the field [My Value] has to be summed in the PowerBI field setting:
If I choose Don't Summarize, the rank will be all 1. Anyone could explain this? What does the Sum have to do with the RANKX() or CALCULATE() in DAX. Thanks.
Upvotes: 4
Views: 9509
Reputation: 8148
The problem you are experiencing has nothing to do with RANKX. The issue is with what's called "Implicit measure" - an (unfortunately) common bad practice in Power BI and Power Pivot.
Numeric fields in Power BI can serve 2 roles:
When you drop 'My Value" into a table without any calculation ("Don't summarize"), you are telling Power BI that you want "My Value" to serve as a filter. In Excel pivot tables, it would be equivalent to dropping "My Value" into "Rows" area instead of "Values". So each row in your table is now grouped by "Subcategory + My Value", not just "Subcategory" (in other words, you've made "My Value" a part of your filter context). Since each combination of "Subcategory + My Value" is unique, you are essentially ranking tables consisting of 1 record (that's why it always returns 1).
When you choose "SUM" for "My Value", it's no longer a row filter - it's now a measure. So you filter context now is not "Subcategory" + "My Value", but just "Subcategory", and your RANKX formula works properly. You can easily see this by removing summed "My Value" from the table - RANKX measure will still work the same way.
When you use this "SUM" aggregation for "My Value", you are telling Power BI to create a DAX measure for you implicitly (that's why it's called "implicit measure"). It happens whenever you drop a numeric field into a visual directly. Such implicit measures are considered a bad practice among experienced designers, for a number of reasons, for example:
A solution is:
In your example, I would create an explicit DAX measure:
Total Value = SUM(RankDemo[My Value])
Now you can use it everywhere in your model. You can drop it into a visual to see sum of My Value. Or you can use it in your RANKX measure:
Rank = RANKX( ALL(RankDemo[Sub Category]), [Total Value])
The benefits of such design are:
Upvotes: 9