Sub topic 9
Finding, Removing, and Replacing Errors
This chapter assumes you can open the Power Query Editor, read the Applied Steps pane, and set a column's data type. Here you will learn what an error value is, how to find errors, and how to remove or replace them.
What an error value is
An error is a cell value that Power Query could not compute. The cell shows Error in red. Other cells in the same column are unaffected. Errors differ from nulls. A null means "no value". An error means "a step tried to produce a value and failed".
Common causes:
- Changing a column to a number or date type when a cell holds text such as
abc. - Dividing by zero in a custom column.
- Referring to a column or field that does not exist in a given row.
Errors appear when the step runs, so a column can look fine until you change its type.
Finding errors
Open the column quality bar in the data profiling tools. Turn on View > Column quality. Each column shows the percentage of Valid, Error, and Empty cells. A non-zero Error percentage tells you which column to inspect.
To see the details, click a red Error cell, but not the word itself. Click the cell's blank area. The preview pane at the bottom shows the message, for example DataFormat.Error: We couldn't convert to Number. Clicking the word Error itself adds a drill-down step that you probably don't want.
To see only the failing rows, open the column's drop-down menu, choose Keep Errors from the column's context menu under Home > Keep Rows, and read the offending values. Delete that step afterward.
Removing or replacing errors
You have two choices.
- Remove Errors deletes every row that has an error in the selected column. Use it when the bad rows are not worth keeping.
- Replace Errors swaps the error for a value you choose. Use it when the row is valid and only that one cell is bad.
Both commands are under Home > Remove Rows > Remove Errors and the Transform > Replace Values > Replace Errors menu. The code below cleans a small table and replaces the failed conversion with 0.
let
Source = #table(
{"Customer", "Region", "Amount"},
{
{" alice smith ", "West", "120"},
{"BOB JONES", null, "abc"},
{"BOB JONES", null, "abc"}
}
),
CleanedText = Table.TransformColumns(Source, {{"Customer", each Text.Proper(Text.Trim(Text.Clean(_))), type text}}),
FilledRegion = Table.ReplaceValue(CleanedText, null, "Unknown", Replacer.ReplaceValue, {"Region"}),
RemovedDuplicates = Table.Distinct(FilledRegion),
ChangedType = Table.TransformColumnTypes(RemovedDuplicates, {{"Amount", type number}}, "en-US"),
ReplacedErrors = Table.ReplaceErrorValues(ChangedType, {{"Amount", 0}})
in
ReplacedErrorsRead the last two steps. Table.TransformColumnTypes turns "abc" into an error. Table.ReplaceErrorValues then takes a list of column and value pairs and writes 0 wherever that column holds an error. The result has two rows: Alice Smith with 120, and Bob Jones with 0.
Order of steps
Replace errors after the step that creates them. If you replace errors before the type change, nothing happens, because the error does not exist yet. If you later change the type again, new errors can appear, and you need another replace step.
Choosing a replacement
0 is not always safe. A zero in Amount lowers totals and averages as if the sale were real. Consider null instead, which aggregations skip. Record your choice so report users know how bad values were treated.
Exercise
A Quantity column shows 2% Error in the column quality bar after you change its type to whole number. You still need those rows for a count of orders. Should you use Remove Errors or Replace Errors, and what replacement value avoids distorting an average of Quantity?
Show answer
Use Replace Errors, because Remove Errors would delete the orders and lower the order count. Replace with null, because averages skip nulls, whereas 0 would pull the average down.