CALCULATE & Filter Context: DAX That Thinks
CALCULATE is the most important function in DAX — it lets a measure change the filters it works under.
What you will learn
- Explain filter context and row context in plain words
- Use CALCULATE to change a measure’s filters
- Read percent-of-total and what-if style measures
Why the same measure gives different numbers
Earlier you saw that Total Sales = SUM(Sales[Amount]) shows 11,750 on a card but a different number per region in a bar chart. That happens because every visual surrounds the measure with an invisible set of filters. That invisible set has a name: filter context.
Filter context, in plain words
Filter context is simply “which rows are allowed right now”. A card with nothing else applied allows all rows, so SUM totals everything. A bar for “North” allows only North rows, so the same SUM totals just those. You never type this context — the visual, the slicers and the axis create it for you.
| Where the measure sits | Filter context (rows allowed) | Total Sales shows |
|---|---|---|
| A plain card | All rows | 11,750 |
| The “North” bar | Only Region = North | 10,100 |
| A slicer set to “Mouse” | Only Product = Mouse | 450 |
Row context — the other kind
There is a second kind of context, used by calculated columns. Row context means “the one current row”. When you wrote Tax = Sales[Amount] * 0.1, DAX knew which row’s Amount to use because a column formula runs row by row — that is row context. Measures, by contrast, work in filter context (a whole set of rows at once).
CALCULATE — change the filters yourself
Usually the visual sets the filter context. But sometimes you want a measure to override it. That is the one job of CALCULATE — the single most important function in DAX. It reads as CALCULATE( the maths, the new filters ): it computes an expression under filters you specify.
Worked example: a measure that always totals North, no matter what the visual says.
North Sales =
CALCULATE(
SUM(Sales[Amount]), -- the maths
Sales[Region] = "North" -- the filter to apply
)Note: Output: North Sales always shows 10,100, even inside a “South” bar or with a different slicer chosen, because CALCULATE replaces the Region filter with Region = “North”. Compare that with plain Total Sales, which obeys whatever the visual asks. CALCULATE is how you take control of the filters.
The classic: percent of total
A measure that needs CALCULATE in nearly every real report is percent of total. The trick: get each slice’s sales, then divide by the grand total — and to get the grand total you must remove the slice filter using ALL (which means “ignore this filter”).
Pct of Total =
DIVIDE(
SUM(Sales[Amount]),
CALCULATE(SUM(Sales[Amount]), ALL(Sales))
)Note: Output: In a table by Region, North shows about 0.86 (10,100 of 11,750), South about 0.04 and East about 0.10. The top SUM follows each row’s filter (one region), while CALCULATE with ALL(Sales) ignores the region filter to always return the full 11,750 — so the division gives each region’s share. Format it as a percentage to read 86%, 4%, 10%. DIVIDE is used instead of / so a zero denominator returns blank, not an error.
Context transition — the subtle part
One more idea you will meet: context transition. When you call a measure (or wrap a column expression in CALCULATE) inside a row context, CALCULATE quietly turns “the current row” into a filter. In short: CALCULATE converts a row context into a filter context. You do not need to master this on day one — just remember the phrase, because it explains why measures behave consistently when used inside calculated columns or iterators like SUMX.
Tip: Say it as a sentence: CALCULATE( what to compute , how to filter ). The first argument is the number; everything after it adjusts the filter context the number is computed under.
Watch out: CALCULATE filter arguments replace that column’s existing filter rather than adding to it. So CALCULATE(SUM(Sales[Amount]), Sales[Region]="North") ignores any Region the user picked in a slicer. Use KEEPFILTERS if you instead want to narrow the user’s selection rather than overwrite it.
Q. What is the main job of the CALCULATE function in DAX?
✍️ Practice
- Write a
North Salesmeasure with CALCULATE and confirm it stays at North’s total in every visual. - Build a
Pct of Totalmeasure using CALCULATE + ALL and format it as a percentage.
🏠 Homework
- Create a measure that returns sales for one chosen product using CALCULATE, then explain in two sentences how its filter context differs from plain Total Sales.