Sub topic 10
Common pitfalls and performance considerations
This chapter assumes you can write basic DAX measures, understand filter context and row context, and have used CALCULATE with FILTER, ALL, ALLEXCEPT, and REMOVEFILTERS. It covers the mistakes that appear most often with these functions and the habits that keep measures fast.
Pitfall 1: Using REMOVEFILTERS as a table expression
REMOVEFILTERS is a CALCULATE modifier. It does not return a table, so you cannot pass it to a function that expects one, such as COUNTROWS. To count the distinct categories in the model, use ALL as a table function.
Category Count = COUNTROWS ( ALL ( 'Product'[Category] ) )Writing COUNTROWS ( REMOVEFILTERS ( 'Product'[Category] ) ) raises an error. Use REMOVEFILTERS only as a direct argument of CALCULATE or CALCULATETABLE.
Pitfall 2: Filtering a whole table when a column is enough
A filter argument that names one column is cheap. A FILTER over a whole table makes the engine iterate every row and keep the columns it needs for the test.
This measure filters the whole Sales table, although it only tests one column:
Large Quantity Sales Slow =
CALCULATE (
[Total Sales],
FILTER ( Sales, Sales[Quantity] > 10 )
)This version uses a boolean filter argument on a single column:
Large Quantity Sales =
CALCULATE ( [Total Sales], Sales[Quantity] > 10 )Both return the same result here. The second form is shorter, and the engine can turn it into a filter on the one column instead of a scan of the table. Reserve FILTER for conditions that combine several columns or measures, as in the Large Line Sales example from the FILTER chapter.
Pitfall 3: Removing more filters than you meant to
ALL ( 'Product' ) and REMOVEFILTERS ( 'Product' ) clear every filter on every column of the table. A slicer on Product[Color] disappears from the measure, which may not be what the report reader expects. If you only want to ignore the filter on one column, name that column.
Sales All Categories =
CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product'[Category] ) )Here a color slicer still applies, and only the category filter is cleared. When you want the opposite, keeping one column and clearing the rest, use ALLEXCEPT.
Pitfall 4: Using ALL when you only mean to clear filters
Inside CALCULATE, ALL ( 'Product' ) and REMOVEFILTERS ( 'Product' ) have the same effect. The difference is readability.
All Sales Using ALL =
CALCULATE ( [Total Sales], ALL ( 'Product' ) )A reader who sees ALL must work out whether it is a modifier or a table. REMOVEFILTERS states the intent. Choose ALL when you need the table it returns, and REMOVEFILTERS when you only clear filters.
Pitfall 5: Forgetting the denominator can be zero
Percent-of-total measures divide by a value that you change with ALL, ALLEXCEPT, or REMOVEFILTERS. That value can be blank or zero, for example when a slicer selects a period with no sales. Use DIVIDE instead of the / operator so the measure returns a blank instead of an error.
Sales % of All Products =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product' ) )
)Performance considerations
The functions in this tutorial affect speed through the size of the table they work on. Keep these habits in mind:
- Filter columns, not tables. A column filter touches the distinct values of one column. A table filter touches every row.
- Avoid nesting FILTER over large fact tables. FILTER inside FILTER multiplies the work.
- Keep iterator conditions simple. A measure inside a FILTER condition is evaluated once for every row FILTER visits.
- Compute shared values once. Store a repeated denominator in a variable with
VARinstead of writing the same CALCULATE twice.
Sales % of Category Using VAR =
VAR CategorySales =
CALCULATE ( [Total Sales], ALLEXCEPT ( 'Product', 'Product'[Category] ) )
RETURN
DIVIDE ( [Total Sales], CategorySales )A quick checklist
The diagram shows the questions to ask before you pick a form.
Exercise
A colleague writes Category Count = COUNTROWS ( REMOVEFILTERS ( 'Product'[Category] ) ) and gets an error. What is wrong, and how do you fix it?
Show answer
REMOVEFILTERS is a CALCULATE modifier and does not return a table, so COUNTROWS cannot use it. Replace it with ALL, which returns a table: COUNTROWS ( ALL ( 'Product'[Category] ) ).
Exercise
Rewrite CALCULATE ( [Total Sales], FILTER ( Sales, Sales[Quantity] > 10 ) ) in a form that is simpler and avoids iterating the whole table.
Show answer
CALCULATE ( [Total Sales], Sales[Quantity] > 10 ). The condition uses one column, so a boolean filter argument is enough.