Summary of Power BI CALCULATE with REMOVEFILTERS
The Power BI CALCULATE function combined with `REMOVEFILTERS` is a powerful feature in Power BI. It allows users to create measures that ignore specific filters while still responding to the overall report context. This functionality is particularly useful for calculating percentages, totals, benchmarks, and comparison metrics. The fundamental concept is straightforward: start with a measure and then remove the filters that should not influence it.
Problem Definition in Power BI
In many reports, the default filter behavior may not align with the analyst’s requirements. Common questions include:
- What is the total sales for each customer, even when the report is sliced by product?
- What is the grand total for the current selection, even when the visual is grouped by region?
- What is the full denominator for a percentage measure when the current row has already been filtered down to a category?
These scenarios highlight filter context challenges. While the report may display a specific slice of data, the business question often necessitates a value calculated without certain filters. This is where `CALCULATE` with `REMOVEFILTERS` becomes essential.
Key Concept of Power BI CALCULATE and REMOVEFILTERS
In Power BI, a measure is typically evaluated within the current filter context. This means that the result can vary based on slicers, page filters, visual filters, and the fields present in the visual. The `REMOVEFILTERS` function alters this behavior by clearing selected filters before the expression is evaluated.
The analytical goal is to calculate a metric while ignoring specific filters, while still retaining the rest of the report context. This is particularly beneficial when a stable denominator, baseline total, or a value that should not be segmented by the visual grain is required.
Data Example for Understanding
Consider the following simple sales table:
| OrderID | Customer | Product | Region | Sales |
|---|---|---|---|---|
| 1 | A | Bike | West | 100 |
| 2 | A | Helmet | West | 50 |
| 3 | B | Bike | East | 200 |
| 4 | B | Helmet | East | 75 |
A basic sales measure can be defined as:
Sales = SUM(Sales[Sales])
If the visual is grouped by Product, the result will be:
| Product | Sales |
|---|---|
| Bike | 300 |
| Helmet | 125 |
Now, suppose we want to calculate total sales for the entire dataset, regardless of the Product filter. We still want the current report to respect other filters, but the Product filter should not affect the total.
Tool Implementation of CALCULATE and REMOVEFILTERS
Functionality of CALCULATE and REMOVEFILTERS
In Power BI, the `CALCULATE` function evaluates an expression under a modified filter context. The `REMOVEFILTERS` function clears filters from a specified table or column.
The syntax is as follows:
CALCULATE(<expression>, REMOVEFILTERS(<table or column>))
For example:
CALCULATE(SUM(Sales[Sales]), REMOVEFILTERS(Sales[Product]))
This means:
- Calculate total sales.
- Ignore any filter on `Sales[Product]`.
- Retain other filters unless they are also removed.
If you remove filters from the entire table, the measure will ignore every filter on that table:
CALCULATE(SUM(Sales[Sales]), REMOVEFILTERS(Sales))
This broader approach removes all filters from the `Sales` table.
Applied Example of Total Sales Ignoring Product
To create a measure for total sales while ignoring the Product filter, you can use the following formula:
Total Sales All Products = CALCULATE(SUM(Sales[Sales]), REMOVEFILTERS(Sales[Product]))
If the visual displays Product, the results will be:
| Product | Sales | Total Sales All Products |
|---|---|---|
| Bike | 300 | 425 |
| Helmet | 125 | 425 |
The sales column varies by product, while the `Total Sales All Products` measure remains constant at 425 because the Product filter is removed before evaluation. This is the primary use case for this functionality.
Importance of CALCULATE and REMOVEFILTERS
Without the `CALCULATE` function, a measure is evaluated within the current filter context. If the visual includes Product, the calculation will be influenced by Product. This is typically appropriate for additive measures but not when a total that ignores row grouping is required.
For instance, the measure:
SUM(Sales[Sales])
will yield sales per Product when Product is included in the visual. The combination of `CALCULATE` and `REMOVEFILTERS` overrides this behavior by modifying the active filters during evaluation. This is why it is frequently used for percent of total measures.
Important Behavioral Details of REMOVEFILTERS
The `REMOVEFILTERS` function is often confused with the `ALL` function. While they are related, they serve different purposes. `REMOVEFILTERS` is the clearer choice when the goal is to clear filter context. In contrast, `ALL` is often used in older patterns and can also be applied in denominator calculations, but it does not convey the same semantic intent.
Another crucial aspect is filter scope. If you remove filters from a column, other columns in the same table can still filter the result. However, if you remove filters from the entire table, all filters from that table will be cleared. This distinction is significant in models with multiple dimension tables and relationships.
Additionally, the order of operations is important. `CALCULATE` first applies the filter modifications and then evaluates the expression. Therefore, the measure result depends on the filters that remain after removal. This is particularly relevant in measures that must respect slicers from one dimension while ignoring another.
For example:
CALCULATE(SUM(Sales[Sales]), REMOVEFILTERS(Sales[Product]))
This will ignore the Product filter but still respect the Region filter if it originates from a different source.
Real Usage Patterns for Power BI CALCULATE and REMOVEFILTERS
Common use cases for `CALCULATE` and `REMOVEFILTERS` include:
- Percent of total calculations
- Benchmark measures
- Grand totals in matrix visuals
- Share of category or region
- Stable denominators in ratios
- Ranking against the full selection
- Comparison to all products, customers, or months
Typical examples include:
Percent of Total = DIVIDE([Sales], CALCULATE([Sales], REMOVEFILTERS(Sales[Product])))
Sales vs All Products = [Sales] - CALCULATE([Sales], REMOVEFILTERS(Sales[Product]))
Category Share = DIVIDE([Category Sales], CALCULATE([Category Sales], REMOVEFILTERS(Sales[Category])))
These patterns are prevalent because business questions often require a reference value that remains constant, regardless of the row being displayed.
Conclusion
The `REMOVEFILTERS` function does not create a new aggregation type; it merely alters the filter context. The expression within `CALCULATE` continues to utilize the standard aggregation logic of the measure. For most reporting scenarios, using `CALCULATE` with `REMOVEFILTERS` is the most effective way to evaluate a measure as if a specific filter were not applied.