Stop 05 · M Language & Advanced Power QueryTopics · 8/8

Error Handling with try otherwise

Part of the Power BI Roadmap

In short

One branch typed "NA" in the Amount column and the whole refresh failed at 7 am before the sales review. try ... otherwise lets one bad cell fall back to a safe value instead of breaking everything.

The try ... otherwise expression catches errors in M and returns a fallback value, so one bad row doesn't break a whole refresh.

The jaadu leaps between two trapezes, misses one, and bounces safely into a wide net while the circus show carries on.

Think of it like this

A safety net under the trapeze

The acrobat tries the jump. If they slip, the net catches them and the show goes on. try attempts an expression, and otherwise is the net that catches errors with a fallback value.

Watch it happen

Number.From on each Amount, with a safety net

1200
0
850
1
NA
2
--
3
430
4

1/5Raw Amount text from a branch file. Two cells will not convert to numbers.

The big ideas

What you need to know

try x otherwise y

Returns x if it works, or y if x raises an error. y is often null, 0 or a label like "Unknown".

try alone returns a record

try x gives [HasError = false, Value = ...] or [HasError = true, Error = [...]] so you can read the error message.

Find errors first

View > Column quality shows error counts. Keep Errors shows only bad rows so you can see what went wrong.

Don't hide everything

Replacing every error silently hides data problems. Flag or count them so someone can fix the source.

See it in code

Walk through the code

powerquery
1let2    Source = BranchRaw,3    SafeAmount = Table.AddColumn(4        Source, "AmountClean",5        each try Number.From([Amount]) otherwise null,6        type nullable number7    ),8    WithReason = Table.AddColumn(9        SafeAmount, "AmountError",10        each let r = try Number.From([Amount]) in if r[HasError] then r[Error][Message] else null,11        type nullable text12    )13in14    WithReason

1/3For each row, try to convert Amount to a number. If it fails, use null.

One column with a safe fallback, and one that keeps the actual error message for checking.

Save yourself the pain

Common mistakes

Oops

Wrapping every step in try ... otherwise 0.

Do this instead

Turning bad values into 0 makes totals look fine while being wrong. Use null and report how many rows failed.

Oops

Using Remove Errors on the whole table without looking.

Do this instead

Use Keep Errors first to see which rows break and why, then decide how to fix or flag them.

Your mission

Make a refresh survive bad data

~20 min

Quick check

What does try Number.From("NA") return (without otherwise)?

Go deeper

  1. 1Wrap a text-to-number conversion in try ... otherwise null.
  2. 2Count how many rows fell back to the default.
  3. 3Use Replace Errors in the interface and look at the code it writes.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge