In this tutorial, we’ll learn about evaluation context in DAX.
Evaluation or initial context is the environment from which your dynamic calculation is based from.
It’s important to understand how evaluation context works and how it affects your DAX calculation. So in the next section, I want to show you the different ways to create the evaluation or initial context.
Creating Evaluation Or Initial Context In DAX
First, you need to have a Date slicer and City filter in your table to achieve dynamic data.
There’s always an initial context that you need to be aware of before doing any calculation. You can find the said evaluation context from any location.
In the sample table, the context can be found from the Date slicer or from the City filter. For example, if you select the Christchurch filter, the results in the table will change. If you also adjust the dates from the 1st of January to the 19th of January, you can see that the results changed.
For example, for the 1st of January, the result is 90,074.80 while on the 19th of January, it’s 111,293.70. This proves that evaluation context can really change for every single result in DAX calculations.
To show you another example, let’s turn the existing table into a Total Sales by Date visualization.
When you select or clear a filter or even adjust the time frame, you can get a new result inside the visualization.
The key thing to note is that the formula that we are using in this table and visualization doesn’t change. It is exactly the same formula.
In Excel, every single formula you see will be slightly different because the cell references will be different. In Power BI, the formula stays exactly the same. What changes the result is the context or the environment in which it is evaluated. This applies for every measure you create.
Every calculation you do within Power BI has an initial context that you need to understand. Before you look at any individual result, you need to understand the current context of that calculation.
If you didn’t know what was selected in the city slicer, you would be looking at this visualization and know that this is the Total Sales by Date. But what kind of Total Sales is it?
In the example below, you’ll know that the total sales displayed in the visualization are from Hamilton based on the slicer.
Relationship Between Context And Data Model
Understanding how this context works is a crucial part of learning and understanding DAX – because this is as easy as it gets. You can then start manipulating the context within formulas and start doing some advanced analysis.
Hopefully, you’re also starting to see how important the model is because the context is directly created by the relationships among your tables.
The city slicer that we are selecting is from the Regions table while the calculation of Total Sales is in the Sales table.
This is called filter propagation.
Filters propagate down the relationship through the arrows and they head towards the many sides on the fact table, which is the Sales table in this case.
You can do as many of these things at any one time because the context can come from one lookup table and another lookup table. And then we could layer even more and create more context. We just have to understand what the initial context of the calculation is.
***** Related Links *****
Understanding Evaluation Context In Power BI
How The DAX Calculation Engine Works
What Is Context Transition And Why Does It Matter?
Conclusion
Filters and slicers are basic ways to create evaluation context in your DAX calculations.
I hope you understand just how important evaluation (initial) context is. If you want to do an advanced analysis later on, you’ll have to change the context within your formulas.
Cheers!
Sam