Power Pivot & DAX course · Lesson 7 of 16

CALCULATE: changing the filter context in DAX

Learn DAX CALCULATE with worked PivotTable results: adding and replacing filters, KEEPFILTERS, AND/OR conditions, and FILTER for conditions on measures.

Intermediate 20 min read

CALCULATE is the most important function in DAX. It evaluates an expression in a modified filter context: you keep the filters coming from the PivotTable and add, replace or remove some of them. Shares of total, comparisons with a fixed category, year-to-date, last year: all of them are CALCULATE underneath. This lesson builds it up one step at a time, using the Revenue measure from Your first DAX measures: SUM, AVERAGE, COUNTROWS, DISTINCTCOUNT and the contexts from Filter context and row context in DAX.

The syntax

DAX
CALCULATE ( <expression>, <filter1>, <filter2>, ... )
The expression is usually a measure; each filter is a condition or a table.
  • expression: what to calculate, typically a measure such as [Revenue] or an aggregation such as SUM ( Sales[Amount] ).
  • filters: zero or more filter arguments. Simple conditions on one column, such as Products[Category] = "Computers", are called Boolean filters. You can also pass a table, for example the result of FILTER or ALL.
  • All filter arguments are combined with AND.

Adding a filter

DAX
Revenue Computers := CALCULATE ( [Revenue], Products[Category] = "Computers" )
Revenue of computer products, whatever else is filtered.

Put this next to Revenue in a PivotTable with Region on rows, and each row shows the computer revenue of that region: the region filter from the PivotTable stays, and the category filter is added. North shows 2,320 (Anna Weber's laptops and tablet).

Replacing a filter

Now put Category on rows instead. Something interesting happens:

DAX
Computers Only := CALCULATE ( [Revenue], KEEPFILTERS ( Products[Category] = "Computers" ) )
The same condition wrapped in KEEPFILTERS.
ABCD
1CategoryRevenueRevenue ComputersComputers Only
2Accessories7454950
3Computers495049504950
4Displays9454950
5Grand Total664049504950

Products[Category] on rows. Revenue Computers replaces the row filter; Computers Only intersects with it.

Revenue Computers shows 4,950 in every row, even Accessories. A Boolean filter on a column replaces any existing filter on that same column. In the Accessories row the PivotTable says Category = Accessories, CALCULATE overwrites it with Category = Computers, and the result is the computer revenue. Filters on other columns (Region, Segment, dates) are left alone.

KEEPFILTERS changes replace into intersect: the new condition is combined with the existing one, so the Accessories row has Accessories AND Computers, which is empty, and shows blank. Use the plain form when you want a fixed comparison figure in every row (for example to compute each category's ratio to Computers); use KEEPFILTERS when the condition should only narrow down what is already there.

Several conditions

DAX
North Computers := CALCULATE ( [Revenue], Customers[Region] = "North", Products[Category] = "Computers" )

North or West := CALCULATE ( [Revenue], Customers[Region] = "North" || Customers[Region] = "West" )
AND across arguments; OR with || inside one argument on the same column.

Separate arguments mean AND: North Computers returns 2,320. To get OR on one column, put both conditions in the same argument with || (the DAX OR operator; && is AND). At the grand total, North or West gives 2,495 + 2,058 = 4,553. Newer versions of DAX also accept Customers[Region] IN { "North", "West" }, which is easier to read when the list grows.

Filtering on a numeric column

Boolean filters can use any comparison on a single column. Suppose orders of five units or more count as bulk:

DAX
Bulk Revenue := CALCULATE ( [Revenue], Sales[Qty] >= 5 )
Bulk % := DIVIDE ( [Bulk Revenue], [Revenue] )
Revenue from bulk order lines and its share.
ABCD
1RegionRevenueBulk RevenueBulk %
2East80724029.7%
3North24951757.0%
4South128033025.8%
5West2058
6Grand Total664074511.2%

Customers[Region] on rows. Bulk % formatted as a percentage with 1 decimal.

Only four order lines have five or more units (1002, 1004, 1007 and 1010), all of them mice and keyboards, so bulk revenue is 745. West's only orders were 4 tablets and 2 monitors, so its Bulk Revenue is blank.

When a Boolean filter is not enough: FILTER

A Boolean filter cannot contain a measure. CALCULATE ( [Revenue], [Revenue] > 1000 ) is rejected. When the condition depends on a measure, pass a table built with FILTER, which iterates the rows of a table and keeps those where the condition is true:

DAX
Revenue Big Customers := CALCULATE ( [Revenue], FILTER ( VALUES ( Customers[CustomerID] ), [Revenue] > 1000 ) )
Revenue from customers whose own revenue exceeds 1,000.

VALUES ( Customers[CustomerID] ) lists the customers visible in the current cell. FILTER evaluates [Revenue] for each of them (context transition again) and keeps those above 1,000. Here that is 3 customers out of 4 (Carter & Co, with 807, falls out), so at the grand total the measure returns 5,833.

Watch out. Avoid FILTER ( Sales, ... ) on a whole fact table when a Boolean filter on a column will do. CALCULATE ( [Revenue], FILTER ( Sales, Sales[Qty] >= 5 ) ) returns the same number as Bulk Revenue but forces the engine to scan every row with all columns, which is far slower on large tables. It also behaves differently: a whole-table filter keeps the existing filters on Sales instead of replacing the Qty filter.

CALCULATE without filters

CALCULATE ( [Revenue] ) with no filter arguments looks pointless in a measure, but in a row context it performs context transition, as you saw with the Customer Revenue column. Remember this when you meet CALCULATE inside iterators.

Order of evaluation, simplified

  1. Take the filter context of the current cell (rows, columns, slicers).
  2. If there is a row context, convert it into filters (context transition).
  3. Evaluate each filter argument. Boolean filters replace filters on their column; KEEPFILTERS intersects; ALL and its relatives remove (see Removing filters: ALL, ALLEXCEPT, ALLSELECTED and REMOVEFILTERS).
  4. Evaluate the expression in the new filter context.
Tip. Build measures in layers: a base measure (Revenue), a CALCULATE variation (Revenue Computers), then a ratio (DIVIDE ( [Revenue], [Revenue Computers] )). Each layer can be checked in the PivotTable before the next one is added.

Check yourself

1. What does CALCULATE ( [Revenue], Products[Category] = "Computers" ) show in the Accessories row?

2. Which function makes a Boolean filter intersect with the existing filter instead of replacing it?

3. How do you express Region = North OR Region = West inside CALCULATE?

4. Why is CALCULATE ( [Revenue], [Revenue] > 1000 ) rejected?

5. Which filter arguments are combined with AND?