Table.ReplaceKeys
TableReplaces all keys on a table with a new set of key definitions.
Syntax
Table.ReplaceKeys(table as table, keys as list) as tableParameters
| Name | Type | Required | Description |
|---|---|---|---|
table | table | Yes | The table whose keys will be replaced. |
keys | list | Yes | A list of key records, each with Columns (list of column names) and Primary (boolean). |
Return Value
table — A table with its keys replaced by the specified key definitions.
Remarks
Table.ReplaceKeys replaces all existing keys on a table with a new set of key definitions. Unlike Table.AddKey, which appends a single key, this function replaces the entire key set at once.
Each key in the keys list is a record with two fields:
- Columns — a list of column names that make up the key (e.g.,
{"OrderID"}or{"CustomerID", "ProductID"}for a composite key). - Primary — a boolean indicating whether the key is a primary key (
true) or a non-primary/alternate key (false).
Keys are metadata attached to the table value. They inform the Power Query engine about uniqueness constraints, which can improve join performance and enable certain optimizations. Use Table.Keys to inspect the keys currently defined on a table.
Examples
Example 1: Replace a primary key with a non-primary key
let
Source = Table.FromRecords({
[Id = 1, Name = "Hello There"],
[Id = 2, Name = "Good Bye"]
}),
WithPrimary = Table.AddKey(Source, {"Id"}, true),
Demoted = Table.ReplaceKeys(WithPrimary, {[Columns = {"Id"}, Primary = false]})
in
Table.Keys(Demoted)Example 2: Define a composite key
let
Keyed = Table.ReplaceKeys(
Sales,
{[Columns = {"OrderID", "Product"}, Primary = true]}
)
in
Table.Keys(Keyed)Compatibility
✓ Power BI Desktop✓ Power BI Service✓ Excel Desktop✓ Excel Online✓ Dataflows✓ Fabric Notebooks