DAX HANDBOOK
6. CALCULATE

If you wish to follow along, you can find PBIX/Excel files at the bottom of the article. 

Authors

Krešimir Ledinski

Krešimir Ledinski

Microsoft certified expert in the field of Business Intelligence. His biggest passions are DAX, M, and data modeling.

Kristian Radoš

Kristian Radoš

Experienced data analyst. Advanced in SQL, PowerApps and M language.

Introduction

CALCULATE/CALCULATETABLE are the most important functions in DAX, and the only ones that are capable of modifying the original filter context. CALCULATE has a few rules you need to follow, and they aren’t that hard to understand. The complexity comes when those rules interfere with each other or with the original filter context.

Original Filter Context

Original filter context is made from all the filters that are influencing the Measure at a certain point in a visual before the Measure starts with its evaluation. These filters can be either direct ones (plotted on the same visual), or cross-filtered through the interaction between 2 or more visuals.

Note: It is extremely important to understand the following example thoroughly. Without the comprehensive understanding of the way original filter context is being created it will be impossible to comprehend filter context modifications or row 2 filter context transition.

When debugging a Measure, always pick a specific cell (or point) in a visual, then check which filters are being applied to the selected cell. Those filters can be direct or indirect. If we focus on the cell with a red border, we can conclude the following.

There are 2 direct filters impacting this cell. Those are:

  • DimProduct[Color]=”Black”
  • DimProduct[CategoryName]=”Components”

Besides direct filters, there are also 2 filters that cross filter this visual, and they are coming from other visuals on the canvas. Those are:

  • DimCustomer[BusinessType]=”Warehouse”
  • DimDate[Year] =”2020”

For that selected cell in the visual, there are 4 filters applied to the cell before the calculation even starts. The calculation [SalesAmount] has the following formula:

SalesAmount = SUMX(FactSales,FactSales[UnitPrice]*FactSales[OrderQuantity])

If we observe the data model, we can see that the measure is using only columns from the FactSales table. But we can also see that all the dimension tables have a 1-* relationship with the FactTable, meaning filters applied to their columns also flow to the FactSales table and filter it before the calculation starts. This is the reason why, even though the calculation is the same for every cell in the visual, it returns different values for different cells. This is all the result of the different filters applied to cells that are forming the original filter context under which the calculation is being evaluated.

Why do you need CALCULATE?

Once you plot measures in a visual, they always fall under the influence of the original filter context. This is extremely useful since you don’t have to worry about changing the calculation for every possible combination of fields plotted on the visual. but this also poses a big issue in case you need to break free of a certain original context filter (e.g. if you wish to always show the grand total values for every field). Or if you wish to change the original filter context so that you could do a comparison between different filters. For those mentioned, and many more purposes, you need a way of modifying or ignoring the original filter context. This is where the CALCULATE/CALCULATETABLE function shine.

Because DAX is all about filtering the data model, every logic you wish to write with DAX has to include some kind of filter manipulation. This is what is meant by “Thinking in DAX terms”. This is the hardest part of DAX to grasp, and since it almost always includes filter context manipulation, it also makes CALCULATE the hardest DAX function to master. But if you pay attention to its basic concepts and order of evaluation, you will have a much easier time understanding and using it correctly in your measures.

Materials

We wish to create the best possible content!

If you are a novice looking for a better explanation of any element of the topic, feel free to comment on the part you didn't quite understand!

If you are an expert in DAX and believe certain topic lacks important internals, your comments are more than welcomed!

COMMENTS

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

OUR SERVICES

Prefer live training or consultations?

Table of Content

Table of Content

GET LATEST BI NEWS TO YOUR INBOX

Receive the latest updates on all business analyst news across all platforms.

By subscribing you are agreeing to our Privacy Policy.

Related blog posts

Newborn Names – A Growing Pool

As the range of consumer choices has expanded over the past 30 years, so has the range of names parents choose for their children. Name Diversity According to data from Republic of Slovenia Statistical office in 1999 there were 187 different female and 190 different male newborn names. By 2024,...

Read more

Newborn Names – Getting Shorter

If it feels like newborn names are getting shorter, you are not imagining it. Past Trending Data from the Statistical Office of the Republic of Slovenia show that in 1992 the average name length was 5.2 letters for girls and 4.9 letters for boys. By the early 2020s, the average...

Read more

Newborn Names – Changing Popularity Over Time

As with most things in life, the names parents give their children change over time. Names for Girls According to data from Republic of Slovenia Statistical office Eva is the most common name given to newborn girls in the period 1992–2024. It was especially popular in the 2000s, ranking third,...

Read more

We have expanded our team again!

We are extremely pleased to announce that Luka has joined our team, with over 20 years of experience in data, analytics and business decision-making. He built his career in leading international companies such as Procter & Gamble, Nielsen and L’Oréal, where he gained strong analytical thinking, a strategic approach and...

Read more
0
Would love your thoughts, please comment.x
()
x
Scroll to Top