๐ Relationships and Filter Flow
๐ Connecting the Dots: Relationships
In the previous chapter, we learned about placing our tables into a Star Schema. But how do we actually connect them? We use Relationships.
A relationship is a line drawn between a column in one table and a matching column in another table (e.g., connecting ProductID in the Products table to ProductID in the Sales table).
๐งฎ Cardinality
When you connect two tables, Power BI evaluates the uniqueness of the columns and assigns a Cardinality. This defines the nature of the relationship.
- One-to-Many (1:*) ๐ฒ
- The gold standard. A single Product ID appears only once in the Products table (the "One" side), but it can appear many times in the Sales table (the "Many" side) because a product can be sold thousands of times.
- One-to-One (1:1) 1๏ธโฃ
- Both tables have entirely unique IDs. Usually means you should just merge these two tables together into a single table.
- Many-to-One (*:1) โช
- The exact same as One-to-Many, just drawn in the opposite direction.
- Many-to-Many (:) ๐ธ๏ธ
- Danger! Both columns contain duplicate values. Power BI doesn't know which row connects to which row.
- Solution: Avoid this if possible. If required, you must build a "Bridge Table" containing unique values to sit between them.
๐ Relationship Behaviour: Filter Flow
When you draw a relationship line in Power BI, you will notice a tiny arrow in the middle of the line. This arrow dictates the Cross-Filter Direction.
Think of relationships like a river. Water (filters) only flows downstream in the direction of the arrow.
Single Cross-Filter Direction (โฌ๏ธ) The arrow points from the Dimension table (the "One" side) down to the Fact table (the "Many" side).
- If you filter your report to only show "Red" products, that filter flows down the line to the Sales table, instantly calculating the total sales for Red products.
- Best Practice: Always arrange your Dimension tables physically above your Fact tables on the screen so you can visually see the filters flowing downwards like a waterfall!
Bidirectional Filtering (โ๏ธ) The arrow points in both directions. Filtering Table A filters Table B, and filtering Table B also filters Table A.
- Warning: While this sounds great, it can cause severe performance issues and "Relationship Ambiguity" (Power BI gets confused about which path a filter should take). Avoid using this unless strictly necessary!
๐ข Active vs ๐ด Inactive Relationships
Sometimes, two tables share multiple matching columns. For example, a Sales table might have an OrderDate and a ShippingDate. You want to connect both of them to your Calendar table.
- Power BI only allows one Active relationship (a solid line) between two tables at a time. This is the default path filters will take.
- Any additional relationships between those tables become Inactive (a dashed line). Filters will ignore this line unless you explicitly wake it up using a DAX formula (specifically, the
USERELATIONSHIP()function).
โ Data Modelling Best Practices
Follow these golden rules to keep your Power BI model healthy and fast:
- Prefer the Star Schema: Keep it simple. Dimensions surrounding a central Fact table.
- Separate Fact and Dimension Tables: Never mix descriptive text attributes (like Customer Name) into your massive Fact tables (like Sales). Keep them physically separate.
- Prefer One-to-Many (1:*) Relationships: They are the fastest and most reliable.
- Prefer Single-Direction Filters: Let filters flow downstream from Dimensions to Facts.
- Hide Technical Fields: Right-click and hide Foreign Key columns (like
CustomerID) in your Fact table. Force your users to drag theCustomer Namefrom the Dimension table instead!
๐๏ธ Practice Drill
Let's create a relationship!
Task:
// Try answering these:
1. In the Power BI Desktop **Model View**, if you have two tables that share a common column (like an ID), click and hold the column name in the first table.
2. Drag your mouse over to the matching column name in the second table and let go.
3. Power BI will draw a line between them!
4. Double-click the line to open the **Edit Relationship** menu. Notice the Cardinality and Cross-filter direction options.
๐ก Click for Solutions
Did the line generate an arrow? Does it point from the 1 side to the * (Many) side? If you double-click the line, you can manually override the cross-filter direction, but it is highly recommended to leave it as Single!
โ ๐ Data Modelling and Schemas | Next Topic โ ๐ Core Data Visualizations