Fatskills
Practice. Master. Repeat.
Study Guide: TECH **Power BI Advanced CALCULATE: KEEPFILTERS, CROSSFILTER, ALLEXCEPT**
Source: https://www.fatskills.com/data-science/chapter/tech-power-bi-advanced-calculate-keepfilters-crossfilter-allexcept

TECH **Power BI Advanced CALCULATE: KEEPFILTERS, CROSSFILTER, ALLEXCEPT**

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~6 min read

Power BI Advanced CALCULATE: KEEPFILTERS, CROSSFILTER, ALLEXCEPT

A Hyper-Practical, Zero-Fluff Study Guide


1. What This Is & Why It Matters

What it is:
CALCULATE is Power BI’s most powerful DAX function—it lets you modify filter context on the fly. But its default behavior (overwriting filters) often causes unexpected results. KEEPFILTERS, CROSSFILTER, and ALLEXCEPT are filter modifiers that give you surgical control over how CALCULATE interacts with existing filters.

Why it matters in production:
- Broken reports: Without these modifiers, your measures might return wrong totals (e.g., showing global sales instead of filtered sales).
- Performance nightmares: Misusing CALCULATE can force Power BI to scan entire tables, killing performance.
- Real-world scenario: You’re building a sales dashboard. A user filters by Region = "West", but your "Total Sales" measure ignores the filter and shows all regions. Or worse—your "YoY Growth" measure breaks because it’s comparing filtered vs. unfiltered data.

Superpower: These modifiers let you preserve, override, or selectively clear filters—critical for accurate comparisons, dynamic aggregations, and complex business logic.


2. Core Concepts & Components


1. CALCULATE (Base Function)

  • Definition: Evaluates an expression in a modified filter context.
  • Production insight: CALCULATE is the only way to override filters in DAX. Misuse leads to incorrect totals or slow queries.

2. KEEPFILTERS

  • Definition: Preserves existing filters while adding new ones (instead of overwriting).
  • Production insight: Without KEEPFILTERS, CALCULATE replaces filters, which can break user selections (e.g., slicers).

3. CROSSFILTER

  • Definition: Temporarily changes the relationship behavior between tables (e.g., disable, one-way, both-way).
  • Production insight: Default relationships (e.g., one-way) can cause measures to return BLANK() if filters flow the wrong way.

4. ALLEXCEPT

  • Definition: Clears all filters on a table except those specified.
  • Production insight: Overusing ALL (instead of ALLEXCEPT) can accidentally clear too many filters, breaking report logic.

5. Filter Context vs. Row Context

  • Definition:
  • Filter context: Applied by slicers, filters, or CALCULATE.
  • Row context: Applied by iterators (e.g., SUMX, FILTER).
  • Production insight: Confusing these leads to measures that return BLANK() or wrong totals.

6. USERELATIONSHIP (Bonus)

  • Definition: Activates an inactive relationship for a CALCULATE call.
  • Production insight: Critical for date tables with multiple relationships (e.g., "Order Date" vs. "Ship Date").


3. Step-by-Step Hands-On: Build a Real-World Measure


Prerequisites

  • Power BI Desktop installed.
  • A dataset with:
  • A Sales table (columns: SaleID, ProductID, Region, Amount).
  • A Products table (columns: ProductID, Category).
  • A relationship: Sales[ProductID] → Products[ProductID] (one-to-many).

Task: Create a "Sales Excluding Current Category" Measure

Goal: Show total sales excluding the currently filtered product category (e.g., if user filters "Electronics," show sales for all other categories).


Step 1: Create a Basic Total Sales Measure

Total Sales = SUM(Sales[Amount])

Step 2: Add CALCULATE Without Modifiers (Broken Version)

Sales Excluding Category (Broken) =
CALCULATE(
[Total Sales],
ALL(Products[Category]) // Clears ALL category filters )

Problem: This always shows global sales, ignoring all user filters (e.g., region, date).


Step 3: Fix with KEEPFILTERS

Sales Excluding Category =
CALCULATE(
[Total Sales],
KEEPFILTERS(ALL(Products[Category])) // Preserves other filters )

How it works:
- ALL(Products[Category]) clears only category filters.
- KEEPFILTERS ensures other filters (e.g., region) are preserved.


Step 4: Verify with a Matrix Visual

  1. Add a matrix visual:
  2. Rows: Products[Category]
  3. Values: [Total Sales] and [Sales Excluding Category]
  4. Expected output:
  5. If "Electronics" = $100K, [Sales Excluding Category] should show total sales - $100K.
  6. If you filter by Region = "West", the measure should only exclude "Electronics" in the West.

Step 5: Advanced Use Case – CROSSFILTER

Scenario: You need a measure that shows sales for products not in the current category, but only for products with >$1K in sales.


Sales Excluding Category (Filtered) =
CALCULATE(
[Total Sales],
KEEPFILTERS(ALL(Products[Category])),
CROSSFILTER(Sales[ProductID], Products[ProductID], NONE), // Disable relationship
FILTER(
ALL(Products),
[Total Sales] > 1000 // Only include products with >$1K sales
) )

Why CROSSFILTER?
- Disables the relationship to avoid circular dependencies.
- Lets FILTER work on the Products table without affecting Sales.


Step 6: ALLEXCEPT for Date Tables

Scenario: You need a "Sales Last Year" measure that ignores all filters except the date.


Sales LY =
CALCULATE(
[Total Sales],
ALLEXCEPT('Date', 'Date'[Year]), // Preserve only year filter
DATEADD('Date'[Date], -1, YEAR) // Shift date back 1 year )

Key insight:
- ALLEXCEPT is safer than ALL because it preserves critical filters (e.g., year).
- Without it, DATEADD might return BLANK() if other filters conflict.


4. ? Production-Ready Best Practices


Performance

  • Avoid ALL on large tables: Use ALLEXCEPT or REMOVEFILTERS instead.
  • Prefer KEEPFILTERS over nested CALCULATE: Reduces context transitions.
  • Test with DAX Studio: Check query plans to spot inefficient filters.

Maintainability

  • Name measures clearly: E.g., Sales Excluding Category (KEEPFILTERS).
  • Document filter logic: Add comments in DAX: dax // Preserves region/date filters but clears category KEEPFILTERS(ALL(Products[Category]))
  • Use variables (VAR): Improves readability and performance: dax Sales Excluding Category = VAR CurrentCategory = SELECTEDVALUE(Products[Category]) RETURN CALCULATE(
    [Total Sales],
    KEEPFILTERS(ALL(Products[Category])) )

Reliability

  • Test edge cases: What if no category is selected? What if multiple categories are selected?
  • Use ISBLANK checks: Avoid BLANK() results: dax Sales LY (Safe) = IF(
    ISBLANK([Total Sales]),
    BLANK(),
    CALCULATE([Total Sales], DATEADD('Date'[Date], -1, YEAR)) )


5. ⚠️ Common Mistakes & Traps

Mistake Symptom Fix/Prevention
Using ALL instead of ALLEXCEPT Measure ignores all filters (e.g., date). Use ALLEXCEPT to preserve critical filters (e.g., ALLEXCEPT('Date', 'Date'[Year])).
Forgetting KEEPFILTERS Measure overwrites user selections. Wrap filters in KEEPFILTERS if you want to add to existing filters.
Misusing CROSSFILTER Measure returns BLANK() unexpectedly. Disable relationships only when necessary (e.g., for FILTER on the same table).
Confusing ALL with REMOVEFILTERS ALL clears all filters; REMOVEFILTERS clears specific filters. Use REMOVEFILTERS for granular control (e.g., REMOVEFILTERS(Products[Category])).
Overusing CALCULATE Slow performance due to context transitions. Minimize nested CALCULATE calls; use variables (VAR) instead.


6. ? Exam/Certification Focus (PL-300)


Typical Question Patterns

  1. "Which modifier preserves existing filters?"
  2. ALL (clears filters)
  3. KEEPFILTERS (preserves filters)

  4. "How do you ignore all filters except date?"

  5. ALL('Date') (clears all date filters)
  6. ALLEXCEPT('Date', 'Date'[Year]) (preserves year filter)

  7. "Why does this measure return BLANK()?"
    dax
    Sales LY =
    CALCULATE(
    [Total Sales],
    DATEADD('Date'[Date], -1, YEAR)
    )

  8. Answer: Conflicting filters (e.g., user filtered for "2023" but DATEADD tries to shift to "2022"). Fix with ALLEXCEPT.

Key Trap Distinctions

Concept Trap Exam Insight
KEEPFILTERS Thinks it adds filters (it preserves existing ones). KEEPFILTERS + ALL = "clear this filter but keep others."
CROSSFILTER Assumes it permanently changes relationships (it’s temporary). Only affects the current CALCULATE call.
ALLEXCEPT Forgets it preserves the specified columns (not clears them). ALLEXCEPT(Table, Column) = "clear all filters except Column."


7. ? Hands-On Challenge (With Solution)


Challenge

Scenario: You have a Sales table with Region and Product columns. Create a measure that shows: - Total sales for the current region (e.g., "West").
- Total sales for all other regions (e.g., "East + North + South").

Requirements:
- Must work with slicers (e.g., if user filters by Product = "Laptop", the measure should respect that).
- Must not use hardcoded region names.

Solution

Sales Other Regions =
VAR CurrentRegion = SELECTEDVALUE(Sales[Region])
RETURN
CALCULATE(
[Total Sales],
KEEPFILTERS(ALL(Sales[Region])), // Clear region filter but keep others
Sales[Region] <> CurrentRegion // Filter for other regions )

Why it works:
- KEEPFILTERS preserves product/date filters.
- ALL(Sales[Region]) clears the region filter temporarily.
- Sales[Region] <> CurrentRegion filters for other regions.


8. ? Rapid-Reference Crib Sheet

Modifier Syntax Use Case Exam Trap ⚠️
KEEPFILTERS KEEPFILTERS(<filter>) Preserve existing filters while adding new ones. Doesn’t add filters—just preserves them.
CROSSFILTER CROSSFILTER(Table1[Col], Table2[Col], NONE) Disable a relationship for a CALCULATE call. Only affects the current CALCULATE scope.
ALLEXCEPT ALLEXCEPT(Table, Column1, Column2) Clear all filters except specified columns. Preserves the columns you list (not clears them).
REMOVEFILTERS REMOVEFILTERS(Table[Column]) Clear specific filters (modern alternative to ALL). Doesn’t work with KEEPFILTERS.
USERELATIONSHIP USERELATIONSHIP(Table1[Col], Table2[Col]) Activate an inactive relationship. Only works in CALCULATE.


9. ? Where to Go Next

  1. Microsoft Docs: CALCULATE Function
  2. SQLBI: Understanding KEEPFILTERS
  3. DAX Guide: CROSSFILTER
  4. Book: The Definitive Guide to DAX (Marco Russo & Alberto Ferrari) – Chapter 5 (Filter Context).


ADVERTISEMENT