Table.Keys

Table

Returns a list of key records defined on the table type.

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

Syntax

Table.Keys(table as table) as list

Parameters

NameTypeRequiredDescription
tabletableYesThe table whose key metadata is returned.

Return Value

listA list of records, each describing a key defined on the table.

Remarks

Table.Keys returns a list of records describing the key declarations on a table's type metadata. Each element has two fields: - Columns: a list of column names forming the key. - Primary: true if the key is a primary key, false for candidate (non-primary) keys.

Keys are typically declared using Table.AddKey or inherited from a data source that exposes primary key metadata (such as SQL Server or certain OData sources). If no keys are declared, Table.Keys returns an empty list {}.

This function is useful for introspection — for example, confirming that Table.AddKey registered correctly, or inspecting which columns a connector has identified as primary keys. The returned list can be traversed programmatically to extract column names or filter for primary keys only.

Examples

Example 1: Inspect keys on a table with no keys declared

Table.Keys(
    #table({"CustomerID", "Name", "City"},
           {{1,"Alice","New York"},{2,"Bob","Chicago"}})
)
Result
Result
1{}

Example 2: Inspect keys after declaring a primary key

let
    Customers = #table(
        {"CustomerID", "Name", "Email"},
        {{1,"Alice","alice@example.com"},{2,"Bob","bob@example.com"}}
    ),
    WithKey = Table.AddKey(Customers, {"CustomerID"}, true)
in
    Table.Keys(WithKey)
Result
Result
1{[Columns = {"CustomerID"}, Primary = true]}

Example 3: Declare both a primary and a non-primary key, then inspect

let
    Products = #table(
        {"ProductID","ProductName","Category"},
        {{1,"Widget A","Widgets"},{2,"Gadget B","Gadgets"}}
    ),
    Step1 = Table.AddKey(Products, {"ProductID"}, true),
    Step2 = Table.AddKey(Step1, {"ProductName"}, false)
in
    Table.Keys(Step2)
Result
Result
1{[Columns = {"ProductID"}, Primary = true], [Columns = {"ProductName"}, Primary = false]}

Compatibility

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