Timedora

DAX Measures and Filter Context

Learn how to build reusable DAX measures, control filter context with CALCULATE, apply date-based calculations, and check measure results against source totals.

Build measures and shape filter context

Build a reusable measure

A measure is evaluated in the filter context of the visual cell where it appears. That context can include filters from the visual, slicers, relationships, and DAX. As a result, one measure can show a subtotal for each category row and a grand total in another cell.

Start with a base measure that summarizes the sales fact table:

Sales Amount =
SUM ( Sales[SalesAmount] )

In a typical model, dimension tables such as Product and Date provide fields for filtering and grouping, while the Sales fact table provides values to summarize. If a visual groups by Product[Category], the category on each row filters [Sales Amount]; a date slicer can narrow it further through the model relationships.

CALCULATE lets you evaluate an expression after modifying its current filter context. For example, this measure asks for sales of blue products:

Blue Sales =
CALCULATE (
    [Sales Amount],
    Product[Color] = "Blue"
)

The measure first applies the color condition, then evaluates [Sales Amount] in that modified context. By default, this condition replaces an existing filter on Product[Color], or adds a color filter if none was present. Filters on other columns—such as date or region—remain in effect.

Replace or intersect a filter

Use KEEPFILTERS when the condition should intersect with an existing filter on the same column rather than replace it:

Blue Sales Within Selection =
CALCULATE (
    [Sales Amount],
    KEEPFILTERS ( Product[Color] = "Blue" )
)

Suppose a report selection already limits colors to red and blue. The ordinary filter in [Blue Sales] replaces that color selection with blue. The KEEPFILTERS version intersects the selection with blue, so it returns blue sales within the selection. In both cases, other applicable context, such as date and region, remains in force.

A Boolean filter argument is useful for a straightforward condition on a column. When the condition calls for more complex row-by-row evaluation, use a table expression such as FILTER:

High-Value Sales =
CALCULATE (
    [Sales Amount],
    FILTER (
        Sales,
        Sales[SalesAmount] > 1000
    )
)

Here, FILTER evaluates the condition for rows of Sales and provides the matching rows as a table expression for CALCULATE. Prefer a simple Boolean filter when it clearly expresses the requirement; use FILTER when the logic needs that table expression or more complex row evaluation.

Control comparison denominators

Control comparison denominators

A comparison such as “sales as a share of all categories” needs a denominator that ignores the current category filter. REMOVEFILTERS can clear the specified filter inside CALCULATE while leaving other report context in place:

Sales % of Categories =
DIVIDE (
    [Sales Amount],
    CALCULATE (
        [Sales Amount],
        REMOVEFILTERS ( Product[Category] )
    )
)

In a category row, the numerator remains filtered to that category. The denominator removes the category filter, so it can represent sales across categories while retaining date, region, and other applicable filters. The resulting division compares category sales with that broader total under the rest of the current context.

The scope of removal matters. REMOVEFILTERS(Product[Category]) removes the category filter specifically. Use REMOVEFILTERS(Product) only if the denominator should ignore filters on the entire Product table. Clearing more filters than intended can produce a misleading comparison because the denominator may no longer match the intended context.

REMOVEFILTERS is a filter modifier used inside CALCULATE; it does not return a table.

Prepare dates for time intelligence

Prepare dates for time intelligence

Time-intelligence calculations depend on a dependable date dimension. For consistent calendar filtering, it should contain one unique, nonblank row for every date, have no gaps, and span complete years. For classic time intelligence, mark it as the model’s date table.

A date range covering complete years can be created from the first and last sales years:

Date =
CALENDAR (
    DATE ( YEAR ( MIN ( Sales[OrderDate] ) ), 1, 1 ),
    DATE ( YEAR ( MAX ( Sales[OrderDate] ) ), 12, 31 )
)

Relate Date[Date] to the date-only column used by the sales fact table. Add year, quarter, and month attributes to the date dimension for reporting, and use its date column in time-intelligence expressions. The relationship lets date filters affect the sales rows being summarized.

For example, a year-to-date measure can be written as:

Sales YTD =
CALCULATE (
    [Sales Amount],
    DATESYTD ( 'Date'[Date] )
)

DATESYTD supplies the year-to-date dates for the current context. CALCULATE then evaluates the reusable [Sales Amount] measure over those dates. In other words, the date expression establishes which dates to use, while the base measure continues to define what is being summarized.

Reconcile measures to source totals

Reconcile measures to source totals

Before relying on a measure, compare it with the corresponding source total at the same grain—the level of detail being summarized—and under matching conditions. Check that the date range and date role match, currencies and units agree, and filters, exclusions, returns, and adjustments are handled consistently. Also verify that model relationships and the fact-table grain are correct: a mismatch can change which rows are included in a sum.

A difference measure makes the comparison visible:

Sales Reconciliation Difference =
[Sales Amount] - [Source Sales Amount]

A result of zero means the measures agree in the current context if they use the same definitions. Zero by itself does not establish that the definitions or comparison conditions are correct; first align the dates, grain, units, relationships, and business rules.

If the difference is nonzero, compare the values by date and then by another useful dimension, such as product or region. Seeing where the difference begins can help identify which detail or condition needs investigation.