Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query Table.AddKey: Guide to Defining Table Keys

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

The Table.AddKey function in Power Query (M language) is used to define one or more columns as the primary key of a table. This metadata assignment tells the Power Query engine which column uniquely identifies each row, which is essential for optimizing joins, merges, and maintaining data integrity when loading data into models like Power BI or Analysis Services. To use it, you must manually enter the function into the Formula Bar or the Advanced Editor, as there is no button in the Power Query ribbon to perform this specific action.

What You'll Need

  • Microsoft Excel or Power BI Desktop with Power Query installed.
  • A table already loaded into the Power Query Editor.
  • A column (or set of columns) that contains unique values with no duplicates and no nulls.
  • Access to the Formula Bar (View > Formula Bar) or the Advanced Editor.

Step-by-Step

  1. Identify Your Unique Column: Before applying the function, scan your data to ensure the column you intend to set as the key contains truly unique values. If there are duplicates, the function will still apply the metadata, but downstream operations relying on that key may produce incorrect results or errors.
  2. Open the Power Query Editor: Load your data source into the editor. Ensure you have performed all necessary cleaning steps (such as removing duplicates or filtering out nulls) before defining the key, as the key definition is a metadata layer applied to the current state of the table.
  3. Enable the Formula Bar: If you do not see the formula bar at the top of the preview window, go to the View tab in the ribbon and check the Formula Bar box. This allows you to wrap your existing steps in the Table.AddKey function.
  4. Insert a Custom Step: Click the fx icon to the left of the formula bar. This creates a new step in the Applied Steps list. By default, it will look like = PreviousStepName.
  5. Apply the Table.AddKey Syntax: Replace the default text in the formula bar with the following syntax: = Table.AddKey(PreviousStepName, {"ColumnName"}). Replace PreviousStepName with the name of the step immediately preceding this one (usually found in the Applied Steps pane) and "ColumnName" with the exact name of your key column, enclosed in double quotes.
  6. Defining a Composite Key: If your unique identifier is spread across multiple columns, list them all within the curly braces, separated by commas. For example: = Table.AddKey(PreviousStepName, {"OrderID", "ProductID"}). This tells Power Query that the combination of these two columns creates a unique record.
  7. Verify the Application: Once you press Enter, the table preview will not look different because Table.AddKey modifies the metadata of the table rather than the visible data. To verify, you can check the table properties in the Advanced Editor to ensure the step is recorded correctly.
  8. Close and Apply: Click Close & Apply in the Home tab to load the table into your data model with the key metadata intact.

Common Mistakes

One of the most frequent errors is attempting to use Table.AddKey on a column that contains null values. In database logic, a primary key cannot be null. While Power Query might not throw an immediate error during the step application, this will lead to failures when the data is loaded into a relational model or used in specific merge operations that expect a strict key.

Another common mistake is case sensitivity. Power Query is case-sensitive; if your column is named "CustomerID" and you write {"customerid"} in the function, the query will return an error stating that the column was not found. Always copy the column name exactly as it appears in the header.

Users often confuse Table.AddKey with adding an Index Column. Adding an Index Column creates a new physical column of numbers (1, 2, 3...). Table.AddKey does not create a new column; it simply flags existing columns as the identifier for the table. If you don't have a unique column, you must first use Add Column > Index Column and then apply Table.AddKey to that new index column.

Lastly, applying the key too early in the transformation process can be problematic. If you define the key and then later perform a step that introduces duplicates (like a merge that creates a many-to-many relationship), the key metadata becomes logically invalid, even if the step remains in the list.

Tips

For maximum performance, always apply Table.AddKey as one of the final steps in your query. This ensures that all data cleaning, filtering, and transformations are complete, and the key is assigned to the final, polished version of the dataset.

When working with very large datasets, defining keys can help the Mashup Engine optimize how it handles joins. By explicitly stating the key, you provide a hint to the engine about the cardinality of the data, which can occasionally reduce the memory overhead during complex merge operations.

If you are building a star schema in Power BI, using Table.AddKey on your Dimension tables (the "lookup" tables) is a best practice. This ensures that the one-to-many relationship is clearly defined from the source, reducing the chance of "Many-to-Many" relationship warnings during the modeling phase.

Combine this function with Table.Distinct. To be absolutely certain your key is valid, you can create a step using Table.Distinct(PreviousStep, {"YourKeyColumn"}) immediately before the Table.AddKey step. This guarantees that no duplicates exist before the metadata is applied.

Troubleshooting

If you receive an Expression.Error: The column "ColumnName" of the table wasn't found, double-check the spelling and capitalization of your column name. Ensure that no previous steps renamed the column. If a step like "Renamed Columns" exists after your Table.AddKey step, it may cause confusion, but the error usually occurs because the column name used in the function does not exist in the table provided by the PreviousStepName.

If the query runs but you see no change in the data, remember that Table.AddKey is a metadata operation. It does not add a column, change a color, or sort the data. To see if it worked, you must look at the M code in the Advanced Editor. If the code is syntactically correct and the step is listed, the key has been assigned.

If you encounter a performance drop after adding a key, try moving the step. In some rare cases, forcing a key definition can interfere with "Query Folding" (the ability of Power Query to push the work back to the SQL server). If you notice that the query is suddenly downloading millions of rows instead of filtering them on the server, remove the Table.AddKey step to see if folding is restored.

If you get a syntax error regarding the curly braces, ensure you are using {} and not (). The second argument of Table.AddKey must be a list of text values, and in M, lists are always defined by curly braces.

Frequently Asked Questions

Does Table.AddKey remove duplicates automatically?

No. Table.AddKey only assigns a property to the column. It does not check for or remove duplicates. You must use Table.Distinct or the "Remove Duplicates" button in the UI before applying the key.

Can I have more than one key in a table?

A table can only have one primary key. However, that key can be a "composite key," meaning it consists of multiple columns. You achieve this by listing multiple column names inside the curly braces.

Is Table.AddKey necessary for Power BI relationships?

It is not strictly required because Power BI allows you to define relationships in the Model view. However, defining it in Power Query is a cleaner approach for documentation and can assist the engine in certain optimization scenarios.

What is the difference between a Key and an Index?

An Index is a physical column of incrementing numbers added to the data. A Key is a piece of metadata that tells the system, "This column (or these columns) uniquely identifies each row."

Does this function work with all data sources?

Yes, Table.AddKey works on any table object within Power Query, regardless of whether the source is a CSV, Excel file, SQL database, or Web API.