OpenBI Docs Buy

KEEPFILTERS

As a filter of CALCULATE, or around the table an iterator goes through, adds to the filters already in place instead of replacing them.

Group: Filter. Compatible with DAX.

Syntax

KEEPFILTERS ( <expression> )
ParameterTypeDescription
expressionFilterA filter of CALCULATE, CALCULATETABLE or SUMMARIZECOLUMNS, a condition or a table; or the table an iterator such as SUMX goes through.

Returns

Filter. The same filter, set to narrow the filters already in place instead of replacing them.

Remarks

  • Without it, a condition in CALCULATE replaces the filter on its column. With it, both apply, so only the values that pass both are left.
  • In a visual by category, CALCULATE ( [Liters sold], KEEPFILTERS ( Products[Category] = "Beer" ) ) is blank in the Cider row, where without KEEPFILTERS it shows the beer.
  • Filters on other columns are combined in any case: KEEPFILTERS matters only where its filter and one already in place share a column.
  • It works around conditions and around table filters such as TREATAS.
  • Around the table of an iterator (SUMX, MAXX, ADDCOLUMNS, FILTER, FIRSTNONBLANK and the like), the filters in place stay while the rows are gone through: each row's filter narrows them instead of replacing them (example 2).
  • Anywhere else it is an error, as in Power BI: in a variable, as the result of a query, inside COUNTROWS, IF or a join, or as CALCULATETABLE's first argument (example 3).

Examples

Two either-or filters on one column

Pils and IPA are selected. Without KEEPFILTERS the condition replaces them with Pils and Cider; with it, only Pils is in both.

EVALUATE
CALCULATETABLE (
    ROW (
        "Replaced", CALCULATE ( [Liters sold], Products[Product] = "Pils" || Products[Product] = "Cider" ),
        "Kept", CALCULATE ( [Liters sold], KEEPFILTERS ( Products[Product] = "Pils" || Products[Product] = "Cider" ) )
    ),
    Products[Product] = "Pils" || Products[Product] = "IPA"
)
ReplacedKept
13054001004326

Around an iterator's table

Pils and IPA are selected. SUMX over ALL goes through every product, each replacing the selection; over KEEPFILTERS ( ALL ) only the selected ones count, as the plain measure does.

EVALUATE
CALCULATETABLE (
    ROW (
        "Plain", [Liters sold],
        "SUMX over ALL", SUMX ( ALL ( Products[Product] ), [Liters sold] ),
        "SUMX over KEEPFILTERS ( ALL )", SUMX ( KEEPFILTERS ( ALL ( Products[Product] ) ), [Liters sold] )
    ),
    Products[Product] = "Pils" || Products[Product] = "IPA"
)
PlainSUMX over ALLSUMX over KEEPFILTERS ( ALL )
133621018064631336210

Inside COUNTROWS

KEEPFILTERS gives no rows to count.

EVALUATE
ROW ( "Products", COUNTROWS ( KEEPFILTERS ( ALL ( Products[Product] ) ) ) )

Result: an error.

Count ALL ( Products[Product] ) itself; KEEPFILTERS has a meaning only where filters meet.

CALCULATE, CALCULATETABLE, FILTER, TREATAS, FILTERS.

Last updated · OpenBI Studio 0.3.74