Table.AddKey

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

Adds a key to table, where columns is the list of column names that define the key, and isPrimary specifies 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:

BI Gorilla Blog

Last update: August 28, 2023 | Contribute » | Contributors: Rick de Groot
Microsoft documentation: https://learn.microsoft.com/en-us/powerquery-m/table-addkey
© 2023 BI Gorilla. All rights reserved. Content derived from Microsoft documentation is property of Microsoft Corp.