Stop 08 · Filter Context & CALCULATETopics · 7/9

KEEPFILTERS

Part of the Power BI Roadmap

In short

Your "Snacks and Beverages Sales" measure shows the same number on every category row, even Dairy. KEEPFILTERS makes the measure respect the row it's on.

By default a filter argument in CALCULATE replaces any existing filter on the same column. KEEPFILTERS makes it intersect with the existing filter instead.

The jaadu stands at a hostel room door fitted with two different padlocks, holding a key for each and opening them one after the other.

Think of it like this

Two locks on one door

A hostel room has the warden's lock and your own lock. The door opens only when both allow it. Normally CALCULATE removes the warden's lock and fits its own. KEEPFILTERS keeps both locks, so only what both allow gets through.

Watch it happen

On the Dairy row of a matrix by category

Without KEEPFILTERS

  • Category IN { Snacks, Beverages }
  • Replaces the row's Dairy filter
  • Dairy row shows Snacks + Beverages
  • Every row shows the same number

With KEEPFILTERS

  • Keeps Dairy and adds the new filter
  • Dairy AND (Snacks or Beverages) = nothing
  • Dairy row is blank
  • Snacks row shows only Snacks

Use KEEPFILTERS when the filter should narrow what the visual already shows, not overwrite it.

The big ideas

What you need to know

Default is replace

A CALCULATE filter on a column overwrites existing filters on that same column.

KEEPFILTERS intersects

Wrapping the argument in KEEPFILTERS keeps the old filter and adds yours, like an AND.

Matters in visuals by that column

If the visual isn't grouped or sliced by the same column, both versions give the same result.

Also inside iterators

KEEPFILTERS can wrap a table in an iterator, like SUMX ( KEEPFILTERS ( ... ), ... ), to keep existing filters there too.

See it in code

Walk through the code

dax
1Snacks and Beverages =2CALCULATE (3    [Total Sales],4    'Product'[Category] IN { "Snacks", "Beverages" }5)6 7Snacks and Beverages (Kept) =8CALCULATE (9    [Total Sales],10    KEEPFILTERS ( 'Product'[Category] IN { "Snacks", "Beverages" } )11)

1/3Replaces the row's category, so every row, including Dairy, shows Snacks + Beverages.

Put both in a matrix with Category on rows to see the difference.

Save yourself the pain

Common mistakes

Oops

Thinking KEEPFILTERS is needed on every CALCULATE.

Do this instead

It only matters when the visual or slicers already filter the same column. Add it when you want to narrow, not replace.

Oops

Expecting CALCULATE filters to always combine with slicers on the same column.

Do this instead

By default they override the slicer for that column. KEEPFILTERS is how you make them combine.

Your mission

Replace vs keep

~15 min

Quick check

A slicer selects Dairy. What does CALCULATE ( [Total Sales], KEEPFILTERS ( 'Product'[Category] = "Snacks" ) ) return?

Go deeper

  1. 1Write a Red Products Sales measure with and without KEEPFILTERS.
  2. 2Put both in a table by colour and explain the different rows.
  3. 3Use KEEPFILTERS when a measure should respect what the user already selected.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge