Sub topic 16
Finding and Handling Errors
This chapter assumes you can build a query in the Power Query Editor, set data types, and read Applied Steps. It also assumes you have seen basic M code in the formula bar. You will learn to find errors, tell the kinds apart, and fix them at the right level.
Two kinds of errors
Power Query reports problems at two levels. Knowing which one you have decides how you fix it.
| Kind | What it looks like | Typical cause |
|---|---|---|
| Cell-level (row) error | A cell shows `Error` in red text. The rest of the column loads normally. | A value cannot be converted, such as `"N/A"` to a number. |
| Step-level error | The whole preview is replaced by an error message and a yellow or red banner. | A missing column, a renamed source, or a file that cannot be found. |
A cell error does not stop the query. If you load the table to a worksheet, the error cell loads as #VALUE!-style text or is dropped, depending on the load target. Either way you get wrong totals without a warning, so check for errors before you load.
Finding cell errors
Power Query does not show every error up front. It only checks the rows in the preview. Use these steps to find them:
- Look at the column quality bar under each header. It shows percentages for Valid, Error, and Empty.
- Turn on View > Column quality and Column profiling based on entire data set (in the status bar at the bottom). Without this, the profile only covers the first 1,000 rows.
- Select a column that shows an error percentage above zero.
- Right-click the column and choose Keep Errors to see only the failing rows. Look at the neighbouring columns to see which source value caused the problem.
To see why a single cell failed, click the cell, not the word Error. The preview pane at the bottom shows the message, such as DataFormat.Error: We couldn't convert to Number. along with the value that failed.
Where errors come from
Most cell errors appear right after a Changed Type step. Consider this source column:
| Amount |
|---|
| 120.50 |
| 98 |
| N/A |
| 1,200 |
Setting the type to number converts the first two values. "N/A" fails. "1,200" may fail or succeed depending on the locale of the query, which is why locale matters when you set types.
Errors can also come from other operations:
- Dividing by zero in a custom column.
- Combining text and a number with
&without converting the number first. - Calling a function on
null, such asText.Upper(null)in some contexts. - Looking up a field that does not exist in a record.
An important rule: an error in a cell stays in that cell until you remove or replace it. It does not spread to other rows, but any calculation that uses that cell also produces an error.
Fixing errors with the interface
The Editor offers three direct options on the column's right-click menu.
- Remove Errors deletes every row where that column has an error. Use this only when the row is truly worthless, because you lose the rest of the row's data.
- Replace Errors swaps each error for a value you choose, such as
0ornull. Usenullwhen the value is unknown, since0looks like a real measurement. - Keep Errors shows only failing rows, as described above.
The generated M for Replace Errors is Table.ReplaceErrorValues:
let
Source = Excel.CurrentWorkbook(){[Name="SalesRaw"]}[Content],
Typed = Table.TransformColumnTypes(Source, {{"Amount", type number}}),
NoErrors = Table.ReplaceErrorValues(Typed, {{"Amount", null}})
in
NoErrorsThis approach hides the problem. The source still contains bad values, and you no longer see them. It is fine for a known, harmless pattern. For an unknown pattern, investigate first.
Handling errors with try and otherwise
For more control, use try ... otherwise inside a custom column. M evaluates the expression after try. If it raises an error, M returns the value after otherwise instead.
let
Source = Excel.CurrentWorkbook(){[Name="SalesRaw"]}[Content],
Safe = Table.AddColumn(Source, "AmountNum", each try Number.From([Amount]) otherwise null, type nullable number)
in
SafeHere, Number.From("N/A") raises an error, so AmountNum holds null for that row. The original Amount column is unchanged, so you can still filter on rows where AmountNum is null and Amount is not, to list the values that failed.
This is the main advantage over Replace Errors. You keep the raw value and add a clean value next to it.
Keeping the error message
Using try without otherwise returns a record with the fields HasError, Value, and Error. This lets you capture the reason a row failed.
let
Source = Excel.CurrentWorkbook(){[Name="SalesRaw"]}[Content],
Attempt = Table.AddColumn(Source, "Attempt", each try Number.From([Amount])),
Message = Table.AddColumn(Attempt, "Problem", each if [Attempt][HasError] then [Attempt][Error][Message] else null, type nullable text),
Value = Table.AddColumn(Message, "AmountNum", each if [Attempt][HasError] then null else [Attempt][Value], type nullable number),
Cleaned = Table.RemoveColumns(Value, {"Attempt"})
in
CleanedThe result has AmountNum for calculations and Problem for a review list. You can load rows where Problem is not null to a separate sheet so someone can correct the source.
Errors in a step that stops everything
A step-level error has a different fix. The message usually names the cause:
Expression.Error: The column 'Region' of the table wasn't found.A later step refers to a column that no longer exists. Typically the source header was renamed, or an earlier step was reordered or deleted. Open the failing step in the formula bar and check the column name against the previous step.DataSource.Error: Could not find file. The path in the Source step is wrong or the file moved. Fix it in the Source step or with a parameter.Expression.Error: There weren't enough elements in the enumeration. A navigation step expected a row or table that is empty, such as an empty file.
Try and otherwise also works for a whole step, but use it sparingly. Wrapping an entire source in try can hide a missing file and give you an empty report that looks valid.
A practical workflow
The flow below shows a reasonable order of decisions.
Common mistakes
- Trusting the 1,000-row preview. Errors lower in the table do not show until you profile the whole data set or load the query.
- Replacing errors with 0. Zero is a valid amount. Use
nullso that averages and counts stay honest. - Fixing errors before the Changed Type step. If you clean values (for example
Text.Replaceto strip a currency symbol) before the type conversion, fewer values fail. Move the cleaning step ahead of the type step. - Auto-added Changed Type steps. Power Query sometimes adds a type step to every column on import. If a source column changes, that step can raise a step-level error. Review it and keep only the columns you need.
- Forgetting the locale. A date such as
03/04/2025converts differently under US and UK settings. Use Change Type > Using Locale when the source follows a different convention.
Exercise
A column Amount contains the values 120, 98, and "N/A". You want a numeric column AmountNum that holds null for bad values, while keeping the original Amount so you can audit it. Which approach fits, and what is the M expression for the custom column?
Show answer
Use a custom column with try and otherwise rather than Replace Errors, because Replace Errors overwrites the column and loses the original. The expression is: each try Number.From([Amount]) otherwise null. Add it with Table.AddColumn and set the type to type nullable number.
Exercise
Your query fails with "Expression.Error: The column 'Region' of the table wasn't found" after a colleague renamed a header in the source file. Is this a cell-level or step-level error, and where do you look first?
Show answer
It is a step-level error. Click the failing step in Applied Steps, then read its formula in the formula bar. Compare the column name it uses with the column names produced by the previous step, and either update the name in the step or fix the header in the source.