Sub topic 5
Cardinality: what one and many mean
This chapter assumes you can load tables into Power BI Desktop and open the Model view. It also assumes you know what a measure and a slicer do. You should know that a fact table holds events, such as sales rows, and that a dimension table describes things, such as products or customers.
What cardinality describes
Cardinality is the property of a relationship that states how many rows on each side can share one key value. Each end of a relationship is either "one" or "many". Power BI offers four settings:
- One-to-many (1:) and many-to-one (:1)
- One-to-one (1:1)
- Many-to-many (:)
The words describe the columns that you join, not the tables as a whole. Look at the key column on each side and ask whether a value can repeat in it.
- A "one" side means every value in the key column appears at most once. The column is unique.
- A "many" side means the same value can appear in several rows.
In a star schema, the dimension key is unique and the fact table's matching column repeats. A Product table has one row per ProductKey. The Sales table has many rows with the same ProductKey. This gives the one-to-many relationship, with Product on the one side and Sales on the many side.
Why Power BI needs to know
Cardinality is not decoration. The engine uses it in three ways.
- Filter travel. A filter on the one side can select matching rows on the many side without ambiguity. Going the other way, from many to one, is less well defined. That is why single-direction filtering runs from the one side toward the many side.
- Validation. When you create a relationship, Power BI checks the data. If you choose one-to-many but the "one" column contains duplicates, the relationship is rejected.
- Blank row handling. For a one-to-many relationship, Power BI can add a blank row to the one-side table to catch unmatched keys. This is covered in a later chapter.
The four settings in detail
| Setting | Meaning | Typical use |
|---|---|---|
| One-to-many | Unique key on one side, repeated key on the other | Dimension to fact. The standard case |
| Many-to-one | The same relationship, with the tables listed in the other order | Same as above |
| One-to-one | Each key appears at most once in both tables | Splitting one entity across two tables |
| Many-to-many | Neither column is required to be unique | Special cases that a bridge table often handles better |
One-to-many and many-to-one are the same relationship. Which one you see depends on which table is listed first in the dialog. The "one" side always holds the unique values.
How Power BI picks a cardinality
When you drag a column from one table onto a column in another, Power BI inspects both columns. It then proposes a cardinality. If one column is unique and the other is not, it proposes one-to-many. If both are unique, it proposes one-to-one. If neither is unique, it proposes many-to-many.
Power BI bases this suggestion on the data currently loaded. That creates a trap. A dimension column that is unique today can pick up a duplicate after the next refresh. The refresh then fails or the relationship changes. A dimension with duplicate keys is a data quality problem that you should fix at the source or in Power Query.
The diagram below shows the standard shape.
Seeing cardinality in the Model view
In the Model view, each relationship is a line between two tables. The line has markers at both ends.
- A 1 marks the one side.
- An \* marks the many side.
- An arrowhead on the line marks the cross-filter direction. A single arrow points from the one side toward the many side. Two arrowheads mean both directions.
Double-click a line to open the Edit relationship dialog. It shows the two tables with a preview of their columns, a Cardinality drop-down, a Cross-filter direction drop-down, and a checkbox for Make this relationship active.
You can also reach every relationship in one place through Manage relationships on the Home tab. The list shows each relationship's From and To columns, its cardinality, and whether it is active.
Worked example: choosing cardinality
Suppose you load three tables.
| Table | Key column | Is the key unique? |
|---|---|---|
| Sales | ProductKey | No. Many rows per product |
| Product | ProductKey | Yes. One row per product |
| ProductDetail | ProductKey | Yes. One row per product |
Consider each pair of tables.
- Product and Sales. Product is unique and Sales repeats. Choose one-to-many, with Product as the one side.
- Product and ProductDetail. Both are unique. Choose one-to-one. This is a hint that the two tables describe the same entity. Often the better design is to merge them into one table in Power Query.
- Sales and another fact table, such as Returns. Both repeat on ProductKey. Joining them directly gives many-to-many. A better design joins both facts to the Product dimension, so that Product filters both.
The last case shows the most useful habit. If you reach for many-to-many between two fact tables, ask whether a shared dimension should sit between them.
Checking whether a column is unique
Before you trust a suggested cardinality, check the key column. This measure counts the number of rows and the number of distinct key values in a table.
Product Key Check =
VAR RowCount = COUNTROWS ( Product )
VAR DistinctKeys = DISTINCTCOUNT ( Product[ProductKey] )
RETURN
IF ( RowCount = DistinctKeys, "Unique", "Duplicates present" )Put the measure in a card visual. If it reports "Duplicates present", the column cannot sit on the one side of a one-to-many relationship. Find the duplicate keys, then fix them in the source or remove them in Power Query.
Common errors
- "The Cardinality you selected isn't valid for this relationship." You chose one-to-many or one-to-one, but the column you called unique holds duplicates. Either clean the data or choose a different cardinality.
- Blank values in the "one" column. A blank counts as a value. Rows with a blank key in the dimension can cause surprising matches. Remove them or replace them with a real key.
- Mismatched data types. A text key on one side and a whole-number key on the other can produce no matches. The relationship may exist but filter nothing. Make both columns the same type.
- Case and spacing differences. Text keys such as "A-100" and "a-100 " may not match as you expect. Trim and standardize them before loading.
- Wrong side marked as "one". If you swap the tables by mistake, the filter arrow points the wrong way. Check the 1 and * markers after you create each relationship.
Edge cases
- A one-to-one relationship always filters in both directions. Because each key appears once on each side, no ambiguity arises, and Power BI sets the direction to both.
- Many-to-many allows either single or both directions. It also does not create a blank row for unmatched keys in the same way. Treat it carefully.
- Composite models. When tables use different storage modes, such as Import and DirectQuery, relationships can become limited relationships. In a limited relationship, Power BI cannot guarantee that the one side is unique. The 1 marker does not apply, and the engine cannot add the blank row. Tables that sit in separate source groups give this result.
Summary of the decision
- Look at the key column in each table.
- If one is unique and the other repeats, use one-to-many with the unique table on the one side.
- If both are unique, use one-to-one, and consider merging the tables.
- If neither is unique, look for a shared dimension or a bridge table before you use many-to-many.
Exercise
You relate a Customer table (one row per CustomerKey) to a Sales table (many rows per CustomerKey). Which cardinality do you use, which table is on the one side, and what does the engine do if Customer later receives two rows with the same CustomerKey?
Show answer
Use one-to-many, with Customer on the one side and Sales on the many side. If Customer receives duplicate CustomerKey values, the one side is no longer unique. The relationship can no longer be valid as one-to-many, so the refresh reports an error. The fix is to remove the duplicates at the source or in Power Query.
Exercise
You have two fact tables, Sales and Returns, and both repeat ProductKey. A colleague proposes joining them directly with a many-to-many relationship. What do you suggest instead?
Show answer
Relate both fact tables to a single Product dimension with one-to-many relationships. Product then filters both Sales and Returns, so a Product slicer or a Category column affects both tables consistently, and no many-to-many relationship is needed.