Sub topic 10
Common pitfalls and performance considerations
This chapter assumes you can write measures with CALCULATE, and that you know the difference between filter context and row context. It also assumes you have used FILTER, ALL, ALLEXCEPT, and REMOVEFILTERS in the patterns from earlier chapters. Here we collect the mistakes that appear most often with these functions, explain why each one happens, and show how to keep the measures fast.
The examples use the same model as the rest of the tutorial: a Sales fact table related to a 'Product' dimension, with the base measure [Total Sales] = SUM ( Sales[Amount] ).
Pitfall 1: Using REMOVEFILTERS where a table is expected
REMOVEFILTERS is a CALCULATE modifier. It changes the filter context and returns no table. Any place that needs a table value, such as the argument of COUNTROWS, the first argument of FILTER, or an iterator, will reject it.
This version fails:
Category Count Wrong =
COUNTROWS ( REMOVEFILTERS ( 'Product'[Category] ) )ALL does return a table, so it works here:
Category Count = COUNTROWS ( ALL ( 'Product'[Category] ) )The rule is that ALL can serve both roles. In a CALCULATE filter argument it acts as a modifier. As an ordinary expression it returns a table. REMOVEFILTERS only does the first job.
Inside CALCULATE, the two measures below give the same result:
All Sales Using ALL =
CALCULATE ( [Total Sales], ALL ( 'Product' ) )
All Sales Using REMOVEFILTERS =
CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product' ) )Prefer REMOVEFILTERS when you only want to clear filters. A reader sees the intent from the name. Use ALL when you need the table of values.
Pitfall 2: Removing filters from the wrong table
Filters in a star schema start on dimension tables and travel to the fact table through relationships. If a visual is grouped by 'Product'[Category], the filter lives on the Product table. It reaches Sales by propagation.
Many people therefore write the denominator of a percent-of-total like this:
Sales % Wrong =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], REMOVEFILTERS ( Sales ) )
)REMOVEFILTERS ( Sales ) clears filters on the columns of Sales itself. It does not clear the filter on Product[Category], and that filter still reaches Sales through the relationship. The denominator stays filtered, and each row shows roughly 100%.
Clear the table that the visual actually filters:
Sales % of All Products =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product' ) )
)The same applies in reverse. Clearing filters on Product does not touch a filter coming from a Date table. If a slicer on year is active, the denominator still respects the year. This is usually what you want, and it explains why "percent of all products in the selected year" works without extra code.
Pitfall 3: Expecting ALLEXCEPT to keep more than it does
ALLEXCEPT removes every filter on a table except the filters on the columns you list. Three details cause confusion.
- The listed columns must belong to the table in the first argument.
ALLEXCEPT ( 'Product', Sales[Quantity] )is an error. - It keeps only filters on the listed columns. If the visual groups by
'Product'[Category]and'Product'[Color], and you list only Category, the Color filter is cleared. - It acts on one table. Filters on other tables are untouched.
Sales % of Category =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], ALLEXCEPT ( 'Product', 'Product'[Category] ) )
)If the report groups by Category and then Subcategory, each row's denominator is its category total. If a row has no Category filter at all, such as the grand total, there is nothing to keep, and the denominator is the all-product total.
A related trap is hierarchies. If the visual shows Category and Subcategory, and you want the share inside the subcategory's parent, listing Category is correct. Listing Subcategory gives 100% on every row, because the denominator keeps the same filter as the numerator.
Pitfall 4: Filtering a whole table when a column is enough
Compare these two measures:
Red Sales Slow =
CALCULATE (
[Total Sales],
FILTER ( ALL ( 'Product' ), 'Product'[Color] = "Red" )
)
Red Sales =
CALCULATE ( [Total Sales], 'Product'[Color] = "Red" )Both return the same number when no other filter on Product changes the result, but they work differently. The boolean form is shorthand for filtering only the Color column. The engine builds a small list of colors and applies it. The first form builds the whole Product table, with every column, and tests each row. On a large dimension that costs more memory and time.
It also changes the result. FILTER ( ALL ( 'Product' ), ... ) removes every existing filter on Product, while the boolean form replaces only the filter on Color. If a slicer selects Category = Audio, the boolean version returns red audio products. The ALL ( 'Product' ) version returns all red products.
Even more costly is FILTER over the fact table:
Red Sales Very Slow =
CALCULATE (
[Total Sales],
FILTER ( Sales, RELATED ( 'Product'[Color] ) = "Red" )
)This iterates every row of Sales in the current filter context. A fact table can hold millions of rows, so this is the pattern to avoid first.
Use these guidelines:
- Use a boolean filter argument when the condition involves one column and constants.
- Use FILTER when the condition compares several columns, or involves a measure.
- Give FILTER the smallest table that holds the columns it needs. A column or a dimension is better than a fact table.
Pitfall 5: Boolean filters replace, and FILTER over a table intersects
The two forms of filter argument interact with existing filters differently:
| Filter argument | Effect on an existing filter on the same column |
|---|---|
| `'Product'[Color] = "Red"` | Replaces it |
| `FILTER ( Sales, ... )` | Keeps it, then narrows further |
| `FILTER ( ALL ( 'Product' ), ... )` | Removes it, then applies the condition |
| `KEEPFILTERS ( 'Product'[Color] = "Red" )` | Intersects with it |
Suppose a slicer selects Blue. Red Sales still returns red sales, because the boolean argument replaces the slicer's choice. Many users expect an empty result, because Blue and Red do not overlap. If you want the intersection, you need KEEPFILTERS, which the last chapter introduces.
Pitfall 6: Argument order and evaluation order
CALCULATE does not evaluate its filter arguments left to right as written. It applies modifiers such as REMOVEFILTERS and ALL first, then the other filters. So these two measures return the same value:
Red Sales All Products A =
CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product' ), 'Product'[Color] = "Red" )
Red Sales All Products B =
CALCULATE ( [Total Sales], 'Product'[Color] = "Red", REMOVEFILTERS ( 'Product' ) )In both, REMOVEFILTERS runs first and clears every filter on Product. The Color condition is then applied to the cleared state. The result is sales of red products across all categories. Writing the arguments in a different order does not change the result. The removal does not undo the Color condition.
Pitfall 7: Percent-of-total errors at the total row and with zero
Two behaviours surprise people in finished reports:
- On the grand total row, a percent-of-total measure returns 100%. The numerator and denominator are the same. This is correct, not a bug.
- If the denominator is zero or blank, a plain
/raises an error or returns infinity. Use DIVIDE, which returns blank by default, or pass a third argument as an alternate result.
Another common mistake is to use a percentage measure inside a visual that is grouped by a column from a different table than the one you cleared. If the visual groups by Date[Year] but your denominator clears only 'Product', each year's share will be 100%. Match the table you clear to the axis you want to measure against.
Performance considerations
DAX measures run at query time, once for every cell of a visual. Small costs multiply. These habits keep the measures fast.
Store repeated values in variables
Without variables, the engine may compute the same sub-expression more than once. A variable computes the value one time:
Sales % of All Products Var =
VAR CurrentSales = [Total Sales]
VAR AllSales =
CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product' ) )
RETURN
DIVIDE ( CurrentSales, AllSales )This also makes the measure easier to read and to debug, because you can return either variable by itself during testing.
Avoid iterating a large table
The measure Large Line Sales from earlier tests a product of two columns on each row of Sales:
Large Line Sales =
CALCULATE (
[Total Sales],
FILTER ( Sales, Sales[Quantity] * Sales[Net Price] > 1000 )
)That is fine for a moderate table, and sometimes it is the only option. If the report is slow and the condition does not depend on user selections, move the arithmetic into a calculated column or into the source query. The measure then becomes a boolean filter:
Large Line Sales Column =
CALCULATE ( [Total Sales], Sales[Line Amount] > 1000 )Here Sales[Line Amount] is a calculated column defined as Sales[Quantity] * Sales[Net Price]. The trade-off is model size and refresh time, because the column is stored. Choose it when the condition is fixed and the measure is used widely.
Measure before you tune
Do not guess. Use Performance Analyzer in Power BI Desktop to find the slowest visuals. Then copy the query into DAX Studio and compare the server timings of two versions of a measure. A rewrite that looks cleaner is not always faster, and the engine may already optimise a pattern you assumed was costly.
Keep filters on columns, not on tables
REMOVEFILTERS ( 'Product' ) clears the whole table, while REMOVEFILTERS ( 'Product'[Color] ) clears one column. Pick the narrowest one that meets the requirement. This is a correctness decision more than a speed one. Clearing the whole table also removes a Category slicer that the user expected to keep.
A debugging checklist
Work through the questions in this order. Most wrong numbers come from clearing the wrong table, and most errors come from using REMOVEFILTERS as a table.
Summary of the pitfalls
| Symptom | Likely cause | Fix |
|---|---|---|
| Error about a table expression | REMOVEFILTERS used where a table is needed | Use ALL |
| Every row shows about 100% | Cleared the fact table, or the wrong dimension | Clear the table the visual filters |
| Denominator loses a needed grouping | ALLEXCEPT omits the column | List the column to keep |
| Result ignores a slicer | Boolean filter replaced the slicer's filter | Use KEEPFILTERS to intersect |
| Slow measure | FILTER over a large fact table | Filter a column, or precompute |
| Error on division | Zero denominator | Use DIVIDE |
Exercise
A report groups by 'Product'[Category]. A colleague wrote Sales % = DIVIDE ( [Total Sales], CALCULATE ( [Total Sales], REMOVEFILTERS ( Sales ) ) ) and every category shows close to 100%. Explain the cause and write the corrected measure.
Show answer
The filter comes from the Product table and reaches Sales through the relationship. REMOVEFILTERS ( Sales ) clears only filters on Sales columns, so the Category filter still applies to the denominator. Clear the dimension instead: Sales % = DIVIDE ( [Total Sales], CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product' ) ) ).
Exercise
Rewrite Red Sales Slow = CALCULATE ( [Total Sales], FILTER ( ALL ( 'Product' ), 'Product'[Color] = "Red" ) ) in the cheaper form. Then say how the result differs when a slicer selects Category = Audio.
Show answer
Red Sales = CALCULATE ( [Total Sales], 'Product'[Color] = "Red" ). The boolean form replaces only the filter on Color, so with the Audio slicer it returns red audio products. The original form with ALL ( 'Product' ) clears the Category filter as well, so it returns red sales across all categories.
Exercise
Why does COUNTROWS ( REMOVEFILTERS ( 'Product'[Category] ) ) fail, and what is the correct way to count all categories?
Show answer
REMOVEFILTERS is a CALCULATE modifier and does not return a table, so COUNTROWS cannot accept it. Use COUNTROWS ( ALL ( 'Product'[Category] ) ), because ALL returns the table of distinct categories.