Forum Discussion
ALL function seems weird - HELP
- 1 year ago
The short answer is that if:
- A column of type date is the primary key of a relationship (
'Dates Dim'[Group 2]in your example ); and - A filter is applied to that column within
CALCULATE
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 3measureRANKXiterates over the rows ofALL( 'Dates Dim'[Group 2] ).- For each of those rows, it evaluates the measure
[Sales 3]. - In the course of evaluating
[Sales 3], context transition adds the current row's value of'Dates Dim'[Group 2]as a filter. - Due to the "automatic date table" behaviour described above, the automatic
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
RANKXcould 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
RANKspecifying partitioning explicity. - A column of type date is the primary key of a relationship (
The short answer is that if:
- A column of type date is the primary key of a relationship (
'Dates Dim'[Group 2]in your example ); and - A filter is applied to that column within
CALCULATE
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
RANKXiterates over the rows ofALL( 'Dates Dim'[Group 2] ).- For each of those rows, it evaluates the measure
[Sales 3]. - In the course of evaluating
[Sales 3], context transition adds the current row's value of'Dates Dim'[Group 2]as a filter. - Due to the "automatic date table" behaviour described above, the automatic
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 🙏🏻
- OwenAuger1 year agoSuper User
You're welcome Chandeep. Sounds good, looking forward to it!
😊🍻