Sub topic 4
Creating and editing relationships
This chapter assumes you can load tables into Power BI Desktop and open the Model view. It also assumes you know what a fact table and a dimension table are, and that you have written a simple measure such as SUM. Here you create relationships three ways, read the settings that Power BI shows for each one, and check that a relationship behaves as you intended.
What a relationship stores
A relationship is a rule stored in the model. It says that a column in one table matches a column in another table. When a visual filters the first table, Power BI uses the rule to filter the second.
Each relationship has these properties:
- A From column and a To column, one in each table.
- A cardinality: one-to-many, many-to-one, one-to-one, or many-to-many.
- A cross-filter direction: single or both.
- An active flag. Only one relationship between two tables can be active at a time.
This chapter covers where to set these properties. Later chapters explain cardinality and direction in depth.
Three ways to create a relationship
Drag and drop in the Model view
Open the Model view from the left rail. Drag a column from one table onto the matching column in another table, for example Product[ProductKey] onto Sales[ProductKey]. Power BI draws a line between the tables and fills in the cardinality and direction it thinks are correct.
This is the fastest method. It does not show you the settings first, so always check the result afterward.
The Manage relationships dialog
On the Home tab, select Manage relationships. The dialog lists every relationship in the model. Select New relationship and then:
- Pick the first table from the first drop-down. Its columns appear in a preview grid.
- Click the key column in the grid.
- Pick the second table and click its key column.
- Review Cardinality and Cross-filter direction.
- Leave Make this relationship active selected unless you want an inactive one.
- Select Save.
This route lets you review the settings before the relationship exists. It is the better choice when a table pair could be related on more than one column.
Autodetect
The dialog also has an Autodetect button. Power BI looks for columns with matching names and data types and creates relationships for them. The same detection can run when data is loaded, depending on your options settings.
Autodetect is useful for a first draft. It guesses from names, so it can relate columns that only look similar. Review each relationship it creates.
Reading the line in the Model view
Each line carries markers that summarize its settings.
| Marker | Meaning |
|---|---|
| `1` at one end, `*` at the other | One-to-many. The `1` side is the table with unique keys. |
| `1` at both ends | One-to-one |
| `*` at both ends | Many-to-many |
| Single arrowhead | Single direction. Filters flow in the arrow's direction. |
| Two arrowheads | Both directions |
| Solid line | Active relationship |
| Dashed line | Inactive relationship |
For a standard star schema, the arrowhead points from the dimension (the 1 side) toward the fact table (the * side).
Editing an existing relationship
You have three ways to open an existing relationship:
- Double-click its line in the Model view.
- Select the line and use the Properties pane on the right.
- Select Manage relationships, pick the relationship, and select Edit.
The edit dialog lets you change cardinality, direction, and the active flag. In the same dialog you can change which columns are related by choosing a different column in either grid.
Select a line and press Delete to remove a relationship. Power BI asks you to confirm. Deleting a relationship does not delete any data. Visuals that depended on it start to show the unfiltered totals described below.
Highlighting
When you select a relationship line, Power BI highlights the two related columns inside their tables. This is the quickest way to confirm that a line connects the columns you meant.
Why a relationship might not save
Power BI blocks or warns about a relationship in these cases:
- Mismatched data types. You cannot relate a text column to a whole-number column. Fix the type in Power Query or the column tools first. A common cause is a key stored as text in one table and as a number in the other.
- Duplicate values on the "one" side. If you choose one-to-many but the "one" column contains duplicates, Power BI reports an error and does not create the relationship as one-to-many. Remove the duplicates or pick a different key.
- Ambiguous paths. Power BI keeps a relationship inactive if making it active would create a second active route between two tables.
- Circular dependencies. A chain of relationships that loops back to its starting table is rejected.
Verifying a relationship
A line on the diagram does not prove the data matches. Test with a visual:
- Create a table visual with a dimension column, such as
Product[Category]. - Add a measure from the fact table, such as
Total Sales. - Check that different categories show different values.
If every row shows the same grand total, the relationship is missing, inactive, or pointing the wrong way. If a blank category row appears with a value, some fact rows have keys that do not exist in the dimension. A later chapter covers that case.
You can also count unmatched rows directly with a measure:
Unmatched Sales Rows =
COUNTROWS (
FILTER (
Sales,
ISBLANK ( RELATED ( Product[Category] ) )
)
)For each row in Sales, RELATED follows the relationship to Product. It returns blank when no matching product exists, and the measure counts those rows. A result of zero means every sales row found its product. This measure assumes the relationship from Sales to Product exists. Without one, RELATED raises an error.
Practical habits
- Create relationships from the fact table's foreign key to the dimension's primary key, not between two fact tables.
- Hide the foreign key columns in the fact table from report view. Report authors should filter on the dimension column instead.
- Keep the diagram readable. Place dimensions around the fact table so each line is visible.
- After every change, test with a visual before you build on top of it.
Exercise
You drag Sales[ProductKey] onto Product[ProductKey] in the Model view. A table visual with Product[Category] and the measure Total Sales then shows the same value on every category row. List two things you would check in the relationship.
Show answer
First, check that the relationship exists and is active. Dashed lines mean it is inactive, and a deleted or never-saved relationship leaves the dimension unable to filter the fact table. Second, check the cross-filter direction. It must point from Product toward Sales so a filter on Category reaches the sales rows. Also confirm that the key columns have the same data type and that the correct columns are related.