Type.AddTableKey
TypeReturns a new table type with an additional key definition added.
Syntax
Type.AddTableKey(type as type, columns as list, isPrimary as logical) as typeParameters
| Name | Type | Required | Description |
|---|---|---|---|
type | type | Yes | The base table type to add a key to. |
columns | list | Yes | A list of column name text values that form the key. |
isPrimary | logical | Yes | True if this is the primary key; false for an alternate key. |
Return Value
type — A 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]The final output — reads the Primary field of the first key to confirm it is true.
Result | |
|---|---|
| 1 | TRUE |
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]The final output — reads the Columns field of the first key to confirm it contains both Year and Month.
Result | |
|---|---|
| 1 | Year,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]The final output — reads the Columns field of the first key to confirm it contains OrderID.
Result | |
|---|---|
| 1 | OrderID |