Forum Discussion
Am I Using RANKX Incorrectly? It causes a cartesian join across relationships.
When I use RANKX, it seems to cause a cartesian join across a relationship.
Example:
FACTtable
| ColourId | Value |
| 1 | 6 |
| 2 | 2 |
| 3 | 7 |
| 4 | 2 |
| 5 | 1 |
| 5 | 4 |
DIMtable
| ColourID | Colour |
| 1 | Red |
| 2 | White |
| 3 | Blue |
| 4 | Orange |
| 5 | Purple |
Create a relationship between these on ColourID
Now create a RANKX measure:
The ranking causes a strange cartesian join that we can see when we add the colour IDs from each respective table:
Now we can fix this by changing the RANK function to be:
My question is. Am I doing something wrong? It took a lot of research and digging to find the solution to this and it doesn't really feel intuitive. When I look at other people's example online, they don't seem to have this issue and there aren't that many posts about this issue online. I'd have thought the forums would be full of this type of issue.
Am I doing my RANKX wrong or does Power BI just expect us to use HASONEVALUE every time we use RANKX?
The function works fine on a single table if you merge your DIMs into your FACT but obviously this goes against all guidance, yet there is no explicit guidance on how to use RANKX properly in a Dimensional model.
Such a seemingly simple issue involves many intricacies under the hood. Even a seasoned DAX user might be incapable to wrap his head around it.
- It's NOT rankx() that causes the cartisian product but it's the fact the cartisian product already exists there and those "unreasonable" rows are "accidentally" made visible by your measure.
Here's a simple way to prove its existence,
all rows can be made dissapear this way,
- When you drag columns into a viz functions, they function independently as separate filters; a measure evaluates under the comprehensive effects of all these filters. As long as a measure evaluates any value other than BLANK(), the correspondent row shows.
- A reasonable measure can be authored this way
- Rankx() is one of the most frequently used and also trickiest functions in DAX. Once you manage to master it, DAX is almost under full control.
- It's NOT rankx() that causes the cartisian product but it's the fact the cartisian product already exists there and those "unreasonable" rows are "accidentally" made visible by your measure.
2 Replies
- CNENFRNLCommunity Champion
Such a seemingly simple issue involves many intricacies under the hood. Even a seasoned DAX user might be incapable to wrap his head around it.
- It's NOT rankx() that causes the cartisian product but it's the fact the cartisian product already exists there and those "unreasonable" rows are "accidentally" made visible by your measure.
Here's a simple way to prove its existence,
all rows can be made dissapear this way,
- When you drag columns into a viz functions, they function independently as separate filters; a measure evaluates under the comprehensive effects of all these filters. As long as a measure evaluates any value other than BLANK(), the correspondent row shows.
- A reasonable measure can be authored this way
- Rankx() is one of the most frequently used and also trickiest functions in DAX. Once you manage to master it, DAX is almost under full control.
- It's NOT rankx() that causes the cartisian product but it's the fact the cartisian product already exists there and those "unreasonable" rows are "accidentally" made visible by your measure.
- v-yueyunzh-msftCommunity Support
Hi , tbennett93
According to your description, you want to show the table , like this:
But when you put the field you need on the visual , it seems to occur the "crossjoin".
The dax you use for this need is right:
rankcol = RANKX(all('FACTtable'),CALCULATE(SUM('FACTtable'[Value])))But for the ALL() function used , it clear the filter in fact table. Internal,it also clear the filter of the Dim table, so when you put the 'DImTable'[Colour] field on the visual , it occur the "Crossjoin"!
If you just put the measure in the fact table , it works perfectly:
So for this question, you can create another measure to show the color name :
Color Name = var _colorId = MAX('FACTtable'[ColourId]) var _t = FILTER('DIMtable','DIMtable'[ColourID] = _colorId) return MAXX(_t,[Colour])Then in the end we can meet your need:
Thank you for your time and sharing, and thank you for your support and understanding of PowerBI!
Best Regards,
Aniya Zhang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly