Site icon Best Excel Tutorial

How to Handle Errors in Power Query

Power Query is extremely powerful for cleaning and reshaping data, but it is also unforgiving: one bad value, missing column, or data type mismatch can cause refresh failures. Learning how to detect, understand, and handle errors is essential if you want your queries to be reliable and refresh automatically without breaking.

This guide walks through the main types of errors in Power Query and shows practical techniques to remove, keep, replace, or gracefully handle them using built‑in commands and the try … otherwise pattern in M.

1. Types of Errors in Power Query

Step‑level vs. cell‑level errors

Common causes

2. How to Find Errors Quickly

Visual error indicators in the preview

In Power Query Editor, error cells are shown as the word Error in red. Clicking an error value opens a detailed dialog with the error message, error type, and sometimes a stack trace that points to the failing operation.

Use Data Profiling (Column Quality & Profile)

  1. In Power Query Editor, go to the View tab.
  2. Enable Column quality and optionally Column profile.
  3. Each column now shows percentages of Valid, Error, and Empty values.
  4. Click on the error percentage to quickly Keep Errors or Remove Errors for that column.

Data profiling is especially helpful when errors appear far down in large datasets and aren’t visible in the initial preview.

3. Remove, Keep, or Replace Errors (No Code)

Remove rows with errors

To completely remove rows that contain errors in specific columns:

  1. Select the column(s) that contain errors.
  2. Go to Home > Remove Rows > Remove Errors.
  3. Power Query adds a Removed Errors step. Only rows without errors in those columns remain.

This is useful when error rows are not critical or are clearly bad records, but remember: you’re deleting data, which might affect totals and counts.

Keep only rows with errors (for auditing)

To investigate problem records, you can isolate all error rows:

  1. Select the column with errors.
  2. Go to Home > Keep Rows > Keep Errors.
  3. The query now contains only rows where that column has an error.

You can duplicate your main query, apply Keep Errors in the copy, and use that as an error report while keeping the main query clean.

Replace errors with a value

To keep the rows but remove the error from a column:

  1. Select the column with errors.
  2. Go to Transform > Replace Values > Replace Errors, or right‑click the column header and choose Replace Errors.
  3. Enter a replacement value (for example, 0, null, or "Unknown" depending on context).
  4. Click OK.

This creates a Replaced Errors step that uses Table.ReplaceErrorValues under the hood.

4. Using try … otherwise (Power Query’s IFERROR)

Power Query does not have an IFERROR function like Excel, but the M language offers equivalent functionality through the try … otherwise construct. It lets you “try” an expression and, if it errors, return a safe fallback value instead.

Basic syntax

try <expression> otherwise <fallback_value>

Example: safe division (avoid divide‑by‑zero)

= Table.AddColumn(
    #"Previous Step",
    "Safe Result",
    each try [Numerator] / [Denominator] otherwise 0,
    type number
)

Any row where the division would have caused an error (e.g., denominator is 0 or null) now gets 0 instead, and the query refreshes successfully.

Example: safe type conversion

Convert text to number without breaking on bad values:

= Table.AddColumn(
    #"Previous Step",
    "AmountAsNumber",
    each try Number.From([AmountText]) otherwise null,
    type number
)

Bad values become null, and you can later filter or replace them.

Using Table.ReplaceErrorValues for whole columns

The UI command Replace Errors produces code like this:

= Table.ReplaceErrorValues(
    #"Changed Type",
    {{"Value", 0}}
)

This is equivalent to wrapping [Value] in try [Value] otherwise 0, but is more concise when you just want to map all errors to a single default.

5. Building an Error Table for Debugging

For robust data pipelines, it’s often better to log errors than silently drop or overwrite them. A common pattern is:

  1. Duplicate your main query.
  2. In the duplicate, use Keep Rows > Keep Errors on suspect columns.
  3. Optionally add columns describing the error context (source file, date, etc.).
  4. Load this “errors only” query to a separate sheet or table as an error report.

This lets you keep production output clean while still investigating data quality issues upstream.

6. Handling Schema & Data Source Errors

Missing or renamed columns

If your source file or table changes (for example, a column is removed or renamed), step‑level errors occur when Power Query tries to reference a column that no longer exists.

You can protect against this with try … otherwise when accessing columns:

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Added Safe Column" =
        Table.AddColumn(
            Source,
            "OptionalColumn",
            each try [NewColumn] otherwise null,
            type any
        )
in
    #"Added Safe Column"

If [NewColumn] doesn’t exist, the expression returns null instead of failing the entire query.

Data source connection errors

For web APIs or unstable data sources, wrap calls in try … otherwise so intermittent failures don’t break everything:

let
    SafeResponse = try Web.Contents("https://api.example.com/data") otherwise null,
    Parsed =
        if SafeResponse <> null
        then Json.Document(SafeResponse)
        else null
in
    Parsed

Downstream steps should then handle null responses (for example, filter them out or log them).

7. When to Remove vs. Replace vs. Fix at Source

8. Best Practices to Avoid Errors

9. Quick Decision Guide

Goal Recommended approach
Refresh must never fail due to bad values Wrap risky expressions with try … otherwise and/or use Table.ReplaceErrorValues on key columns.
You need an error log for auditors Duplicate query, use Keep Errors, and load that as a separate “error table”.
You want clean data for analysis, errors are unimportant Use Remove Errors on affected columns and document the decision.
You must preserve rows but can use defaults Use Replace Errors to replace errors with 0, null, or a meaningful placeholder.

The Bottom Line

Errors in Power Query are inevitable when working with real‑world data. The goal is not to eliminate them entirely, but to detect them early, understand their causes, and handle them deliberately. Use the built‑in commands (Remove/Keep/Replace Errors) for quick fixes, and adopt try … otherwise when you need robust, Excel‑style IFERROR logic embedded in your transformations.

By combining error logging, safe defaults, and good upstream data practices, you can build Power Query solutions that refresh reliably and give you confidence in the quality of your analytical output.

Exit mobile version