Skip to content

Error Handling

Excel deals with errors in a formula through IFERROR, which returns a fallback value when the first argument fails:

= IFERROR(value, value_if_error)

Power Query has no IFERROR function. It uses the keywords try and otherwise instead, and it can also report why each error happened. This page works through both, using a small price table in which some prices are Excel errors:

An Excel table with Price, Amount and List Price columns, where five of the Price cells hold #REF!, #DIV/0!, #NAME? and #VALUE! errors

The aim is a new SubTotal column that multiplies [Price] by [Amount]. Where the price is an error, the calculation should fall back to [List Price] instead.

Once the table is loaded into Power Query, each Excel error appears as the word Error in the Price column:

The table in the Power Query editor, where the five broken prices show as Error and the Price column’s quality bar is partly red

2. Add a custom column with try and otherwise

Section titled “2. Add a custom column with try and otherwise”

On the Add Column tab, choose Custom Column, name the column SubTotal, and enter a formula that tries the price first and falls back to the list price:

The Custom Column dialog with the new column named SubTotal and the formula try [Price][Amount] otherwise [List Price][Amount]

Power Query evaluates the expression after try. If it succeeds, its result is used, and if it fails, the expression after otherwise is used instead. Every row now has a subtotal:

The query with a SubTotal column filled on every row, where the rows with a broken price use the list price, such as 680.6 for 83 at 8.2

A fallback hides the error, which is not always what is wanted. To see why each price failed:

  • Remove the Changed Type step from Applied Steps.
  • Add another custom column, called Error Message, holding try on its own, with no otherwise:

The Custom Column dialog with the new column named Error Message and the formula try[Price]

  • try on its own returns a record for each row. Click the expand button on the column header and keep all three of its fields:

The expand menu on the Error Message column, with HasError, Value and Error all ticked

The table now shows whether each row failed. A good row has HasError set to FALSE and its price in Value, while a failed row has HasError set to TRUE and a record in Error:

The query with HasError, Value and Error columns, where the five failed rows show TRUE, null and Record, and the rest show FALSE, the price and null

  • Expand the Error column in turn, keeping Reason and Message, and then remove the HasError and Value columns:

The expand menu on the Error column, with Reason and Message ticked and Detail left clear

The finished table gives the cause of each error beside the row it affects:

The final query, where each failed row shows the reason DataFormat.Error and a message such as Invalid cell value ‘#REF!’, and the good rows show null

When the expression after try succeeds, the record it returns has HasError set to false, and Value holds the result:

A record from a successful try, showing HasError as FALSE and Value as 5

Because the record can be tested like any other, errors can be handled in M without otherwise. The query below keeps the try record in a column, and then takes the value where there was no error and null where there was:

let
Source = Excel.CurrentWorkbook(){[Name="Sample"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "Record", each try[Price]),
#"Added Custom1" = Table.AddColumn(#"Added Custom", "Price.1", each if [Record][HasError] then null
else [Record][Value])
in
#"Added Custom1"

The result is the same as writing try [Price] otherwise null, which returns null wherever the price is an error.

An error can also be replaced with one that carries a message of your own, so that whoever sees it knows what went wrong:

let
Source = Excel.CurrentWorkbook(){[Name="Sample"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "Record", each try[Price] otherwise error "Price is invalid")
in
#"Added Custom"