Table.ReplaceRelationshipIdentity

D

T

The M Code Behind the Power Query M function Table.ReplaceRelationshipIdentity

Understanding Relationships in Power Query

Before we dive into the M code behind the Table.ReplaceRelationshipIdentity function, it is important to understand relationships in Power Query. In Power Query, relationships are used to connect tables based on a common column. For example, if you have a table of customers and a table of orders, you can create a relationship between the two tables based on the customer ID column. This allows you to create queries that combine data from both tables.

What is the Table.ReplaceRelationshipIdentity Function?

The Table.ReplaceRelationshipIdentity function is used to replace the identity of a relationship in a table. In Power Query, every relationship has a unique identity. This identity is used to identify the relationship when it is used in a query. Sometimes, it is necessary to replace the identity of a relationship. This can happen when you are creating a new table that has the same structure as an existing table, but with different data. In this case, you need to replace the identity of the relationship in order to connect the new table to the existing table.

The M Code Behind the Table.ReplaceRelationshipIdentity Function

The M code behind the Table.ReplaceRelationshipIdentity function is relatively simple. The function takes two arguments: the table that contains the relationship that you want to replace and the new relationship that you want to use. The M code for the function looks like this:


Table.ReplaceRelationshipIdentity(table as table, oldRelation as any, newRelation as any) as table


Let’s break down each part of the code:

– `Table.ReplaceRelationshipIdentity` is the name of the function.

– `table as table` is the first argument of the function. This is the table that contains the relationship that you want to replace.

– `oldRelation as any` is the second argument of the function. This is the old relationship that you want to replace.

– `newRelation as any` is the third argument of the function. This is the new relationship that you want to use.

– `as table` at the end of the code is the return type of the function. This tells Power Query that the function will return a table.

Examples of the Table.ReplaceRelationshipIdentity Function

Let’s look at a few examples of how the Table.ReplaceRelationshipIdentity function can be used.

Example 1: Replace the Identity of a Relationship in a Table

Suppose you have a table called “Orders” that has a relationship with another table called “Customers”. The relationship is based on the “CustomerID” column. Now, you want to create a new table called “NewOrders” that has the same structure as “Orders”, but with different data. You need to replace the identity of the relationship in “NewOrders” in order to connect it to “Customers”. Here’s the M code for this scenario:


let

Source = Orders,

NewOrders = Table.ReplaceRelationshipIdentity(Source, {"Orders", [CustomerID]}, {"Customers", [CustomerID]})

in

NewOrders


In this code, “Source” is the original “Orders” table. The “Table.ReplaceRelationshipIdentity” function is used to replace the identity of the relationship in “Orders”. The old relationship is identified as {“Orders”, [CustomerID]} and the new relationship is identified as {“Customers”, [CustomerID]}. The resulting table is called “NewOrders”.

Example 2: Replace the Identity of a Relationship in Multiple Tables

Suppose you have multiple tables that have relationships with the “Customers” table. You want to replace the identity of the relationship in all these tables. Here’s the M code for this scenario:


let

TablesToModify = {"Orders", "Sales", "Returns"},

ModifyTables = List.Transform(TablesToModify, each Table.ReplaceRelationshipIdentity(_, {"Orders", [CustomerID]}, {"Customers", [CustomerID]}))

in

ModifyTables


In this code, “TablesToModify” is a list of the tables that you want to modify. The “List.Transform” function is used to apply the “Table.ReplaceRelationshipIdentity” function to each table in the list. The resulting tables are returned as a list.

The Table.ReplaceRelationshipIdentity function is a powerful tool for replacing the identity of a relationship in a table. With simple M code, you can easily modify relationships in Power Query. Whether you are replacing the identity of a single relationship or multiple relationships, the Table.ReplaceRelationshipIdentity function is an essential tool for any Power Query user.

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)