Starting December 3, join live sessions with database experts and the Microsoft product team to learn just how easy it is to get started
Learn moreGet certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now
Hello,
I encountered confusing results when creating measures using DAX calculations. It appears I’m getting different outputs when using CALCULATE vs CALCULATETABLE in certain circumstances. I’m not getting the expected output when using CALCULATE. I’m adding filter context on both measures and the filter context is the same. Details below.
Measures used:
SAO Opportunities = CALCULATETABLE(
ROW ("SAO Opportunities", COUNTROWS(Opportunity) ),
KEEPFILTERS ( TREATAS ( {"true"}, 'Opportunity'[SAO] )),
USERELATIONSHIP ( 'Date'[Date], Opportunity[SAO Date])
)
SAO Opportunities (Calculate function) = CALCULATE ( COUNTROWS ( Opportunity ),
FILTER ( Opportunity, Opportunity[SAO] = "true" ),
USERELATIONSHIP ( 'Date'[Date], Opportunity[SAO Date] )
)
Output:
For further context, there is an inactive relationship set up for the Date table with the Opportunity SAO Date field. There is a column in the Opportunity table called SAO and it’s a text field.
When I filter the data in the Data View section to meet the above criteria, here is the output (matches output from CALCULATETABLE):
Is anyone able to provide insight into the discrepancy?
Thanks!
Solved! Go to Solution.
Hello @jfbonterra
I will attempt to give some explanation, but a model diagram would be useful to be sure of the explanation.
Firstly, just restating your two measures for reference:
SAO Opportunities =
CALCULATETABLE (
ROW ( "SAO Opportunities", COUNTROWS ( Opportunity ) ),
KEEPFILTERS ( TREATAS ( { "true" }, 'Opportunity'[SAO] ) ),
USERELATIONSHIP ( 'Date'[Date], Opportunity[SAO Date] )
)
SAO Opportunities (Calculate function) =
CALCULATE (
COUNTROWS ( Opportunity ),
FILTER ( Opportunity, Opportunity[SAO] = "true" ),
USERELATIONSHIP ( 'Date'[Date], Opportunity[SAO Date] )
)
The key difference between these measures is the way in which the Opportunity[SAO] filter is applied: a column filter vs a table filter.
The reason I suspect the 2nd measure gives an unexpected result is that FILTER(...) returns the expanded Opportunity table (with filter applied), which includes columns of 'Date' (I'm assuming there is an active relationship between Opportunity and 'Date'). When USERELATIONSHIP is applied to modify the relationship, the 'Date' rows that were originally related to Opportunity rows are applied as filters using the modified relationship. This doesn't happen with the first measure.
The upshot of this is I recommend using a column filter with KEEPFILTERS as in your 1st measure, as table filters can have unintended side effects due to table expansion.
To quote The Definitive Guide to DAX:
"Using table filters is always challenging because of table expansion. Whenever one applies a filter to a table, the filter is really applied to the expanded table, and this can cause several side effects. The golden rule is simple: Try to avoid using table filters whenever possible. Working with columns leads to simpler calculations, whereas working with tables is much more problematic."
Regards
Thank you for the detailed explanation!
Hello @jfbonterra
I will attempt to give some explanation, but a model diagram would be useful to be sure of the explanation.
Firstly, just restating your two measures for reference:
SAO Opportunities =
CALCULATETABLE (
ROW ( "SAO Opportunities", COUNTROWS ( Opportunity ) ),
KEEPFILTERS ( TREATAS ( { "true" }, 'Opportunity'[SAO] ) ),
USERELATIONSHIP ( 'Date'[Date], Opportunity[SAO Date] )
)
SAO Opportunities (Calculate function) =
CALCULATE (
COUNTROWS ( Opportunity ),
FILTER ( Opportunity, Opportunity[SAO] = "true" ),
USERELATIONSHIP ( 'Date'[Date], Opportunity[SAO Date] )
)
The key difference between these measures is the way in which the Opportunity[SAO] filter is applied: a column filter vs a table filter.
The reason I suspect the 2nd measure gives an unexpected result is that FILTER(...) returns the expanded Opportunity table (with filter applied), which includes columns of 'Date' (I'm assuming there is an active relationship between Opportunity and 'Date'). When USERELATIONSHIP is applied to modify the relationship, the 'Date' rows that were originally related to Opportunity rows are applied as filters using the modified relationship. This doesn't happen with the first measure.
The upshot of this is I recommend using a column filter with KEEPFILTERS as in your 1st measure, as table filters can have unintended side effects due to table expansion.
To quote The Definitive Guide to DAX:
"Using table filters is always challenging because of table expansion. Whenever one applies a filter to a table, the filter is really applied to the expanded table, and this can cause several side effects. The golden rule is simple: Try to avoid using table filters whenever possible. Working with columns leads to simpler calculations, whereas working with tables is much more problematic."
Regards
Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.
User | Count |
---|---|
21 | |
21 | |
19 | |
13 | |
12 |
User | Count |
---|---|
42 | |
28 | |
23 | |
22 | |
22 |