The error "Expression.Error: The variable '[StepName]' is not defined" or "Step references an unknown variable" occurs in Power Query when a step refers to a variable or step name that does not exist in the current query block. This usually happens after a step has been deleted, renamed, or moved in the Applied Steps pane without updating the downstream references that depend on it.
Fast Fix
Open the Advanced Editor (Home > Advanced Editor). Identify the step producing the error, find the first variable name passed into its function argument, and change it to match the exact name of the step immediately above it. If the referenced step contains spaces, ensure it is wrapped in quotes and preceded by a pound sign:
#"Previous Step Name".
Diagnostic Snapshot
| Property | Details |
|---|---|
| Error Type | Expression.Error |
| Exact Syntax | The variable '[StepName]' is not defined. |
| Trigger Event | Manually deleting an intermediate step, renaming a step, or pasting M-code snippets |
| Language Feature | Power Query M is a functional language using immutable variables defined in sequential let ... in blocks |
| Case Sensitivity | Strict. #"Changed Type" is completely distinct from #"Changed type" |
Understanding the M-Code Step Chain
Power Query scripts operate as a connected chain of variables declared between let and in. Each step is actually a named variable that typically accepts the previous step’s output as its input.
Consider this standard query:
let
Source = Csv.Document(File.Contents("C:\Data.csv")),
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers", {{"ID", Int64.Type}})
in
#"Changed Type"
Notice the chain of references:
#"Promoted Headers"takesSourceas its input.#"Changed Type"takes#"Promoted Headers"as its input.- The final
instatement outputs#"Changed Type".
If you delete the #"Promoted Headers" step directly in the user interface, Power Query deletes that line from the code:
let
Source = Csv.Document(File.Contents("C:\Data.csv")),
// The "Promoted Headers" step was deleted here!
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers", {{"ID", Int64.Type}})
in
#"Changed Type"
The step #"Changed Type" is still trying to consume a variable named #"Promoted Headers". Because that variable no longer exists, the engine halts with: The variable ‘Promoted Headers’ is not defined.
Step-by-Step Resolution
Step 1: Open the Advanced Editor
While the visual interface can flag the broken step, you need to open the Advanced Editor to see the chain of variables clearly:
- In Excel, open the Power Query Editor.
- Select the broken query in the left pane.
- In the Home tab, click Advanced Editor.
Step 2: Repair the Broken Pointer Reference
Scan the code to identify the break in the chain:
- Find the step identified in the error message.
- Look at its first argument, this is where it accepts data from the previous step.
- Update that argument to match the exact name of the step that now precedes it.
Broken Example:
let
Source = Excel.CurrentWorkbook(){[Name="SalesTable"]}[Content],
#"Filtered Rows" = Table.SelectRows(Source, each [Amount] > 100),
// Step #"Sorted Rows" was deleted from here
#"Renamed Columns" = Table.RenameColumns(#"Sorted Rows", {{"Amount", "Revenue"}})
in
#"Renamed Columns"
Repaired Example:
let
Source = Excel.CurrentWorkbook(){[Name="SalesTable"]}[Content],
#"Filtered Rows" = Table.SelectRows(Source, each [Amount] > 100),
// Repaired: Now correctly points to #"Filtered Rows"
#"Renamed Columns" = Table.RenameColumns(#"Filtered Rows", {{"Amount", "Revenue"}})
in
#"Renamed Columns"
Step 3: Check Identifier Syntax (#"")
Power Query has strict rules for naming variables:
- If a step name is a single word containing only letters and numbers (like
SourceorFiltered1), it can be written directly without quotes. - If a step name contains spaces, punctuation, or special characters, it must be written as a quoted identifier using the
#""format:#"My Step Name".
Common syntax errors include:
- Missing Quotes:
Table.SelectRows(Promoted Headers, ...)fails. It must be written asTable.SelectRows(#"Promoted Headers", ...). - Case Mismatches:
#"Changed Type"vs#"Changed type". M-code is case-sensitive; these are treated as two entirely different variables.
Step 4: Verify the Final in Statement
A common mistake when manually editing M-code is deleting or renaming the final step in the let block without updating the in statement at the very bottom:
let
Source = Sql.Database("Server", "DB"),
Data = Source{[Schema="dbo",Item="Orders"]}[Data],
CleanedData = Table.SelectRows(Data, each [Total] > 0)
in
FinalTable <-- ERROR: FinalTable does not exist!
To resolve this, ensure the identifier after in matches the name of the final step you want the query to output:
in
CleanedData
For related column-level lookup issues, see Power Query “Expression.Error: The column of the table wasn’t found”. If you are getting errors referencing missing tables or records in source navigation, refer to “Expression.Error: The key didn’t match any rows in the table”. For syntax errors involving standard library functions, see “Expression.Error: The name ‘DateTime.LocalNow’ wasn’t recognized” (Syntax Fix).