Sub topic 4
How filters travel across relationships
This chapter assumes you know what a one-to-many relationship is and can read a simple DAX measure. It also assumes you know that a filter context is the set of filters active when a formula is evaluated. Here you will learn how a filter on one table reaches another table, and why it sometimes does not.
The model used in this chapter
The examples use a small star schema. Product and Customer are dimension tables, and Sales is the fact table. Each product can appear in many sales rows, and each customer can appear in many sales rows.
The "one" side holds unique keys. The "many" side repeats those keys. This difference decides which way filters flow.
The default direction: one side to many side
A relationship passes filters from the one side to the many side. This is the default behavior of a single-direction relationship.
Suppose a slicer selects Product[Color] = "Red". The filter context now keeps only the red rows in Product. Because Product is on the one side of the relationship, that filter continues to Sales and keeps only the sales rows whose ProductKey matches a red product. Any measure that reads Sales then sees only those rows.
Consider this measure:
Total Quantity = SUM ( Sales[Quantity] )Place Product[Color] on the rows of a matrix. For the "Red" row, the filter context contains one filter on Product[Color]. The filter travels to Sales, so Total Quantity adds up only red sales. No code in the measure mentions Product. The relationship does the work.
Filters do not travel the other way by default
A filter on the many side does not reach the one side. If a visual filters Sales[Quantity] to values above 5, the Product table is not reduced by that filter, so a count of products stays the same.
Product Count = COUNTROWS ( Product )Put Customer[Country] on the rows of a matrix with Product Count. Each country shows the same number, which is the total number of products. The filter on Customer reaches Sales, but it stops there. Product is on the one side of its own relationship, and filters do not flow from Sales up to Product.
To count only the products that were sold to the selected country, you can count the products that still appear in the filtered Sales table:
Products Sold =
CALCULATE (
COUNTROWS ( Product ),
Sales
)Using the Sales table as a filter argument turns the surviving sales rows into a filter on Product. This works because the argument is a table whose columns include ProductKey. We cover CALCULATE in detail in a later chapter, so treat this as a preview.
Bidirectional filtering
A relationship can be set to filter in both directions. Then a filter on Sales also reaches Product. This can make the Product Count measure above respond to a country selection.
Be careful with this setting. It has costs:
- Filters can reach tables you did not expect, so results become harder to predict.
- Several bidirectional relationships can create ambiguous filter paths between tables.
- Evaluation can be slower on large models.
Prefer a single direction unless a specific report needs both. When you need the effect once, use a table filter inside CALCULATE, as shown above.
How several filters combine
When filters arrive at the same table from several places, DAX applies all of them together. A row of Sales is visible only if it passes every filter.
For example, a slicer on Product[Category] and a matrix row on Customer[Country] both reach Sales. Only rows that match the selected category and the selected country remain. Filters on different tables combine with AND logic. Several selected values on the same column, such as two colors, combine with OR logic inside that column.
Common errors
- Expecting a filter on
Salesto change a dimension. If a count of dimension rows does not change, check the filter direction first. - Using a column from an unrelated table. A column with no relationship path to the fact table filters nothing. Every row shows the same total.
- Joining on a column with duplicate keys. A relationship's one side needs unique keys. Duplicates in the key column break the one-to-many shape and cause errors or a many-to-many relationship.
- Turning on bidirectional filtering to fix one visual. The fix then changes every other measure that uses the relationship.
Exercise
A matrix shows Customer[Country] on the rows and the measure COUNTROWS ( Product ). Every country shows the same number, and the relationships are the default single-direction ones. Why, and how can you count only the products sold in each country?
Show answer
The Customer filter flows to Sales but cannot flow from Sales up to Product, so Product is never filtered. Use CALCULATE ( COUNTROWS ( Product ), Sales ) so the filtered Sales rows become a filter on Product.