Power BI / Intermediate: context, quality and access / Lesson 14
Intersecting and Replacing Filters
Compare CALCULATE filter replacement with KEEPFILTERS using a small, auditable model.
On this page
Lesson goals
- Trace filter context for a CALCULATE expression.
- Explain the effect of KEEPFILTERS on the same column.
- Validate results under conflicting and compatible selections.
Build a controlled context
For this Power BI Desktop lab, create a Product table with one red and one blue product, and a related Sales table whose red total is 10 and blue total is 20. Define [Total Sales] as the sum of the Sales amount. Confirm the relationship filters Sales by Product and that the unfiltered total is 30.
The snippets require this model and are illustrative; the public reader does not execute them. Use a table or slicer to apply a blue selection, and inspect the model and visual filters before comparing the measures. Security filters and more complex relationships require separate testing.
Apply a new color filter
The first measure supplies a red filter to CALCULATE. For this simple same-column context, that filter replaces the existing color filter unless another constraint prevents the red product from being visible. Under the stated blue selection and simple model, the expected value is the red total of 10.
Red Sales = CALCULATE([Total Sales], Product[Color] = "Red")
The CALCULATE filter is evaluated for the Product Color column in the stated model.
Intersect with the existing filter
KEEPFILTERS changes the way the new filter combines with the existing filter on the same column. In the fixture, blue intersected with red leaves no matching product. The result is blank when [Total Sales] has no qualifying rows; confirm the base measureโs blank behavior rather than assuming a zero.
Red Sales Kept = CALCULATE([Total Sales], KEEPFILTERS(Product[Color] = "Red"))
The red condition is retained together with the existing blue color selection, leaving no matching color.
Test compatible and conflicting selections
Compare both measures with no color selection, a red selection, and a blue selection. Inspect the visible Product rows and the base measure before changing the formula. Add other filters only after the simple fixture agrees with the expected results, because relationship paths and filters on other columns can affect the outcome.
Document the intended filter contract
State which filters a measure is allowed to replace and which must remain. Validate subtotals and grand totals separately, and use representative report interactions rather than a single card. Keep row-level security and tenant permissions outside this formula-level example; test those boundaries with appropriate identities in the real model.
Common errors and fixes
A red measure returns a value under a blue selection unexpectedly.
- Why it happens
- CALCULATE replaced the existing filter on the same column.
- How to fix it
- Use the intended filter-combination rule and test the context.
- Prevent it next time
- Document replacement versus intersection before authoring.
An empty intersection is shown as a confirmed zero.
- Why it happens
- Blank behavior was assumed rather than checked.
- How to fix it
- Inspect the base measure and define how blanks are displayed.
- Prevent it next time
- Test empty contexts explicitly.
The fixture behaves differently in the full report.
- Why it happens
- Other filters or relationship paths affect the context.
- How to fix it
- Trace the complete filter path and compare with the simple model.
- Prevent it next time
- Test representative visual and security contexts.
2 practice exercises
1. In the fixture, what does the intersecting measure return under a blue color selection?
Show worked solution
Red and blue cannot both match the same Color value, so no product remains. The stated base SUM measure returns blank for the empty eligible set.
2. Why might a full report differ from the simple red-versus-blue fixture?
Show worked solution
Filters on other columns, relationship paths, visual interactions, or security can constrain the context. Inspect those separately before attributing the result to KEEPFILTERS.
Key takeaways
- CALCULATE can replace a filter on the same column.
- KEEPFILTERS can intersect the new and existing filters.
- Test empty and total contexts explicitly.