Tables.GetRelationships

D

T

The M Code Behind the Power Query M function Tables.GetRelationships

How Tables.GetRelationships() Works

The Tables.GetRelationships() function is part of the M language syntax that is used in Power Query. This function is responsible for extracting all the relationships between tables in a workbook or a data model. The function takes a single argument, which is the name of the table or data model that you want to extract the relationships from. The function then returns a table that contains the following columns:

– Table: The name of the source table that contains the relationship

– Column: The name of the column in the source table that is part of the relationship

– Related Table: The name of the target table that is part of the relationship

– Related Column: The name of the column in the target table that is part of the relationship

Here is an example of how the Tables.GetRelationships() function works:


let

Source = Excel.Workbook(File.Contents("C:MyWorkbook.xlsx"), null, true),

Relationships = Source{[Name="Model"]}[Content]{0}[Tables]{0}[Tables.GetRelationships]()

in

Relationships


In this example, the function extracts all the relationships from the table named “Model” in an Excel workbook located at “C:MyWorkbook.xlsx”. The result of the function is a table that contains the columns described above.

Usage of Tables.GetRelationships()

The Tables.GetRelationships() function is a very useful tool for managing and transforming data in Power Query. Here are some of the common use cases for this function:

1. Understanding the Data Model

When working with a complex data model in Power BI, it is often important to understand the relationships between tables. The Tables.GetRelationships() function can be used to extract all the relationships in a data model and present them in a clear and concise format. This information can be used to gain a deeper understanding of the data model and identify potential issues or improvements.

2. Creating Calculated Columns and Measures

The Tables.GetRelationships() function can also be used to create calculated columns and measures in Power BI. Calculated columns and measures often require information about the relationships between tables, and the Tables.GetRelationships() function can be used to extract this information and use it in calculations. This can improve the accuracy and performance of calculated columns and measures.

3. Data Cleansing and Transformation

The Tables.GetRelationships() function can also be used for data cleansing and transformation in Power Query. For example, you may want to identify all the tables in a data model that are not related to any other tables. This information can be extracted using the Tables.GetRelationships() function and used to identify redundant tables that can be removed from the data model.

The Tables.GetRelationships() function is a powerful tool for managing and transforming data in Power Query. It is used to extract the relationships between tables in a workbook or data model and can be used for a variety of purposes, such as understanding the data model, creating calculated columns and measures, and data cleansing and transformation. The M code behind the Tables.GetRelationships() function is simple and easy to understand, and it can be modified to suit the specific needs of your data analysis tasks.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)