Sub topic 2
Boolean Filters and Existing Filters in DAX
Assume you know basic DAX measures, calculated columns, and star-schema relationships. You should also know that slicers, rows, and columns create filter context for a measure.
Boolean filters replace filters on the same column
A Boolean filter is a condition such as 'Product'[Color] = "Red". In CALCULATE, it changes the filter context before evaluating the expression. By default, it replaces an existing filter on the same column.
Total Sales = SUM ( Sales[Amount] )
Red Sales =
CALCULATE (
[Total Sales],
'Product'[Color] = "Red"
)If a slicer selects Blue, Red Sales still evaluates sales for Red. The filter on Product[Color] is replaced. Filters on other columns, such as category or year, remain active.
flowchart TD
A[Current filter context] --> B[CALCULATE]
B --> C[Replace Product Color filter]
C --> D[Evaluate Total Sales for Red]caption: How a Boolean filter changes one column's filter alt: A flow from the current filter context through CALCULATE, which replaces the product color filter before evaluating total sales for red.
Removing filters with REMOVEFILTERS, ALL, and ALLEXCEPT
Use REMOVEFILTERS when you want to clear filters. For example, this measure keeps the current numerator but removes the category filter from the denominator.
Sales Share of Category =
DIVIDE (
[Total Sales],
CALCULATE (
[Total Sales],
REMOVEFILTERS ( 'Product'[Category] )
)
)ALL can also remove filters and can return an unfiltered table or column. ALLEXCEPT removes filters from a table except for the columns you name.
Sales Within All Categories =
CALCULATE (
[Total Sales],
ALL ( 'Product'[Category] )
)
Sales Within Selected Year =
CALCULATE (
[Total Sales],
ALLEXCEPT ( 'Date', 'Date'[Year] )
)Use REMOVEFILTERS when your intent is specifically to clear filters. It is usually easier to read than ALL in that role.
Intersecting with existing filters using KEEPFILTERS
KEEPFILTERS changes replacement behavior to intersection. The result must satisfy both the existing filter and the new filter.
Red Sales Within Selection =
CALCULATE (
[Total Sales],
KEEPFILTERS ( 'Product'[Color] = "Red" )
)If the current selection is Red and Blue, the result contains only Red. If the selection excludes Red, the result is blank or zero, depending on the measure and data.
Exercise
A slicer selects Blue. What does Red Sales return, and what does Red Sales Within Selection return?
Show answer
Red Sales returns red sales because its Boolean filter replaces the color filter. Red Sales Within Selection returns no matching rows because KEEPFILTERS intersects Red with Blue.