Sub topic 16
Finding and Handling Errors
This chapter assumes you can import a table, read Applied Steps, and set data types. You should also have seen a line of M code in the formula bar. About 470 words of guidance follow, so the walkthrough is short.
What a Power Query error is
A cell error appears when one value cannot be computed. A typical case is changing a column to a number type when one cell holds "N/A". The cell shows Error in the preview, but the rest of the column loads normally. Errors are not the same as nulls. A null is an empty value. An error is a failed calculation.
Errors can reach your worksheet. If you load a table that has error cells, Excel shows them as #VALUE!-style cells, and later formulas can break. Find and handle them before loading.
Finding errors
- Look at the column quality bar under each header. It shows the percentage of Valid, Error, and Empty values.
- Select the column and choose Home > Keep Rows > Keep Errors. This shows only the failing rows.
- Click the word
Errorin a cell. Power Query shows the reason below the table, such as "We couldn't convert to Number." - Check the Applied Steps list. The error starts at the step that causes it, so click earlier steps to find where the data was still clean.
By default, Power Query profiles only the first 1,000 rows. To check all rows, change the status bar setting to Column profiling based on entire data set.
Handling errors with the menu
Select a column, then use Home > Remove Rows > Remove Errors to drop failing rows. Use Transform > Replace Values > Replace Errors to substitute a value, such as 0 or null. Removing rows loses data silently, so keep the failing rows in a review query first when the data matters.
Handling errors with try and otherwise
For more control, use try ... otherwise in a custom column. It evaluates an expression and returns a fallback if that expression fails.
let
Source = Excel.CurrentWorkbook(){[Name="SalesRaw"]}[Content],
Safe = Table.AddColumn(Source, "AmountNum", each try Number.From([Amount]) otherwise null, type nullable number)
in
SafeThe Safe step adds AmountNum. If Number.From([Amount]) fails, the cell holds null instead of an error. The final argument, type nullable number, sets the new column's type so you do not need a separate type step.
The original Amount column is unchanged. You can filter for rows where AmountNum is null while Amount is not, which lists exactly the values that failed to convert.
Common mistakes
- Typing before cleaning. Convert types after trimming and replacing text such as "N/A". Fewer values fail.
- Locale mismatches. "1,5" can fail in a US locale. Set the column locale with Change Type > Using Locale.
- Hiding errors. Replacing every error with
0can distort totals. Usenullwhen the value is unknown.
Exercise
A column called Amount contains the text "N/A" in a few rows. You want a numeric column that keeps those rows but shows null for them. Which M expression in a custom column does this?
Show answer
try Number.From([Amount]) otherwise null. The try expression catches the conversion failure for "N/A" and otherwise returns null, so the row stays in the table.