Sub topic 2
Cardinality and how filters travel
This chapter assumes you can open Power BI Desktop model view and recognize fact and dimension tables. It also assumes you know what a one-to-many relationship is and can write basic DAX measures with SUM and CALCULATE. Here you will learn what cardinality means, which side of a relationship it applies to, and how a filter moves from one table to another. The next chapter covers the direction of that movement in more detail.
What cardinality describes
Cardinality describes how many rows on one side of a relationship can match rows on the other side. It is a property of each relationship, and it depends on the uniqueness of the two columns the relationship joins.
Power BI offers four settings:
| Setting | Meaning | Typical use |
|---|---|---|
| One-to-many (1:*) | The "one" column has unique values. The "many" column can repeat them. | Dimension to fact |
| Many-to-one (*:1) | The same relationship, drawn the other way round. | Fact to dimension |
| One-to-one (1:1) | Both columns have unique values. | Splitting one wide table in two |
| Many-to-many (*:*) | Neither column is required to be unique. | Covered in later chapters |
One-to-many and many-to-one are the same thing. Only the order in which you chose the tables differs. In model view, the diagram marks the "one" end with 1 and the "many" end with *.
The example model
The chapters in this tutorial use a small banking model. For this chapter you need three tables:
Customershas one row per customer, withCustomerID.Accountshas one row per account, withAccountIDandAccountType.Transactionshas one row per transaction, withTransactionID,AccountIDandAmount.
Here the relationship runs from Accounts[AccountID] to Transactions[AccountID]. Each account is listed once in Accounts. It can appear in many transactions. That makes Accounts the "one" side and Transactions the "many" side.
The rule behind the "one" side
The "one" side must hold unique values. If Accounts[AccountID] contained the same ID twice, Power BI could not tell which row a transaction belongs to. Two things follow from this.
- If you create a one-to-many relationship on a column that has duplicates, Power BI rejects it.
- Blank values count as a value. A column on the "one" side cannot hold more than one blank.
Matching columns should also share a data type. A whole-number AccountID in one table and a text AccountID in the other will not match, even when the values look identical on screen. Fix the type in Power Query before you build the relationship.
How a filter travels
A relationship's main job is to pass filters. When you pick a value in a slicer or place a column on a visual axis, you create a filter on that column. Power BI then looks at every relationship that touches the filtered table. It restricts the rows of the related table so that only matching rows remain. The set of rows still visible is called the filter context.
With the default setting (a single cross-filter direction), a filter travels from the "one" side to the "many" side. It does not travel the other way.
Take a slicer on Accounts[AccountType] with "Savings" selected:
- The slicer filters
Accountsto the rows whereAccountTypeis "Savings". - The relationship passes the remaining
AccountIDvalues toTransactions. Transactionsshows only rows whoseAccountIDis in that list.- A measure such as
SUM ( Transactions[Amount] )adds up only those rows.
Measures to see the behavior
Define three measures. They let you test the filter flow in a table visual.
Total Amount = SUM ( Transactions[Amount] )Account Count = COUNTROWS ( Accounts )Transaction Count = COUNTROWS ( Transactions )Now build two visuals and compare what each measure returns.
| Visual | Total Amount | Account Count | Transaction Count |
|---|---|---|---|
| Rows grouped by `Accounts[AccountType]` | Splits correctly by type | Splits correctly by type | Splits correctly by type |
| Rows grouped by `Transactions[TransactionID]` | Shows each amount | Same total on every row | Shows 1 on every row |
The second row of the table needs explaining. When you group by a column from the "many" side, the filter lands on Transactions. Single-direction filtering stops there. Accounts is never filtered, so Account Count returns the full account count on every row. This repeated value is the most common sign that a filter is not traveling the way you expected.
Filters travel through chains
A filter can pass through more than one relationship. Suppose you add Customers above Accounts, so that each account has exactly one owner. That gives a chain:
Customers → Accounts → Transactions
Each link is one-to-many, and each passes the filter downhill. A filter on Customers[CustomerID] reaches Accounts, and from there reaches Transactions. The model needs no special setup for this. Power BI follows every link in the chain.
This works because each account has one customer. When an account can belong to several customers, a customer-to-account relationship stops being one-to-many. That is the situation the rest of the tutorial deals with. The next chapters explain why it happens and how to model it.
Referential integrity and the blank row
Sometimes a "many" side value has no match on the "one" side. A transaction may point to AccountID 999, which does not exist in Accounts. This is a referential integrity violation.
Power BI keeps these rows. It adds a virtual blank row to the "one" table (the Accounts table here). The unmatched transactions attach to that blank row. You will see it as a blank category in visuals grouped by an Accounts column. The amounts in that blank group are real. They are the transactions that could not be matched.
Treat a blank category as a data-quality signal. Do not hide it with a visual filter until you know where those rows come from. A quick check is a DAX query, which you can run in DAX query view:
EVALUATE
SUMMARIZECOLUMNS (
Accounts[AccountType],
"Amount", [Total Amount]
)
ORDER BY Accounts[AccountType]If the output contains a row with a blank AccountType, some transactions have an AccountID that does not exist in Accounts.
One active path between two tables
Two tables can have several relationships between them. Only one of them can be active at a time. The active one is the solid line in model view, and the inactive ones are dotted.
The same limit applies to paths through other tables. If two different routes of active relationships connected the same pair of tables, a filter would reach the target by two routes and the result would be ambiguous. Power BI blocks this. It marks one relationship inactive, or it refuses to make the new one active.
A measure can switch on an inactive relationship for a single calculation with USERELATIONSHIP:
Amount by Ship Date =
CALCULATE (
[Total Amount],
USERELATIONSHIP ( Transactions[SettlementDate], Dates[Date] )
)This example assumes the model has a Dates table and a Transactions[SettlementDate] column, which the banking model above does not include. The point is the pattern. You will meet the same idea again, since later chapters also control filter flow inside a measure.
Common errors at this stage
- A repeated total on every row. A visual groups by a "many" side column and a measure reads the "one" side. The filter does not travel against the relationship direction.
- A relationship that cannot be created as one-to-many. The column you chose as the "one" side has duplicates or more than one blank. Check it with
DISTINCTCOUNTagainstCOUNTROWS. - A relationship that matches nothing. The two columns have different data types, or the values differ in case, spacing or leading zeros. Clean them in Power Query.
- A blank category in a visual. The "many" side holds a key that the "one" side lacks. Fix the source data or add the missing dimension row.
- A relationship drawn as many-to-many by auto-detect. Power BI found duplicate values on both sides. Do not accept it without asking why. Often one of the columns should have been deduplicated, or the relationship should use a different column.
Summary of the working model
A relationship joins one column in each table. Cardinality states how many matches each value can have. The "one" side must be unique. By default, filters move from the "one" side to the "many" side, and they continue along chains of such relationships. Rows on the "many" side with no matching key appear under a blank member. When a measure returns the same value on every row, check which table the visual filters and which direction the relationships allow.
Exercise
A model has Accounts (one row per AccountID) related one-to-many to Transactions on AccountID. A table visual lists Transactions[TransactionID] and shows the measure COUNTROWS ( Accounts ). Every row shows 250, the total number of accounts. Why, and which side of the relationship does the filter reach?
Show answer
Grouping by TransactionID filters the Transactions table, which is the "many" side. With the default single direction, filters travel only from the "one" side (Accounts) to the "many" side (Transactions), so the filter never reaches Accounts. COUNTROWS ( Accounts ) is therefore evaluated with no filter and returns all 250 accounts on every row.
Exercise
You try to create a one-to-many relationship from Accounts[AccountID] to Transactions[AccountID], and Power BI refuses. Name two checks that could explain the failure.
Show answer
First, Accounts[AccountID] may contain duplicate values or more than one blank, so it cannot serve as the unique "one" side. Compare DISTINCTCOUNT ( Accounts[AccountID] ) with COUNTROWS ( Accounts ). Second, the two columns may have different data types, such as whole number against text, so they cannot be matched. Convert both to the same type in Power Query.