Sub topic 5
Cardinality: what one and many mean
This chapter assumes you can load tables into Power BI Desktop, know what a key column is, and have seen the Model view. Cardinality describes how rows in one table match rows in another through a relationship. Power BI uses it to decide how filters move between tables and how it checks your data.
What cardinality describes
Each end of a relationship is either "one" or "many".
- The one side has unique values in the key column. No value appears twice, and blanks count as a value.
- The many side can repeat values in its key column.
Cardinality is a property of the key columns, not of the tables as a whole. Power BI detects it when you create the relationship, by checking for duplicates in each column.
The four options
| Cardinality | Meaning | Typical use |
|---|---|---|
| One-to-many (1:*) | Unique key on one side, repeated on the other | Dimension to fact table |
| Many-to-one (*:1) | The same relationship, listed in the other direction | Fact table to dimension |
| One-to-one (1:1) | Both columns are unique | Splitting one entity across two tables |
| Many-to-many (*:*) | Neither column is unique | Special cases, covered in a later chapter |
One-to-many and many-to-one are the same relationship. Only the order of the tables in the dialog differs.
Reading it in the Model view
In the Model view, the line between two tables shows a 1 at the one end and a * at the many end. Double-click the line to open the Edit relationship dialog and see the cardinality Power BI chose.
Why the choice matters
A wrong cardinality causes two kinds of trouble.
- If you pick one-to-many on a column that has duplicates, Power BI refuses to create the relationship.
- If a many-to-many relationship is created when a one-to-many was possible, filters can act in unexpected ways and totals can look wrong.
The usual cause of an unexpected many-to-many is a dimension table with duplicate keys. For example, a Customer table that lists the same CustomerKey twice. Fix the data instead of accepting the weaker relationship.
Duplicate Keys =
COUNTROWS ( Customer ) - DISTINCTCOUNT ( Customer[CustomerKey] )Use this measure in a card visual. A result above zero means the key is not unique. If the column also contains blanks, DISTINCTCOUNT counts the blank as a value, so compare it with the row count.
Choosing cardinality
- Identify the dimension table, which describes things such as customers or products.
- Confirm its key column has no duplicates.
- Identify the fact table, which records events such as sales.
- Relate the dimension key (one) to the matching fact column (many).
Exercise
A Product table has 500 rows but its ProductKey column has only 498 distinct values. You try to relate Product[ProductKey] to Sales[ProductKey] as one-to-many. What happens, and what should you do?
Show answer
Power BI cannot create the one-to-many relationship, because the one side must have unique values. Find the duplicated ProductKey values in the Product table and remove or correct them, so that each key appears once. Then create the relationship again.