Type.AddTableKey

D

T

The M Code Behind the Power Query M function Type.AddTableKey

Among the many M functions available in Power Query, the Type.AddTableKey function is one of the most important and widely used. In this article, we will explore the M code behind the Type.AddTableKey function and show how it can be used to create effective data transformations.

What is the Type.AddTableKey function?

The Type.AddTableKey function is a Power Query M function that is used to create a table key for a given table. A table key is a unique identifier for each row in a table that helps to ensure data integrity and accuracy. It is usually created by combining one or more columns in the table to create a unique value for each row.

The Type.AddTableKey function takes two arguments: the table to which the key will be added, and the list of column names that will be used to create the key. Here is an example of how the function is used:


let

Source = Table.FromRecords({

[ID = 1, Name = "John", Age = 30],

[ID = 2, Name = "Jane", Age = 25],

[ID = 3, Name = "Bob", Age = 40]

}),

AddKey = Table.AddTableKey(Source, {"ID"})

in

AddKey


In this example, we create a table called “Source” with three columns: ID, Name, and Age. We then use the Type.AddTableKey function to add a key to the table based on the ID column. The resulting table will have an additional column called “__Key” that contains the unique identifier for each row.

How does the Type.AddTableKey function work?

The Type.AddTableKey function works by creating a new column in the table that contains a unique identifier for each row based on the specified columns. The function first checks to make sure that the specified columns do not contain any null or empty values, as these would cause errors in the key generation process.

Next, the function creates a new column in the table by combining the values of the specified columns into a single string. This string is then hashed using a cryptographic algorithm to create a unique identifier for each row. The resulting hash value is stored in the new “__Key” column.

Why is the Type.AddTableKey function useful?

The Type.AddTableKey function is useful for several reasons. First, it helps to ensure data integrity and accuracy by creating a unique identifier for each row in the table. This makes it easier to track changes to the data over time and to identify any potential errors or inconsistencies.

Second, the Type.AddTableKey function can be used to join tables together based on their keys. By creating a common key between two tables, it is possible to combine them into a single table and perform further analysis and transformation.

Finally, the Type.AddTableKey function can be used to improve the performance of certain operations in Power Query. For example, when sorting or filtering a large table, using a key can significantly reduce the amount of time required to complete the operation.

Tips for using the Type.AddTableKey function

Here are some tips for using the Type.AddTableKey function effectively:

– Always ensure that the specified columns do not contain any null or empty values, as this will cause errors in the key generation process.

– Consider using multiple columns to create the key if a single column does not provide enough uniqueness.

– Avoid using columns that are likely to change frequently, such as dates or other volatile data, as this can cause the key to become outdated.

– When joining tables based on their keys, make sure that the keys have the same data type and format.

The Type.AddTableKey function is a powerful M function in Power Query that can be used to create table keys for improved data integrity and accuracy. By understanding how this function works and following some best practices for its use, you can create effective data transformations and extract valuable insights from your data.

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)