KEEPFILTERS in DAX: add a filter instead of replacing it

How KEEPFILTERS changes CALCULATE from replacing filters to intersecting them, examples with a matrix by colour, KEEPFILTERS in iterators like SUMX over VALUES, when the default replacement is what you want, and how it relates to FILTER over a table.

Short answer: by default, a filter argument in CALCULATE replaces the existing filter on the same columns. KEEPFILTERS ( filter ) makes it intersect with the existing filter instead: the result is what both allow. CALCULATE ( [Revenue], KEEPFILTERS ( 'Product'[Color] IN { "Red", "Blue" } ) ) shows red revenue on the red row, blue on the blue row, and blank on the green row, instead of red-plus-blue on every row.

A measure "Revenue of red and blue products" placed in a matrix by colour shows the same number on every row: the row's colour filter was replaced. Most of the time that is a mistake, and KEEPFILTERS is the fix.

What does the difference look like?

Red and Blue =
CALCULATE ( [Revenue], 'Product'[Color] IN { "Red", "Blue" } )

Red and Blue Kept =
CALCULATE ( [Revenue], KEEPFILTERS ( 'Product'[Color] IN { "Red", "Blue" } ) )
ColorRevenueRed and BlueRed and Blue Kept
Red4001,000400
Blue6001,000600
Green2501,000
Black9001,000
Total2,1501,0001,000
A table by colour

Red and Blue replaces the row's colour with Red or Blue, so every row shows 1,000. Red and Blue Kept intersects: Red row and {Red, Blue} gives Red; Green row and {Red, Blue} gives nothing.

When do you want replacement?

When the measure should be the same whatever the row says: a benchmark, a fixed segment, a total for comparison.

-- compare each colour with the black products, on every row
Black Benchmark = CALCULATE ( [Revenue], 'Product'[Color] = "Black" )

How does KEEPFILTERS help in iterators?

An iterator that calls a measure applies each row as a filter (context transition), and that filter also replaces the existing one on the same column. Wrapping the iterated table in KEEPFILTERS makes the row filter intersect instead:

Sum over Colours = SUMX ( ALL ( 'Product'[Color] ), [Revenue] )

Sum over Colours Kept = SUMX ( KEEPFILTERS ( ALL ( 'Product'[Color] ) ), [Revenue] )

In the table by colour, Sum over Colours shows 2,150 on every row: each colour of ALL replaces the row's colour. Sum over Colours Kept shows each row's own revenue, because only the colour that matches the row survives the intersection.

How is it different from FILTER over a table?

CALCULATE ( [Revenue], FILTER ( 'Product', 'Product'[Color] IN { "Red", "Blue" } ) ) also respects the existing colour filter, because FILTER only sees the products already filtered. But it iterates the whole Product table and puts all its columns into the filter. KEEPFILTERS with a Boolean filter gives the intersection with a filter on one column, which is cheaper and clearer.

What goes wrong?

  • Same number on every row. A filter on the visual's own column is replaced; add KEEPFILTERS.
  • KEEPFILTERS in the wrong place. It works on filter arguments of CALCULATE and CALCULATETABLE and on tables iterated with context transition; elsewhere it changes nothing.
  • DirectQuery. Not supported in calculated columns or row-level security rules in DirectQuery mode.

Where can you check this?

Where do you start?

Find measures with a filter on a column that is also on the visual's rows, and decide for each: fixed benchmark (keep replacement) or the part of the row (add KEEPFILTERS). CALCULATE explains the default.

Keep reading

More from the blog.

Every post

Next

Building this? Say hello.

A question about this post, a pattern you would build differently, or the next thing you want written out: Behnia reads every message.