Sub topic 6
One-to-many: the standard relationship
This chapter assumes you can load tables into Power BI Desktop, know what a measure like SUM does, and have seen fact and dimension tables. You do not need to know cross-filter direction in detail yet.
What one-to-many means
In a one-to-many relationship, the "one" side is a column where every value appears once. This is usually a dimension table's key. The "many" side is a column where a value can appear any number of times. This is usually a fact table's foreign key.
For example, Product[ProductKey] holds each product once. Sales[ProductKey] holds a product once for every sale. One product relates to many sales rows.
Power BI draws this in the Model view as a line with a 1 at the dimension end and a * at the fact end. By default, the arrow on the line points from the "one" side to the "many" side. This shows the direction filters travel.
Why it is the default choice
Power BI detects cardinality when you create a relationship. It picks one-to-many when one column has unique values and the other does not. Use it whenever you can, for three reasons:
- It matches the star schema, so filters flow from dimensions into facts.
- It is fast, because the "one" side is a lookup of unique keys.
- It is predictable. A slicer on
Product[Category]filtersSalesin one clear direction.
Requirements for the "one" side
The "one" column must contain no duplicates. A blank is allowed, but only once. If you try to create a one-to-many relationship on a column with duplicates, Power BI reports that it cannot create the relationship. Check the dimension table for repeated keys before you blame the relationship.
Worked example
Suppose Sales has ProductKey and SalesAmount, and Product has ProductKey and Category. After you relate them, a table visual with Product[Category] and a total shows each category's sales.
Total Sales = SUM ( Sales[SalesAmount] )The measure is a sum over Sales. The category in the visual comes from Product. The relationship carries the category filter into Sales, so each row of the visual sums only that category's sales rows.
You can also use the relationship in a calculated column. Added to Sales, this returns each row's category by following the relationship from the "many" side to the "one" side:
Category = RELATED ( Product[Category] )Common errors
- Wrong direction of thought. Filters flow from "one" to "many". Filtering
Salesdoes not narrowProductunless you change the cross-filter direction, which a later chapter covers. - Mismatched data types. A text key on one side and a whole number on the other will not match. Fix both columns to the same type.
- Duplicates in the dimension. A "one" side with repeated keys forces a many-to-many relationship, which hides data quality problems.
Exercise
A Customer table has CustomerKey values 1, 2, 3, 3. Sales has CustomerKey on every row. Why can Power BI not create a one-to-many relationship from Customer to Sales, and what should you do?
Show answer
The "one" side must have unique values, and CustomerKey 3 appears twice in Customer. Remove or merge the duplicate row so each key appears once. Then create the one-to-many relationship from Customer[CustomerKey] to Sales[CustomerKey].