01.8Relationships, primary keys and foreign keys

Your tables are now separated. The Sales fact table keeps only the customer’s code. To find the customer’s city, Power BI links Sales to the Customers dimension table. This link relies on two kinds of columns, the keys.

01.8.1Primary key and foreign key

In Customers, the Customer ID column holds a different value on each row. C001 appears only once. This column identifies each customer without any ambiguity. It is the of Customers.

In Sales, the same Customer ID column repeats. C001 appears twice, once per sale to Orvane. Here, the column points to the primary key of another table. It is a .

Likewise, “Product Code” is the primary key of Products, and a foreign key in Sales.

Tip

To do its job well, a primary key has no duplicates and no blank values. Power BI actually refuses to create a relationship whose “1” side contains duplicates. A foreign key repeats as many times as there are sales for the same customer.

01.8.2The relationship

A links two tables through a shared column. Customers and Sales are linked by “Customer ID,” Products and Sales by “Product Code.”

Here are these relationships as Power BI shows them, in its Model view. Click a relationship, in the diagram or in the list, to see the two columns it links.
Customers
City
Customer ID
Name
Type
Products
Brand
Category
Model
Product Code
Sales
Amount
Customer ID
Date
Price
Product Code
Qty
Sale ID
Manage relationships

This is a reproduction of the Power BI interface for teaching purposes. It differs from the real interface.

Most of the time, you have nothing to do. On load, Power BI spotted a column with the same name in Customers and in Sales, “Customer ID,” and created the relationship on its own. It did the same with “Product Code,” between Products and Sales. Open Model view to check. A line links each dimension table to Sales.

A “1” marks the primary key side, where each value is unique. A “*” marks the foreign key side, where it can repeat. This is called a one-to-many relationship, one customer for many sales.

If a relationship wasn’t created automatically, create it yourself. In Model view, drag the column onto the matching column of the other table. The “New relationship” window opens, already filled in. Check two fields. The cardinality, which says how many rows can match on each side, must read “One to many (1:*).” The cross-filter direction, the way the filter travels from one table to the other, must read “Single.” If a field shows something else, click it. The list of other choices opens, and you pick the right value.

Calendar, on the other hand, was created after loading, in DAX. Power BI didn’t link it on its own. Link it to Sales the same way, through the Date column. Each date appears only once in Calendar, and several times in Sales. So check that the cardinality also reads “One to many (1:*),” and the direction “Single.”

01.8.3The filter follows the relationship

A filter placed on a dimension flows down to the fact table, from the “1” side to the “*” side. Filtering Customers on New York keeps the customers C001, C002 and C003. The relationship passes this filter on to Sales, which keeps only their 5 sales.

This path is what lets a visual show revenue by city, even though the city isn’t in Sales.

Watch out!

By default, the filter doesn’t flow back up. A filter on the brand, in Products, reaches Sales, but doesn’t go back up to Customers. A measure that counts the rows of Customers then shows 7 for every brand, as if the filter didn’t exist. If a figure doesn’t react to a slicer, your formula isn’t necessarily to blame. Look at the direction of the relationships first. This piece explains many figures that “don’t move.” If a slicer has no effect on your measure, the Diagnostic walks you through it step by step.

01.8.4Why keys matter

For filters. On its own, a filter travels from one table to another only through the relationship, and so through its keys. If a sale carries a Customer ID that isn’t in Customers, for example C009, it is tied to no city. A visual by city then puts it on a row marked “(Blank).”

If the primary key has a duplicate, for example two C001 rows in Customers, Power BI can no longer create a one-to-many relationship. It offers a many-to-many relationship instead, whose results are much harder to predict.

For file size and speed. The fact table keeps only one code per customer, not their name, city and type. Over millions of sales, that is far less data to store and to scan.

A short key, ideally a whole number, takes less space than a long text. The file is lighter, and the visuals calculate faster.

Watch out!

Before creating a relationship, check that the primary key has no duplicates and no blank values, and that every foreign key does exist in the dimension.

Quiz

The questions often rely on the course tables. Keep them in view in their floating window, which you can move and enlarge.

Question 1

In a visual, you place the Customers[City] column and the measure Number of Sales = COUNTROWS( Sales ). What does the Toronto row show?

Question 2

A visual by Products[Brand] shows the measure COUNTROWS( Customers ). The relationships are single-direction. What does the Velmo row show?

Question 3

A new sale of 80,000 carries the Customer ID C009, which isn’t in the Customers table. Where does it appear in a visual of revenue by city?

Key takeaways
  • The primary key identifies each row of a table without ambiguity, like “Customer ID” in Customers. In the fact table, the same column is a foreign key, which repeats.
  • A relationship links two tables through a shared column. On the “1” side, each value is unique. On the “*” side, it can repeat.
  • A filter flows down from a dimension to the fact table. Unless you change a setting, it doesn’t flow back up.
  • Clean keys, with no duplicates and no orphan values, give correct filters. Short keys give a light file and fast visuals.