Type.TableKeys

Type

Returns the list of key definitions for a table type.

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

Syntax

Type.TableKeys(type as type) as list

Parameters

NameTypeRequiredDescription
typetypeYesA table type whose key definitions are returned.

Return Value

listA list of key records, each with Columns (list of column names) and Primary (logical) fields.

Remarks

Type.TableKeys returns the list of key definitions declared on a table type. Each element of the returned list is a record with two fields:

- Columns — a list of column name text values that form the key - Primarytrue for the primary key, false for an alternate key

If no keys are defined on the table type, an empty list ({}) is returned.

Keys in Power BI inform the engine about uniqueness constraints on tables and are used to enable and optimize relationships between tables in the data model. They can be added to a table type using Type.AddTableKey, set in bulk using Type.ReplaceTableKeys, or applied directly to a table using Table.AddKey.

Type.TableKeys is the read-side complement to Type.AddTableKey and Type.ReplaceTableKeys. It is useful for inspecting a table's declared schema, validating that expected key constraints are in place, or building generic connector utilities that need to be aware of key definitions.

Examples

Example 1: A table type with no declared keys returns an empty list

let
    T = #table(type table [ID = number, Name = text], {{1, "Alice"}}),
    Keys = Type.TableKeys(Value.Type(T))
in
    List.Count(Keys)
Applied Steps

The final output — counts the keys in the list, returning 0 because no keys are defined.

Result
10

Example 2: Inspect key details after adding a primary key

let
    T = #table(type table [ID = number, Name = text], {{1, "Alice"}}),
    TWithKey = Table.AddKey(T, {"ID"}, true),
    Keys = Type.TableKeys(Value.Type(TWithKey))
in
    Keys{0}
Applied Steps

The final output — accesses the first key record, showing its Columns list and Primary flag.

Columns
Primary
1IDTRUE

Example 3: Check whether a table has a primary key declared

let
    T = #table(type table [OrderID = number, Product = text], {{1, "Widget A"}}),
    TWithKey = Table.AddKey(T, {"OrderID"}, true),
    Keys = Type.TableKeys(Value.Type(TWithKey)),
    HasPrimary = List.AnyTrue(List.Transform(Keys, each _[Primary]))
in
    HasPrimary
Applied Steps

The final output — returns true because the table has a primary key declared.

Result
1TRUE

Compatibility

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