Table relationships play an important role in Power BI

By Riya Jain & Abhinash Jena on September 3, 2024

Understanding table relationships is fundamental to creating effective and accurate data models in Power BI, or any data analysis tool that supports DAX (Data Analysis Expressions). Relationships define how tables in a data model interact with each other.

Relationships are critical for aggregating data correctly across multiple tables. Furthermore, relationships enable complex calculations that depend on data spread across tables. A previous article discussed integrating multiple datasets by appending and merging. This lays the foundation for the current article, which focuses on different DAX functions that depend on data integration from various tables.

TIP

Improperly defined relationships can lead to ambiguity, circular references, or incorrect calculations.

In Power BI, table relationships are typically established using common keys, such as IDs. However, there are scenarios where dynamically adjusted relationships or custom filtering between related tables are needed.

Key concepts in table relationships

Before diving into the importance and use cases of table relationships, it’s essential to understand a few key concepts:

  • Primary Key and Foreign Key: In a relational data model, the primary key is a unique identifier in a table, while a foreign key in another table links to the primary key, establishing a relationship.
EXAMPLE

CustomerID in a customer's table and CustomerID in a sales transactions table.

  • One-to-Many Relationship: The most common type of relationship where one record in a primary table can relate to many records in a related table.
  • Many-to-Many Relationship: A more complex relationship where multiple records in one table can relate to various records in another table. These require a bridge table or are handled using DAX.
  • Cardinality: The nature of the relationship between two tables, describing how many rows in one table correspond to rows in another table.
  • Directionality of Relationships: Relationships can be unidirectional where the data flows from one table to another or bidirectional where the data flows in both directions between tables.

Filtering and Cross-Filtering across tables

Filtering and cross-filtering in Power BI and DAX are powerful mechanisms that determine how data is filtered and propagated across related tables.

  • Filter Context: The filter context refers to the filters applied to a data model at a given point in time. This can include filters set by slicers, rows, columns, or other functions. Filter context influences the result of DAX expressions.
  • Cross Filter: Cross-filtering occurs when filters applied to one table automatically filter related tables. The direction of the relationship between tables (single or bidirectional) determines how the filter context flows between them.
  • Single Direction Filtering: In single-direction filtering, the filter context flows in one direction—from the primary table to the related table.
  • Bidirectional Filtering: Bidirectional filtering allows the filter context to flow in both directions between related tables. This is often used in more complex models and can lead to circular dependencies if not managed carefully.

DAX offers several functions to manipulate and control filtering behaviour across tables. The FILTER function allows to creation of a filter context for a table based on a specified condition.

FILTER(<table>, <expression>)
EXAMPLE

Using the sample file, create a filter context and then calculate the total sales from the category “Electronics”.

ElectronicsSales = CALCULATE(SUM(transations[TotalSales]),
    FILTER(Products, Products[Category] = "Electronics")
)

Similarly, add a new column in transactions to calculate the percentage of total sales for each product, ignoring the Category filter use this equation:

SalesPct = DIVIDE(SUM(transactions[TotalSales]),
    CALCULATE(SUM(transactions[TotalSales]), ALL(Products[Category]))
)

Here, ALL(Products[Category]) removes any filters applied to the Category column, calculating the total sales across all categories for comparison.

Furthermore, the CROSSFILTER function in DAX is used to change the direction of cross-filtering between two related tables. When this filter is applied to one visual, other visuals connected to the same dataset are automatically filtered based on that selection. It is useful when bidirectional relationships need to be temporarily changed to single-direction for specific calculations. The function returns no value but only sets the cross-filtering direction for the indicated relationships.

CROSSFILTER (<column1>, <column2>, <direction>)
Direction can be None, OneWay or Both
EXAMPLE

The transactions table and the customers table in the sample file is related by CustomerID and transactions[ProductID] is related to products[ProductID]. However, by default, the filter flows from products to transactions, you wouldn’t be able to filter customers by product category directly. To add a new column in the customers table and fill it with the total sales for specific customers that purchased “Electronics” use CROSSFILTER.

SalesByElectronics = 
CALCULATE(
    SUM(transactions[TotalSales]),
    CROSSFILTER(transactions[CustomerID], customers[CustomerID], BOTH),
    products[Category] = "Electronics"
)

In this example, CROSSFILTER changes the relationship to bidirectional, allowing filters to flow from transactions to customers and vice versa.

Furthermore, to calculate the total sales regardless of any customer-related filters. By using CROSSFILTER with the NONE option, you can disable the filtering effect of the relationship.

TotalSalesWithoutCustomerFilter = 
CALCULATE(
    SUM(transactions[TotalSales]),
    CROSSFILTER(transactions[CustomerID], customers[CustomerID], NONE)
)

This measure calculates the total sales without considering any filters that might be applied to the customers table.

RELATED and RELATEDTABLE functions

The RELATED function is used to retrieve values from a related table based on the existing relationships. This function is particularly useful to bring in additional data from a related table into the primary table. While the RELATED function retrieves a single value from a related table in a one-to-many relationship, RELATEDTABLE retrieves a table of related rows.

EXAMPLE

To create a calculated column in the transactions table that shows the customer’s region the following equation can be used:

Region = RELATED(customers[Region])

This uses RELATED to pull the Region value from the customers table into the transactions table.

The RELATEDTABLE function is beneficial for operations that require working with a set of related rows. The function is used for returning a table of values. The RELATEDTABLE function changes the context in which data is filtered and evaluates the expression in the newly specified context.

EXAMPLE

In the sample file create a new column in customers table to calculate the total number of orders placed by each customer.

TotalOrders = COUNTROWS(RELATEDTABLE(transactions))

Here, RELATEDTABLE(transactions) returns all records from the transactions table related to each customer, and COUNTROWS counts them.

Best practices for managing complex table relationships

  • Document Relationships: Use single-direction filters for simplicity, and bidirectional filters only when necessary. Keep documentation of your data model’s relationships to help in troubleshooting and understanding how data flows between tables.
  • Create a Schema: Organize your data model into fact and dimension tables, with one-to-many relationships between them. This simplifies relationships and improves performance.
  • Use Active Relationships: Ensure that only one relationship is active between any two tables, avoiding ambiguity.
  • Leverage Bridge Tables: For many-to-many relationships, use bridge tables to manage relationships effectively.
  • Regularly Review Relationships: As your data model evolves, review and adjust relationships to ensure they remain accurate and relevant.
NOTES

I work as an editor and writer for Project Guru. I have a keen interest in new and upcoming learning and teaching methods. I have worked on numerous scholarly projects in the fields of management, marketing and humanities in the last 10 years. Currently, I am working in the footsteps of the National Education Policy of India to help and support fellow professors to emphasise interdisciplinary research and curriculum design.

I am a Senior Analyst at Project Guru, a research and analytics firm based in Gurugram since 2012. I hold a master’s degree in economics from Amity University (2019). Over 4 years, I have worked on worked on various research projects using a range of research tools like SPSS, STATA, VOSViewer, Python, EVIEWS, and NVIVO. My core strength lies in data analysis related to Economics, Accounting, and Financial Management fields.

Discuss