Elevate DAX: 5 Key Concepts To Master CALCULATE In Power BI

Understand Everything About The Most Powerful DAX Function in Power BI

Akshay Mishra
Microsoft Power BI
6 min readFeb 27, 2024

--

Photo by Scott Graham on Unsplash

Welcome back to my Elevate DAX blog series. This is my 3rd article in the series. If you haven’t had the chance to catch up on the previous articles, you can find them here.

In this blog I’ll try to explain everything about CALCULATE, often hailed as the most powerful yet sometimes daunting DAX function!!!

Introduction
CALCULATE is definitely a complex function. Understanding CALCULATE requires grasping several fundamental concepts, which I’ve broken down in this blog.

It also took a lot of time for me to have a grasp on it. Drawing from my experience, I’ve identified five key concepts to help you understand DAX better. By explaining these concepts in detail, my aim is to provide you with a solid foundation that demystifies CALCULATE once and for all.!!!

To maintain continuity in this blog series, I’m using the Running Total calculation from my previous blog to explain CALCULATE.

Running Total Using CALCULATE (figure by Author)

While the DAX code above may initially appear similar to what I discussed in my Running Total blog, upon closer inspection, we find that the calculation isn’t yielding the expected results. Instead of a running sum, we’re seeing the Total Profit value repeated in every row.

Let me reveal the reason for this discrepancy in the result. Instead of having MaxDate calculated in the current code, we are making a reference to the already calculated [MaxDate] measure.

To understand why this approach isn’t giving us the desired outcome, lets dig deeper. There are some fundamental DAX concepts involved crucial for understanding the functionality of CALCULATE. Let’s break down these concepts one-by-one and explore how they relate to the DAX code mentioned above.

Are you ready? Let’s dive in!

1. Row Context

As the name suggest, it is the concept of “Current Row”. Basically whenever there is a need to access individual rows during a calculation, the Row context directs the DAX engine to fetch the row in the evaluation context.

In calculated columns, the row context is inherent because the formula iterates over each row of the table. However, for measures, the row context must be explicitly defined. This is typically achieved through the use of iterator functions such as FILTER, SUMX, COUNTX, MAXX, and others.

Now, let’s apply this understanding to our DAX formula. Within CALCULATE, we’re employing FILTER as an iterator to sequentially scan through all values of Datedim[Date] row-by-row, returning a set of rows that meet our specified filter condition.

FILTER Iterator Inside CALCULATE (figure by Author)

2. Filter Context

In our Running Total example, it’s crucial to understand the interplay between the various sets of filters applied to the calculation. Firstly, there’s the outer filter applied to the Date in the table visual. However, due to our use of ALL(DateDim[Date]) within the CALCULATE filter condition, this outer filter is ignored during the calculation process.

Next, we encounter the inner filter, which is defined within the FILTER function itself. This inner filter determines which rows of the Datedim table are included in the calculation, based on the specified criteria.

Outer Filter Context Overridden by CALCULATE (fig by Author)

3. Context Transition

Now that we have already discussed Row and Filter context, Context Transition has to be discussed. It means that an existing Row Context gets converted into an equivalent Filter Context. There are two essential conditions for context transition to occur:

  1. Presence of an Iterator
  2. Presence of CALCULATE

In our Running Total calculation, the presence of FILTER as the iterator and the use of the [MaxDate] measure within the FILTER function confirm the involvement of CALCULATE. As mentioned in my previous blog post, a measure always implicitly implies the use of CALCULATE.

Hence we have a perfect recipe in our code for context transition to happen. So, what exactly is happening here?

Due to context transition, the [MaxDate] measure isn’t computing the maximum date within the current filter context (outer/inner). Instead, it calculates the maximum date within a new filter context created through context transition. This new filter context is essentially the transformed row context of the FILTER function, iterating through each row of dates, considering only the currently iterated date.

Consequently, [MaxDate] will always yield the currently iterated date, leading the FILTER function to always return all the dates. This explains why we’re observing incorrect values for the Running Total.

The correct approach is to calculate the maximum date during the Running Total calculation itself, without relying on a measure. Correct code would be the one that I discussed in my running total blog.

Correct DAX for Running Total (figure by Author)

4. Calculate Modifiers

There are some situations when we want CALCULATE to apply some filters to control the calculation. With the help of modifiers, we can control how implicit filters work.

In our Running Total DAX, we have used ALL function which modifies the implicit filter on Date in the table visual i.e. it ignores the outer filter context. Without the use of ALL(Datedim[Date]), our calculation would yield a different result, similar to the [Total Profit] calculation

Wrong Running Total calculation Without ALL Modifier (figure by Author)

Some other modifiers are as below:
→KEEPFILTER: To stop CALCULATE from overriding existing filters
→REMOVEFILTER: To remove any existing filter during calculation
→ALLEXCEPT/ALLSELCETED: To return specific rows in a table or specific values in a column based upon exceptions/selection respectively.

5. Expanded Tables

Expanded tables concept is an implicit part of DAX, representing an enlarged version of tables connected to each other through relationships. As the name suggests, the expanded table includes all columns from the base table and any related tables connected to it.

In our scenario, the Datedim table is linked to the Financial table through a one-to-many relationship based on the date field. This means that any filter applied to the Datedim[Date] column also filters the Financial table, following the established relationship between them.

Connected Tables (figure by Author)

When we consider the Running Total calculation, it’s important to understand that filters applied to the Datedim table have a cascading effect on the Financial table due to the expanded table concept. The expanded version of the Datedim table includes the Date field from the Financial table, so any filter applied to Datedim also affects the Financial table.

This same logic applies to context transition. If the iteration occurs on a field from the base table, all related fields in the expanded table are filtered within the transformed filter context.

Conclusion

And that wraps up our discussion on the fundamental concepts related to CALCULATE. While there’s much more to explore when utilizing CALCULATE in DAX, these concepts serve as the building blocks for understanding its functionality.

I trust that this blog has provided you with clarity on how CALCULATE operates. While I’ve covered these concepts briefly here, stay tuned for more detailed blogs on each of them in the future.

If you have any questions or require further clarification, feel free to leave a comment — I’m here to help. Your feedback is also invaluable, so please share your thoughts.

Connect with me through my social media handles; I look forward to hearing from you!

  1. Medium
  2. LinkedIn
  3. Twitter/X

Access previous stories in the Elevate DAX series:

Elevate DAX

7 stories

Don’t forget to subscribe to

👉 Power BI Publication

👉 Power BI Newsletter

and join our Power BI community

👉 Power BI Masterclass

--

--

Akshay Mishra
Microsoft Power BI

Professionally, I am a Power BI and Data Analytics enthusiast | Outside of work, a passionate Traveller and Explorer. https://linktr.ee/akshaymishra_bi