Type.TableKeys
TypeReturns the list of key definitions for a table type.
Syntax
Type.TableKeys(type as type) as listParameters
| Name | Type | Required | Description |
|---|---|---|---|
type | type | Yes | A table type whose key definitions are returned. |
Return Value
list — A 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
- Primary — true 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)The final output — counts the keys in the list, returning 0 because no keys are defined.
Result | |
|---|---|
| 1 | 0 |
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}The final output — accesses the first key record, showing its Columns list and Primary flag.
Columns | Primary | |
|---|---|---|
| 1 | ID | TRUE |
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
HasPrimaryThe final output — returns true because the table has a primary key declared.
Result | |
|---|---|
| 1 | TRUE |