Relationships (one-to-many, many-to-many) in Power BI - Real Business Scenario
| Sales | ||||
|---|---|---|---|---|
| OrderID | ProductID | CustomerID | Quantity | SalesAmount |
| 1001 | 101 | 201 | 2 | 40 |
| 1002 | 102 | 202 | 1 | 25 |
| 1003 | 103 | 203 | 3 | 75 |
| 1004 | 101 | 204 | 1 | 20 |
| 1005 | 104 | 201 | 4 | 100 |
| 1006 | 102 | 202 | 2 | 50 |
| 1007 | 103 | 203 | 1 | 25 |
| 1008 | 104 | 204 | 3 | 75 |
| Products | |
|---|---|
| ProductID | Category |
| 101 | Electronics |
| 102 | Clothing |
| 103 | Clothing |
| 104 | Electronics |
| Customers | |
|---|---|
| CustomerID | Region |
| 201 | North |
| 202 | South |
| 203 | East |
| 204 | West |
Use 'Get Data' to import all tables from your data source.Drag ProductID from Products (one side) to ProductID in Sales (many side). Set relationship as 'Single' direction.Drag CustomerID from Customers (one side) to CustomerID in Sales (many side). Set relationship as 'Single' direction.Total Sales = SUM(Sales[SalesAmount])Add Products[Category] to Rows, Customers[Region] to Columns, and 'Total Sales' measure to Values.Check that sum of all matrix values equals SUM(Sales[SalesAmount]) = 410.Total Sales by Product Category and Customer Region +----------------+--------+--------+--------+--------+--------+ | Category | North | South | East | West | Total | +----------------+--------+--------+--------+--------+--------+ | Electronics | 140 | 0 | 0 | 95 | 235 | | Clothing | 0 | 75 | 100 | 0 | 175 | +----------------+--------+--------+--------+--------+--------+ | Total | 140 | 75 | 100 | 95 | 410 | +----------------+--------+--------+--------+--------+--------+
Create a many-to-many relationship scenario by adding a Promotions table that links multiple products to multiple promotions, then analyze sales by promotion.
Practice
What does a one-to-many relationship in Power BI represent?
Choose the best description.
Solution
Step 1: Understand one-to-many relationship meaning
A one-to-many relationship means one record in the first table connects to multiple records in the second table.Step 2: Match description to options
One record in the first table relates to many records in the second table. correctly describes this connection. Other options describe different or no relationships.Final Answer:
One record in the first table relates to many records in the second table. -> Option DQuick Check:
One-to-many = One record to many records [OK]
- Confusing one-to-many with many-to-one
- Thinking many-to-many is the same as one-to-many
- Assuming no relationship means one-to-many
Which of the following is the correct way to define a many-to-many relationship in Power BI?
Choose the correct syntax or description.
Solution
Step 1: Recall how relationships are defined in Power BI
Relationships are set in the model view by choosing cardinality like 'One to Many' or 'Many to Many'.Step 2: Identify the correct method for many-to-many
Set relationship cardinality to 'Many to Many' between two tables. correctly states setting cardinality to 'Many to Many'. The other options describe incorrect or unsupported methods.Final Answer:
Set relationship cardinality to 'Many to Many' between two tables. -> Option BQuick Check:
Many-to-many relationship = Set cardinality to Many to Many [OK]
- Thinking DAX creates relationships
- Confusing one-to-one with many-to-many
- Trying to link tables with calculated columns
Given two tables: Customers and Orders, with a one-to-many relationship from Customers[CustomerID] to Orders[CustomerID], what will the following DAX measure return?
OrderCount = COUNTROWS(RELATEDTABLE(Orders))
Assuming the measure is used in a table visual showing Customers[CustomerName].
Solution
Step 1: Understand RELATEDTABLE function in one-to-many context
RELATEDTABLE returns all rows from the 'many' side (Orders) related to the current row in the 'one' side (Customers).Step 2: COUNTROWS counts those related rows
So COUNTROWS(RELATEDTABLE(Orders)) counts how many orders belong to each customer.Final Answer:
The number of orders for each customer. -> Option AQuick Check:
RELATEDTABLE + COUNTROWS = count related rows per customer [OK]
- Thinking it counts total orders for all customers
- Confusing RELATEDTABLE with RELATED
- Assuming it returns an error
You created a many-to-many relationship between Products and Sales tables but your report shows incorrect totals. What is the most likely cause?
Solution
Step 1: Understand many-to-many relationship effects
Many-to-many relationships can create ambiguous filter contexts because multiple rows on both sides can match.Step 2: Identify cause of incorrect totals
This ambiguity often causes totals to be incorrect or duplicated, which matches The many-to-many relationship causes ambiguous filter context.Final Answer:
The many-to-many relationship causes ambiguous filter context. -> Option AQuick Check:
Many-to-many can cause ambiguous filters = incorrect totals [OK]
- Assuming inactive relationship is the cause
- Thinking calculated columns fix relationships
- Ignoring data type mismatches
You have three tables: Students, Courses, and Enrollments. Enrollments links Students and Courses with many-to-many relationships. How should you model these relationships in Power BI to correctly analyze which students took which courses?
Solution
Step 1: Understand the role of the Enrollments table
Enrollments acts as a bridge table linking Students and Courses with many-to-many data.Step 2: Model relationships correctly
Best practice is to create two one-to-many relationships: Students (one) to Enrollments (many), and Courses (one) to Enrollments (many). This avoids complex many-to-many direct links.Final Answer:
Create two one-to-many relationships: Students to Enrollments and Courses to Enrollments. -> Option CQuick Check:
Bridge table uses two one-to-many relationships [OK]
- Trying to create many-to-many directly between Students and Courses
- Using one-to-one relationships incorrectly
- Merging tables with calculated columns instead of relationships
Ready to prove it?
Pass the test and this pattern moves unknown
