Sub topic 6
One-to-many: the standard relationship
This chapter assumes you can load tables into Power BI Desktop, you know what a slicer and a measure are, and you have seen the Model view. It covers the relationship type you will create most often, how it behaves by default, and the errors that come with it.
What a one-to-many relationship is
A one-to-many relationship links a column that holds each value once to a column that can repeat values. The "one" side is usually a dimension table, such as Product, Customer, or Date. The "many" side is usually a fact table, such as Sales.
- One side: every value in the key column is unique, and none is blank.
Product[ProductKey]has one row per product. - Many side: the matching column can repeat values.
Sales[ProductKey]has one row per sale, so a popular product appears many times.
The diagram shows a small star schema with two one-to-many relationships.
In the Model view, this relationship appears as a line with a 1 at the dimension end and a * at the fact end. A small arrow in the middle shows the filter direction.
Requirements for the one side
Power BI needs the "one" column to be a valid key. When you create the relationship, it checks the data in that column.
- Values must be unique. Two rows with
ProductKey = 17break the one side. - The column should not contain blanks. A single blank counts as a value, and most models should not have one on a dimension key.
- Both columns must have the same data type. A whole-number key does not match a text key, even if the text looks like a number.
- Text matching ignores case in the engine, but not stray spaces.
"A100"and"A100 "are different values.
Check uniqueness before you blame the relationship. The query below, run in DAX query view, returns any key that appears more than once. An empty result means the column is safe as a one side.
EVALUATE
FILTER (
ADDCOLUMNS (
VALUES ( Product[ProductKey] ),
"Rows", CALCULATE ( COUNTROWS ( Product ) )
),
[Rows] > 1
)Default direction: from one to many
A new one-to-many relationship filters in a single direction. The filter goes from the one side to the many side. This is the intended behavior, and it is why dimensions work as filters.
Suppose you have this measure:
Total Sales = SUM ( Sales[SalesAmount] )Now compare what happens in a report:
| Action | Result |
|---|---|
| Put `Product[Category]` on rows of a matrix with `[Total Sales]` | Each category shows the sum of its own sales rows |
| Select "Bikes" in a `Product[Category]` slicer | Sales rows are limited to Bikes, so the total drops |
| Select a value in a slicer built from `Sales[ProductKey]` | The Product table is not filtered, because the filter does not travel from many to one |
The third row is the common surprise. A slicer built from a fact column cannot narrow a dimension. Build slicers from dimension columns and the problem does not appear.
Reading the relationship in the Manage relationships dialog
Open Modeling > Manage relationships, or double-click the line in the Model view. The dialog shows:
- The "from" table and column, and the "to" table and column.
- Cardinality, which should read Many to one (:1)* when the from table is Sales and the to table is Product. The order only reflects which table you listed first.
- Cross-filter direction, which should read Single for the standard case.
- Make this relationship active, which should be checked.
Power BI often detects cardinality for you. Treat the detected value as a hint and confirm it against what you know about the data. If you expect a one-to-many relationship and the dialog shows many-to-many, that tells you the dimension key has duplicates.
Using the relationship in DAX
The relationship also works inside calculated columns. RELATED follows the relationship from the many side to the one side and returns a single value. This calculated column on Sales returns the product category for each sales row:
Category = RELATED ( Product[Category] )RELATEDTABLE goes the other way. From the one side, it returns the rows on the many side that belong to the current row. This calculated column on Customer counts that customer's sales rows:
Order Count = COUNTROWS ( RELATEDTABLE ( Sales ) )Neither function is needed in measures that group by a dimension column. The relationship already filters Sales for you. Use them when you must create a column that depends on the other table.
Unmatched keys
A one-to-many relationship tolerates keys on the many side that have no match in the dimension. If Sales[ProductKey] contains 999 and Product has no such row, Power BI groups the sale under a blank category in visuals. The total stays correct, but a "(Blank)" row appears. A later chapter covers how to find and fix these rows.
The reverse is not a problem. Products that never sold do not affect Sales totals. They simply show no data, or they do not appear in a visual that hides empty rows.
Common errors
- "The column contains duplicates" or an automatic switch to many-to-many. The intended one side has repeated keys. Remove duplicates, or choose a better key column.
- Relationship cannot be created because of type mismatch. Convert both columns to the same type in Power Query, not with a calculated column.
- A slicer does nothing to another visual. The slicer is probably on the many side, or the relationship is inactive.
- Totals look too large after a merge. A dimension table was loaded with duplicate keys, so a join repeated fact rows. Check uniqueness with the query above.
- Key columns are summarized. Numeric keys may appear with a sigma icon in the Data pane. Set their summarization to Don't summarize so they are not added accidentally.
Exercise
A Sales table has a ProductKey column with repeated values. A Product table has a ProductKey column with each value once. Which table is on the "many" side, and which direction does the default filter travel?
Show answer
Sales is on the many side and Product is on the one side. The default filter travels in a single direction, from Product to Sales. Selecting a product category narrows Sales, but selecting a sales row does not narrow Product.
Exercise
Power BI shows many-to-many cardinality when you connect Sales to a Customer table you expected to be one-to-many. What is the most likely cause, and how can you confirm it?
Show answer
The Customer key column probably has duplicate values or blanks, so it is not a valid one side. Confirm it by running a DAX query that returns each CustomerKey with its row count and filters for counts above 1, as in the ProductKey query in this chapter.
Exercise
You want a calculated column on the Customer table that counts that customer's rows in Sales. Which function do you use, and which side of the relationship does it start from?
Show answer
Use RELATEDTABLE, for example Order Count = COUNTROWS ( RELATEDTABLE ( Sales ) ). It starts on the one side (Customer) and returns the related rows from the many side (Sales).