Table.AddKey

D

T

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

What is a Key Column?

A key column is a column in a table that contains unique values for each row. It can be used to identify and combine related data across different tables. A key column is often created by concatenating other columns in the table to create a unique identifier for each row.

The Table.AddKey Function

The Table.AddKey function is a Power Query M function that adds a key column to a table. It takes two arguments: the first argument is the table to which the key column is to be added, and the second argument is a list of columns to use for creating the key column.

The syntax for the Table.AddKey function is as follows:


Table.AddKey(table as table, keyColumns as list, optional name as nullable text)


The first argument, `table`, is the table to which the key column is to be added. The second argument, `keyColumns`, is a list of columns to use for creating the key column. The third argument, `name`, is optional and is used to specify the name of the key column. If not specified, the name of the key column will be “Key”.

Example Usage

Let’s consider an example to understand the usage of the Table.AddKey function. Suppose we have a table named “Sales” with the following columns:

– Product

– Region

– Sales Amount

We want to add a key column to this table using the columns “Product” and “Region”. We can use the following M code to achieve this:


let

Source = Excel.CurrentWorkbook(){[Name="Sales"]}[Content],

#"Added Custom" = Table.AddKey(Source, {"Product", "Region"}, "SalesKey")

in

#"Added Custom"


In the above M code, we first define a variable `Source` that contains the table “Sales”. We then use the Table.AddKey function to add a key column to this table using the columns “Product” and “Region”. We also specify the name of the key column as “SalesKey”. Finally, we return the updated table using the “in” keyword.

The Table.AddKey function is a powerful tool in Power Query that allows you to add key columns to tables. It is a useful function for creating unique identifiers for each row in a table, which can be used for joining related data across different tables. By understanding the M code behind this function, you can leverage this functionality to enhance your Power Query workflows and make data processing more efficient.

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)