Understanding Evaluation Contexts in DAX
- 11/3/2015
Evaluation contexts recap
Let’s recap all what we have learned about evaluation contexts.
- Evaluation context is the context that modifies the value of a DAX expression by filtering the data model and providing the concept of current row, when needed to access a column value.
- The evaluation context consists of two parts: the row context and the filter context. They co-exist and they are present for all the formulas. In order to understand a formula’s behavior, you always need to take into account both contexts, because they operate at the same time.
- DAX creates a row context automatically when you define a calculated column. You can also create a row context programmatically by using iterator functions. All iterators define a row context.
- You can nest row contexts and, in such a case, the EARLIER function is useful to get access to the previous row context.
- DAX creates a filter context when you use a pivot table by using fields on rows, columns, slicers, and filters. There is a way to programmatically create filter contexts by using CALCULATE, but we still have not learned it yet. We hope that at this point you should be very curious to learn more about it!
- Row context does not propagate through relationships automatically. Propagation happens manually by using RELATED and RELATEDTABLE. You need to use these functions on the correct side of a one-to-many relationship: RELATED on the many side, RELATEDTABLE on the one side.
- Filter context automatically propagates following the filtering of the relationship. It always propagates from the one side of the relationship to the many side. In addition, you also have the option of enabling the propagation from the many side to the one side. No functions are available to force the propagation: Everything happens inside the engine in an automatic way, according to the definition of relationships in the data model.
- VALUES returns a table containing a one-column table with all the unique values of the column that are visible in the current filter context. You can use the resulting table as a parameter to any iterator.
At this point, you have learned the most complex conceptual topics of the DAX language. These points rule all the evaluation flows of your formulas and they are the pillars of the DAX language. Whenever you encounter an expression that does not compute what you want, there’s a huge chance that was because you have not fully understood these rules.
As we said in the introduction, at a first reading all these topics look very simple. In fact, they are. What makes them complex is the fact that in a complex expression you might have several evaluation contexts active in different parts of the formula. Mastering evaluation context is a skill that you will gain with experience, and we will try to help you on this by showing many examples in the next chapters. After some DAX formulas on your own, you will intuitively know which contexts are used and which functions they require and you will finally master the DAX language.