Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
Hello Ninjas,
Consider this data model.
with the following data
Data 1 Table
Dimension 1 Table
And the following measures
Sales 1 = SUM('Data 1'[Value])
Rank 1 = RANKX ( ALL('Dimension 1'[Group 2]), [Sales 1],, DESC )
And the visual comes up like this (where Group1 and Group 2 are coming from the Dimension 1 Table)
Nothing unusual here - As expected the ALL function overrides the visual filter and takes all unique values from Group 2 and sustains Group 1 Filter therefore the ranking is done within each value of Group 1.
Cool. Now comes another similar model.
With the following data
Dates Data (Group 2 is intentionally converted to date type)
Dates Dim (Group 2 is intentionally converted to date type)
And the following measures
and then this visual (where group 1 and group 2 are coming from the dates dim table)
Notice the Rank 3 measure calculates overall Rank and not the rank within the Group 1.
Now my question
Here are a few queries
Query on the first model:
Query on the seond model:
Notice the behaviour of the ALL function is as expected when used with CALCULATE but not in the case of the CONCATENATEX function.
WHY is that?
Solved! Go to Solution.
The short answer is that if:
'Dates Dim'[Group 2]
in your example ); andCALCULATE
then the DAX engine treats the table containing the column of type date as though it had been marked as a date table, and automatically removes filters on that table when applying the filter on the specific date column.
See this SQLBI article.
In your example, within the Rank 3
measure
RANKX
iterates over the rows of ALL( 'Dates Dim'[Group 2] )
.[Sales 3]
.[Sales 3]
, context transition adds the current row's value of 'Dates Dim'[Group 2]
as a filter.ALL ( 'Dates Dim' )
removes all filters on 'Dates Dim'.So you end up with a rank for each value of 'Dates Dim'[Group 2]
which is determined ignoring any other filters on 'Dates Dim'
, namely the filter on 'Dates Dim'[Group 1]
due to grouping in the visual.
A possible fix still using RANKX
could be:
Rank 3 =
VAR DatePartition =
CALCULATETABLE (
VALUES ( 'Dates Dim'[Group 2] ),
REMOVEFILTERS ( 'Dates Dim'[Group 2] )
)
RETURN
RANKX (
DatePartition,
CALCULATE (
[Sales 3],
KEEPFILTERS ( DatePartition )
),
,
DESC
)
or you could write a measure using RANK
specifying partitioning explicity.
The short answer is that if:
'Dates Dim'[Group 2]
in your example ); andCALCULATE
then the DAX engine treats the table containing the column of type date as though it had been marked as a date table, and automatically removes filters on that table when applying the filter on the specific date column.
See this SQLBI article.
In your example, within the Rank 3
measure
RANKX
iterates over the rows of ALL( 'Dates Dim'[Group 2] )
.[Sales 3]
.[Sales 3]
, context transition adds the current row's value of 'Dates Dim'[Group 2]
as a filter.ALL ( 'Dates Dim' )
removes all filters on 'Dates Dim'.So you end up with a rank for each value of 'Dates Dim'[Group 2]
which is determined ignoring any other filters on 'Dates Dim'
, namely the filter on 'Dates Dim'[Group 1]
due to grouping in the visual.
A possible fix still using RANKX
could be:
Rank 3 =
VAR DatePartition =
CALCULATETABLE (
VALUES ( 'Dates Dim'[Group 2] ),
REMOVEFILTERS ( 'Dates Dim'[Group 2] )
)
RETURN
RANKX (
DatePartition,
CALCULATE (
[Sales 3],
KEEPFILTERS ( DatePartition )
),
,
DESC
)
or you could write a measure using RANK
specifying partitioning explicity.
Thanks a lot Owen. I'd been intuitvely working with dual behaviour for a long time until I ran some tests.
I owe you a beer when we meet. Big fan otherwise 🙏🏻
Perfect