Solved - Excel Power Query fails to refresh with Expression.Error - The key didnt match any rows in the table when dynamic source path depends on a cell value
📌 The Problem Explained
Section titled “📌 The Problem Explained”Imagine you have built a sophisticated Excel dashboard that pulls data from an external CSV or Workbook. To make it “user-friendly,” you created a setup sheet where a user can type or paste a folder path into a cell. You then linked this cell to Power Query using a Named Range.
Everything works perfectly on your machine. However, the moment the file path changes, or a colleague tries to run the report, Excel throws a dreaded error: Expression.Error: The key didn’t match any rows in the table.
This happens because Power Query is looking for a specific metadata “Key” (like a Sheet Name or Table Name) that was cached during the initial setup. When the dynamic path points to a new file, if the internal structure doesn’t perfectly match the hardcoded navigation steps in your M-code, the entire refresh process breaks. This is a common bottleneck in Excel automation and workflow optimization.
💡 The Quick Solution
Section titled “💡 The Quick Solution”The most robust way to fix this is to separate the Path Variable from the Data Navigation. Use the following M-code pattern to safely extract your cell value:
let // 1. Pull the path from your Named Range (e.g., "MyFilePath") PathSource = Excel.CurrentWorkbook(){[Name="MyFilePath"]}[Content]{0}[Column1],
// 2. Use that path in your Source step Source = Excel.Workbook(File.Contents(PathSource), null, true),
// 3. Select the first item in the file regardless of its name TargetData = Source{0}[Data]in TargetData🛠️ Step-by-Step Breakdown
Section titled “🛠️ Step-by-Step Breakdown”Follow these steps to implement a dynamic file path that won’t break when the file moves or the name changes.
| Step | Action | Description |
|---|---|---|
| 1. Create Named Range | Select your path cell > Formula Bar Name Box | Type a name like MyFilePath and press Enter. This makes the cell accessible to Power Query. |
| 2. Extract to Query | Data > From Table/Range | This opens the Power Query Editor. Rename this query to varPath. |
| 3. Convert to Text | Right-click the cell value > Drill Down | This transforms the table into a single text string. Now you have a dynamic variable. |
| 4. Edit Main Query | Home > Advanced Editor | Locate the query that is failing and open the code window. |
| 5. Update Source | Replace hardcoded path with varPath |
Change File.Contents("C:\OldPath\File.xlsx") to File.Contents(varPath). |
| 6. Fix Navigation | Replace {[Item="Sheet1"]} with {0} |
Instead of looking for a specific name (the “Key”), {0} selects the first object found in the file. |
⚠️ Common Mistakes to Avoid
Section titled “⚠️ Common Mistakes to Avoid”- Privacy Level Conflicts: This is the #1 reason dynamic paths fail after the logic is fixed. If you get a “Formula.Firewall” error, go to File > Options and Settings > Query Options. Under Privacy, select Ignore the Privacy Levels and potentially improve performance. This allows Power Query to combine data from your local cell with the external file path.
- Trailing Backslashes: Ensure your cell value path is clean. If your formula concatenates a folder and a filename, double-check that you aren’t ending up with
C:\Folder\\File.xlsx(double backslashes). - Case Sensitivity: Power Query M-language is case-sensitive. If your Named Range is
MyFilePathbut you typemyfilepathin the Advanced Editor, the refresh will fail with a “Table not found” error.
By using the Drill Down method and referencing by Index ({0}) rather than Name, you create a resilient Excel automation workflow that handles dynamic data sources like a pro.