Sub topic 2
How Filters Flow Through Relationships
This chapter assumes you can write basic DAX measures and calculated columns. It also assumes you know a star schema: fact tables on the many side, dimension tables on the one side, joined by one-to-many relationships. You should know how a slicer or a row in a matrix filters a measure.
The idea
A filter context is a set of filters on columns. A slicer on 'Product'[Color] filters the Product table directly. A measure over Sales[Amount] still responds to that slicer, because the relationship between Product and Sales carries the filter across.
Think of the relationship as a one-way channel. By default, a filter travels from the one side to the many side. A filter on a dimension table reaches the fact table. A filter on the fact table does not travel back to the dimension.
A worked model
Take a small star schema.
| Table | Role | Key columns |
|---|---|---|
| `Product` | Dimension (one side) | `ProductKey`, `Color`, `Category` |
| `Customer` | Dimension (one side) | `CustomerKey`, `Country` |
| `Sales` | Fact (many side) | `ProductKey`, `CustomerKey`, `Quantity`, `Amount` |
Each dimension has a one-to-many relationship to Sales. We use the base measure from earlier in this tutorial.
Total Sales = SUM ( Sales[Amount] )Suppose a matrix shows 'Product'[Category] on rows, and a slicer selects 'Customer'[Country] = "France". For the row "Bikes", the filter context holds two filters:
'Product'[Category] = "Bikes"'Customer'[Country] = "France"
Both filters propagate to Sales. DAX keeps only the Sales rows that satisfy both, and SUM adds up Amount for those rows.
Direction matters
Filters flow from the one side to the many side. They do not flow the other way by default. This produces results that surprise people at first.
Suppose a filter reaches Sales from the Product table. Does the Customer table see it? No. The filter moves from Product to Sales and stops. Sales does not pass it on to Customer, because that would mean going from the many side to the one side.
Consider a measure that counts customers.
Customer Count = COUNTROWS ( Customer )If a matrix shows 'Product'[Category] on rows, every row shows the same number: the total count of customers. The Product filter reaches Sales but never reaches Customer. To count only customers who bought in that category, you would count through the fact table or change the relationship's filter direction.
Customers Who Bought =
CALCULATE (
COUNTROWS ( Customer ),
Sales
)Here Sales is a table argument. Its rows, already filtered by Product, become a filter on Customer. Table filter arguments get their own chapter later, so treat this as a preview.
An alternative is to set the relationship to filter in both directions. That works, but it can create ambiguous paths and slow queries. Prefer the single-direction model unless you have a clear reason.
Relationships and the filter context together
Two points connect this chapter to the next ones.
- The filter context is defined on columns, and relationships decide which other tables those column filters reach. Propagation happens automatically for every measure evaluation.
CALCULATEmodifies the filter context, and the modified context propagates exactly as before. IfCALCULATEsets'Product'[Color] = "Red", that filter flows toSalesthrough the same relationship as a slicer selection would.
This is why you usually filter a dimension column rather than a fact column. A filter on 'Product'[Color] reaches every fact table related to Product. A filter on a column in Sales affects only Sales and anything below it.
Common errors
- Expecting a fact-table filter to reach a dimension. A filter on
Sales[Quantity]does not changeProductorCustomer. A count of products does not shrink. - Forgetting that unrelated tables are not filtered. If two tables share no relationship path, a filter on one has no effect on the other.
- Turning on bidirectional filtering to fix one measure. This changes every measure that uses the relationship. Try a table filter argument first.
Exercise
A matrix has 'Product'[Category] on rows. A measure defined as COUNTROWS ( Product ) shows the same total in every row, while Total Sales changes per row. Why does the count not change, and what is wrong with the assumption behind the question?
Show answer
The measure counts rows in Product, which is the table being filtered by the row. If the matrix really shows the same total in every row, the matrix is filtering a column from a different table than Product, or the measure is evaluated where no filter reaches Product. Check which table the row field belongs to. Remember the rule: filters flow from the one side to the many side. A filter on a column in Product changes COUNTROWS ( Product ) directly. A filter on Sales or Customer does not reach Product, so the count stays at the total.
Exercise
Using the model in this chapter, a matrix has 'Customer'[Country] on rows. Does COUNTROWS ( Product ) change from row to row? Explain.
Show answer
No. The Country filter travels from Customer to Sales, because Customer is on the one side. It stops at Sales and does not continue to Product, since that would mean moving from the many side to the one side. Product is unfiltered, so every row shows the total product count.