Code action RW002
(Rewrites) Rewrite using FILTER
Description
A scalar predicate in a filter argument to CALCULATE
is equivalent to a 1-column table expression that uses FILTER
.
Example 1
Change:
CALCULATE([Total Sales], Products[Color] = "Red")
To:
CALCULATE(
[Total Sales],
FILTER(
ALL(Products[Color]),
Products[Color] = "Red")
)
)
Example 2
Change:
CALCULATE([Total Sales], KEEPFILTERS(Products[Color] = "Red"))
To:
CALCULATE(
[Total Sales],
FILTER(
VALUES(Products[Color]),
Products[Color] = "Red")
)
)
Why is Tabular Editor suggesting this?
This rewrite is useful in case you want to add more complex filtering logic.
Note
This code action is in the Rewrites category, which means that it does not represent a general recommendation or best practice. Instead, the code action provides a quick way to rewrite the code in a different way, for example as part of a larger refactoring. After applying the code action and until further edits are made, you may see an Improvement or Readability code action that suggests to modify the code back to its original state.