Type.AddTableKey

Type

Returns a new table type with an additional key definition added.

Examples on this page use shared sample tables. View them to understand the input data before reading the examples below.

Syntax

Type.AddTableKey(type as type, columns as list, isPrimary as logical) as type

Parameters

NameTypeRequiredDescription
typetypeYesThe base table type to add a key to.
columnslistYesA list of column name text values that form the key.
isPrimarylogicalYesTrue if this is the primary key; false for an alternate key.

Return Value

typeA new table type with the specified key added.

Remarks

Type.AddTableKey adds a key definition to a table type, returning a new type value. Keys identify the column or columns whose values uniquely identify each row — analogous to primary and unique keys in relational databases. The isPrimary flag distinguishes the primary key (true) from alternate keys (false). A table type can have at most one primary key but multiple alternate keys.

This function operates on types, not tables. It returns a new type — the original type is unchanged and no data is modified. To apply the updated type to an actual table, use Value.ReplaceType(table, newType). Alternatively, Table.AddKey applies a key to a table directly and handles the type plumbing for you.

Key declarations matter most in Power BI Desktop, where they are used by the engine to manage relationships and can improve query folding. In Fabric and dataflows, they inform the connector layer about uniqueness constraints.

The result of Type.AddTableKey accumulates on top of any existing keys — it does not replace them. To replace all keys at once, use Type.ReplaceTableKeys. To inspect current keys, use Type.TableKeys.

Examples

Example 1: Add a primary key on a single column and verify it

let
    T = #table(type table [ID = number, Name = text], {{1, "Alice"}, {2, "Bob"}}),
    NewType = Type.AddTableKey(Value.Type(T), {"ID"}, true),
    Keys = Type.TableKeys(NewType)
in
    Keys{0}[Primary]
Applied Steps

The final output — reads the Primary field of the first key to confirm it is true.

Result
1TRUE

Example 2: Add a composite primary key and inspect its columns

let
    T = #table(type table [Year = number, Month = number, Value = number], {}),
    NewType = Type.AddTableKey(Value.Type(T), {"Year", "Month"}, true),
    Keys = Type.TableKeys(NewType)
in
    Keys{0}[Columns]
Applied Steps

The final output — reads the Columns field of the first key to confirm it contains both Year and Month.

Result
1Year,Month

Example 3: Apply a primary key to a table using Value.ReplaceType

let
    T = #table(type table [OrderID = number, Product = text], {{1, "Widget A"}, {2, "Widget B"}}),
    TypeWithKey = Type.AddTableKey(Value.Type(T), {"OrderID"}, true),
    TWithKey = Value.ReplaceType(T, TypeWithKey),
    Keys = Type.TableKeys(Value.Type(TWithKey))
in
    Keys{0}[Columns]
Applied Steps

The final output — reads the Columns field of the first key to confirm it contains OrderID.

Result
1OrderID

Compatibility

Power BI Desktop Power BI Service Excel Desktop Excel Online Dataflows Fabric Notebooks