Download presentation
Presentation is loading. Please wait.
1
Evaluation Context Concepts and Examples
2
At the beginning, it looks very easy
When you use it, it turns into a nightmare From slide by Marco Russo
3
Every DAX formula is evaluated inside a context; the same formula gives different results
4
There are always two contexts: Filter and Row
Filter context Filters tables Could be empty but that never happens in real world Row context Iterates rows Will apply to only what’s active in the filter context Could be empty No iteration running Row context made easy – start with a calculated column
5
Filter Context
6
Row Context No, this is simply part of the Pivot table filter context
What is a row context? Is this row context? “A row context is a context that always contains a single row and DAX automatically defines it during the creation of calculated columns (and other ways). “
7
Calculated column (inside Power Pivot) example that looks like Excel
How to understand row context really? Compare and contrast to what we already know- Excel Calc columns in Power Pivot make for a good example of row context. Different than Excel, even though it looks like Excel. Calculated column (inside Power Pivot) example that looks like Excel Sales cost naked column=[Quantity]*[Unit Cost] Does it hop over to the cell and use that to identify row context? No, actually, there is no information in the formula about what row to use – Instead, “when you define the calc col, DAX starts an iteration from the first row of the table; creates a row context containing that row and evaluates that expression. Then moves to 2nd row.” Row context: (Is it the rows of the filter context, or something else)? Add rows to the sum on sheet 1. Is this the row context, or is it part of the filter context? p. 66 Row context no filter context – calculated column where you try to do a sum. .
8
What happens when I put a measure in a calc column?
Sales Cost w Sum=SUM([Unit Cost])*SUM([Quantity]) What will happen? Why? Fills in total sum because the calc column has a row context, but no filter context. More accurately, the filter context is the entire table What happens if I add a little magic and why? =CALCULATE(SUM([Unit Cost])*SUM([Quantity]))
9
The Magic of CALCULATE()
Transforms any existing row context into a filter context. Context Transition What functions can perform context transition? Only CALCULATE() and CALCULATETABLE() How does this work between tables? Add Calculate to the Calc Column
10
In the Product Table, create a new calculated column
=SUM(Sales[Unit Cost])*SUM(Sales[Quantity]) Now, put CALCULATE() on this and compare. =CALCULATE(SUM(Sales[Unit Cost])*SUM(Sales[Quantity]) The calculation works across tables. WHY? Because CALCULATE() does context transition – makes the row context a filter context – and SINCE FILTERS PROPAGATE ACROSS RELATED TABLES, the formula works. Add Calculate to the Calc Column
11
Does it matter if you use an iterator, like SUMX on a calculated column?
=SUMX(Sales,Sales[Net Price]*Sales[Quantity]) That is, how does an iterator work in a place where you already have a row context? It’ doesn’t – need to still add CALCULATE() around it. Try this: Create another calculated column on the products table SUMX(RELATEDTABLE(Sales),(Sales[qty]*Sales[Net Price]) RELATEDTABLE is syntax sugar for CALCULATETABLE Final test – try this: On the Product table, add this calc column =SUMX(Customer,[Sales Amount]) Why does this work? Because every time you have a measure in another measure, there is an implicit CALCULATE([Sales Amount]) around the measure, so Context Transition happens!
12
Final test – try this: On the Product table, add this calc column
=SUMX(Customer,[Sales Amount]) Why does this work? Because every time you have a measure in another measure, there is an implicit CALCULATE([Sales Amount]) around the measure, so Context Transition happens! The equation above is equivalent to this – try putting the CALCULATE in yourself =SUMX(Customer, CALCULATE([Sales Amount]))
13
What about MEASURES (not in Calc columns) and Row Context
How many of you have gotten this error? Calculation error in measure 'Sales'[not working]: The value for column 'Unit Cost' in table 'Sales' cannot be determined in the current context. Check that all columns referenced in the calculation expression exist, and that there are no circular dependencies. This can also occur when the formula for a measure refers directly to a column without performing any aggregation--such as sum, average, or count--on that column. The column does not have a single value; it has many values, one for each row of the table, and no row has been specified.
14
MEASURES (not in Calc columns) and Row Context
What happens when I write the following formula: [Gross Margin]= Sales[Price]- Sales[Cost] No naked columns in measures! Missing the row of where to get the current value of sales amount (OK in a calc column) Why does it work when you change it to this? [Gross Margin]=SUM(Sales[Price])-SUM(Sales[Cost]) Because the aggregation means the formula does NOT depend on a row context. It only requires a filter context. (p 69 of The Definitive Guide)
15
Iterators and how they work
= SUMX ( Sales, ← External contexts Sales[SalesAmount] * 1.1 ← External contexts + new Row Context ) Create a new row context for each row of the table received as the first parameter. Evaluate the second parameter inside the newly created row context (plus any other context which existed before the iteration started), for each row of the table. Aggregate the values computed during step 2. It is important to remember that the original contexts are still valid inside the expression: Iterators only add a new row context; they do not modify existing ones in any way. This rule is usually valid, but there is an important exception: If the previous contexts already contained a row context for the same table, then the newly created row context hides the previously existing row context.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.