Power BI DAX: Implicit vs. Explicit Filtering

Mohamad MahmoodMohamad Mahmood
4 min read

Consider the following two DAX expressions.

First expression:

Headcount =
CALCULATE ( COUNTROWS ( 'Headcount' ), LASTDATE ( 'Date'[Date] ) )

Second expression:

RecentSalesCount Measure RecentSalesCount =
CALCULATE (
    COUNTROWS ( Sales ),
    Sales[OrderDate] = LASTDATE ( Sales[OrderDate] )
)

The second expression includes an Explicit Filter Context in the second argument of the CALCULATE() function.

What is Implicit/Explicit Filter Context?

Implicit vs Explicit Filter Context

Let's break down the difference in how the resulting tables are handled in the above two expressions:

Headcount Measure Headcount =
CALCULATE ( COUNTROWS ( 'Headcount' ), LASTDATE ( 'Date'[Date] ) )

• Implicit Filter Context: In this measure, the LASTDATE function is used directly as a filter argument within CALCULATE. This implicitly filters the 'Headcount' table based on the most recent date in the 'Date' table.

• No Explicit Table Declaration: The LASTDATE function here does not explicitly declare a resulting table. Instead, it returns a single date value, which CALCULATE uses to filter the 'Headcount' table.

RecentSalesCount Measure RecentSalesCount =
CALCULATE (
    COUNTROWS ( Sales ),
    Sales[OrderDate] = LASTDATE ( Sales[OrderDate] )
)

• Explicit Filter Context: In this measure, the filter argument is explicitly declared as Sales[OrderDate] = LASTDATE(Sales[OrderDate]). This creates a clear filter condition that CALCULATE applies to the 'Sales' table.

• Explicit Table Declaration: The Sales table is explicitly mentioned in the filter condition, making it clear that the filter is applied to the 'Sales' table based on the most recent 'OrderDate'.

Key Differences

  1. Implicit vs. Explicit Filtering:

• Headcount: Uses LASTDATE directly, which implicitly filters the 'Headcount' table.

• RecentSalesCount: Uses an explicit filter condition Sales[OrderDate] = LASTDATE(Sales[OrderDate]), clearly defining the filter context.

  1. Resulting Table Declaration:

• Headcount: Does not explicitly declare a resulting table in the filter argument.

• RecentSalesCount: Explicitly declares the 'Sales' table in the filter condition.

In summary, the main difference lies in how the filter context is defined and applied. The Headcount measure relies on an implicit filter from LASTDATE, while the RecentSalesCount measure uses an explicit filter condition to clearly define the resulting table and filter context. This explicit declaration can make the logic more readable and easier to understand.

Removing Explicit Declaration

It is possible to avoid explicitly declaring Sales[OrderDate] in the filter argument, but it requires a different approach:

RecentSalesCount =
CALCULATE (
    COUNTROWS ( Sales ),
    FILTER ( Sales, Sales[OrderDate] = LASTDATE ( Sales[OrderDate] ) )
)

In this expression:

• FILTER(Sales, Sales[OrderDate] = LASTDATE(Sales[OrderDate])): This part creates a table that includes only the rows from the Sales table where the OrderDate matches the most recent date.

• CALCULATE(COUNTROWS(Sales), ...): The CALCULATE function then counts the rows in this filtered table.

This approach achieves the same result without explicitly declaring Sales[OrderDate] in the filter argument of CALCULATE. Instead, the filter is applied within the FILTER function, which is then used as an argument in CALCULATE.

Conclusion

Explicitly declaring the table and column in your DAX expressions can make your code more readable and easier to understand. It clearly defines the context in which the calculation is performed, which can help avoid confusion and potential errors.

For example, in the expression:

RecentSalesCount Measure RecentSalesCount =
CALCULATE (
    COUNTROWS ( Sales ),
    Sales[OrderDate] = LASTDATE ( Sales[OrderDate] )
)

The explicit declaration of Sales[OrderDate] makes it clear that the filter is applied to the Sales table based on the OrderDate column. This clarity can be especially helpful when working with complex models or collaborating with others.

On the other hand, using implicit filters can sometimes make the code shorter but might obscure the logic, making it harder to debug or understand later.


Exercise

Let's create some example tables to illustrate how the above expressions work.

Example 1: Headcount Measure Headcount Table:

EmployeeIDDate
12024-09-14
22024-09-14
32024-09-15
42024-09-15
52024-09-16

Date Table:

Date
2024-09-14
2024-09-15
2024-09-16

Headcount Measure:

Headcount = CALCULATE(COUNTROWS('Headcount'), LASTDATE('Date'[Date]))

Explanation:

• The LASTDATE('Date'[Date]) function returns the most recent date in the 'Date' table, which is 2024-09-16.

• The CALCULATE function then counts the rows in the 'Headcount' table where the date is 2024-09-16.

• Result: 1 (only one row matches the most recent date).

Example 2: RecentSalesCount Measure Sales Table:

OrderIDOrderDate
1012024-09-14
1022024-09-14
1032024-09-15
1042024-09-15
1052024-09-16

RecentSalesCount Measure:

RecentSalesCount = CALCULATE(COUNTROWS('Sales'), 'Sales'[OrderDate] = LASTDATE('Sales'[OrderDate]))

Explanation: • The LASTDATE('Sales'[OrderDate]) function returns the most recent order date in the 'Sales' table, which is 2024-09-16.

• The CALCULATE function then counts the rows in the 'Sales' table where the OrderDate is 2024-09-16.

• Result: 1 (only one row matches the most recent order date).

Summary • Headcount Measure: Counts rows in the 'Headcount' table for the most recent date in the 'Date' table.

• RecentSalesCount Measure: Counts rows in the 'Sales' table for the most recent order date in the 'Sales' table.

These examples should help clarify how the measures work with their respective tables

0
Subscribe to my newsletter

Read articles from Mohamad Mahmood directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mohamad Mahmood
Mohamad Mahmood

Mohamad's interest is in Programming (Mobile, Web, Database and Machine Learning). He studies at the Center For Artificial Intelligence Technology (CAIT), Universiti Kebangsaan Malaysia (UKM).