Table.AddKey

Updated on

Table.AddKey is a Power Query M function that adds a key to a table using a list of column names that define the key, and specifies whether the key is primary. The function returns a new table with the added key.

Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365

Syntax

Table.AddKey(
   table as table,
   columns as list,
   isPrimary as logical,
) as table

Description

Table.AddKey adds a key to table. The key is defined by a list of column names in columns, and isPrimary indicates whether the key is primary.

Examples

Add a single-column primary key to a table.

let
    table = Table.FromRecords( {
        [Id = 1, Name = "Hello There"],
        [Id = 2, Name = "Good Bye"]
    } ),
    resultTable = Table.AddKey( table, {"Id"}, true )
in
    resultTable

 /* Output: 
Table.FromRecords( {
    [Id = 1, Name = "Hello There"],
    [Id = 2, Name = "Good Bye"]
} )
 */ 

Other functions related to Table.AddKey are:

Contribute » | Contributors: Rick de Groot
Microsoft documentation: https://learn.microsoft.com/en-us/powerquery-m/table-addkey