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

Advanced Excel training for client Hilding Anders

We have started our autumn training sessions! We are starting with a dear client from Međimurje, Hilding Anders, where we’ll deliver an advanced Excel training for the logistics and production departments. The training will last a total of 16 hours, split into 2-hour sessions. Advanced Excel is useful for all...

Read more

Napredna Excel edukacija za klijenta Hilding Anders

Krenuli smo s jesenskim edukacijama! Edukacije započinjemo kod dragog klijenta iz Međimurja, Hilding Anders, gdje ćemo za odjele logistike i proizvodnje proći kroz naprednu Excel edukaciju. Edukacija će trajati sveukupno 16 sati u terminima po 2 sata. Napredni Excel koristan je svim polaznicima koji svakodnevno rade sa velikom količinom podataka,...

Read more

Business improvement with the help of Power BI

How did we improve our client’s business with the help of Power BI? A global brand with a branch in the Adriatic region had several problems: • Uncollected receivables• Sales without margin (price lists in disarray)• Hidden losses• Unprofitable product lines and stockpiling• Lack of focus in sales and procurement,...

Read more

Unaprijeđenje poslovanja pomoću Power BI-ja

Kako smo uz pomoć Power BI-ja unaprijedili poslovanje našeg klijenta? Globalni brand s podružnicom u Adriatic regiji je imao više problema: Nenaplaćena potraživanja Prodaja bez marže (cjenici u rasulu) Skriveni gubici Neprofitne linije proizvoda i gomilanje zaliha Nije bilo fokusa u prodaji i nabavi već se sve obavljalo stihijski Zašto...

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