[expression.error] the key didn\t match any rows in the table.

One of the most common errors you’ll see when working with Power Query in Power BI or Excel is this:

Expression.Error: The key didn’t match any rows in the table

[expression.error] the key didnt match any rows in the table.

It can occur with almost any data source and in a wide variety of different circumstances, and for new users of Power Query it can be very confusing. In this post I’ll explain what the error message means and when you’re likely to encounter it using a simple example.

TL;DR You’re probably getting this error because your Power Query query is trying to connect to a table or worksheet or something in your data source that has been deleted or been renamed.

Say you have an Excel workbook with three worksheets in called Sheet1, Sheet2 and Sheet3:

[expression.error] the key didnt match any rows in the table.

On Sheet1 there’s some data you want to load into Power BI or Excel using Power Query:

[expression.error] the key didnt match any rows in the table.

It’s very easy to do this: after you click on the Excel data source and select your Excel workbook, you see the Navigator window with the contents of the workbook and select Sheet1:

[expression.error] the key didnt match any rows in the table.

…click OK and your query is ready:

[expression.error] the key didnt match any rows in the table.

What actually happens in this query though? If you click on each of the four steps in the Applied Steps pane on the right-hand side of the screen, you’ll see what each step does. In particular, notice that the first step in the query (called Source) returns a table with one row for each worksheet, table and named range in the workbook:

[expression.error] the key didnt match any rows in the table.

This is something that happens with many types of data source: after you have connected you have to choose what data inside the data source you want to connect to in the first step, and the list of things you can connect to is returned in a table. This table lists the same things displayed in the Navigator dialog shown above. In the same way, if you connect to a SQL Server database this first step returns a table with all the tables and views in the database; if you connect to an OData feed the first step shows all the resources available through the feed.

The other thing to notice in the table in the screenshot is the Data column, which contains nested table values. If you were to click on one of these nested tables you would navigate to the data in the worksheet named on that row and that is in fact what the second step in the query does.

Here’s the M code for the whole query:

let Source = Excel.Workbook( File.Contents( "C:\MyExcelFile.xlsx"), null, true), Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data], #"Promoted Headers" = Table.PromoteHeaders( Sheet1_Sheet, [PromoteAllScalars=true] ), #"Changed Type" = Table.TransformColumnTypes( #"Promoted Headers", {{"Month", type text}, {"Sales", Int64.Type}} ) in #"Changed Type"

If you want to understand how the code in the second step of this query gets the contents of the cell containing the nested table, I have a blog post here that goes into a lot more detail. Basically the second step (called Sheet1_Sheet in the code, but shown as Navigation in the Applied Steps pane) returns the nested table in the Data column from the row in the table where the Item column contains the value “Sheet1” and the Kind column contains the value “Sheet”. It’s the contents of the Item and Kind columns that are used to identify the row in the table that contains the data you want to see – in database terminology these columns are the key columns on the table.

What happens if you change the name of the worksheet that contains the data you want, from “Sheet1” to “Hello”?

[expression.error] the key didnt match any rows in the table.

The result is that the first step of the query now returns a table that looks like this:

[expression.error] the key didnt match any rows in the table.

Since there isn’t a row where the Item column contains “Sheet1” any more, the second step can no longer find the row it’s looking for – the key value it’s looking for no longer exists in the table – which is why you see the error message you do:

[expression.error] the key didnt match any rows in the table.

There are two ways to fix the error:

  1. Go to the Excel workbook and change the name of the sheet called “Hello” back to “Sheet1” or
  2. In either the Advanced Editor or the Formula Bar edit the M code and replace the reference to “Sheet1” with “Hello” in the second step of the query:
    [expression.error] the key didnt match any rows in the table.

In summary, when you’re working with data sources in Power Query you need to be aware that if something in your data source is renamed or deleted then your query will break. You can of course write some clever M code to deal with situations like this but my recommendation is to try to fix the problem in the data source and not in your query.

How do you fix the key didn't match any rows in the table?

There are two ways to fix the error:.
Go to the Excel workbook and change the name of the sheet called “Hello” back to “Sheet1” or..
In either the Advanced Editor or the Formula Bar edit the M code and replace the reference to “Sheet1” with “Hello” in the second step of the query:.

How do you resolve Expression error in Power Query?

Here are the steps I took:.
Get Data from 2 different folders, "Price Reports" and "Product Reports"..
Clean and transform data from the files in each folder..
Merge the files from both folders..
Load to Table and Pivot Chart..

How do I change the key in Power Query?

In Query Editor:.
Do your Unpivoting work..
Add Column; Index Column; From 0 or 1. ... .
Create a copy of the column that the editor thinks is your primary key. ... .
Delete the column that the editor thinks is your primary key (so delete "Primary" column in this example.) ... .
Apply your changes..

How do I remove query errors?

Remove rows with errors.
To open a query, locate one previously loaded from the Power Query Editor, select a cell in the data, and then select Query > Edit. For more information see Create, load, or edit a query in Excel..
Decide which columns for which you want to remove errors. ... .
Select Home > Remove Rows > Remove Errors..