To handle errors in Power Query, you can use the "Replace Errors" feature for a quick fix or a custom column with the "try...otherwise" M formula for more granular control. These tools prevent your entire data refresh from failing when a single cell contains a calculation error, a type mismatch, or a missing reference, allowing you to substitute a null, a zero, or a custom message instead.
What You'll Need
- Microsoft Excel or Power BI Desktop.
- A dataset loaded into the Power Query Editor that contains existing errors (such as #REF!, #VALUE!, or data type conversion errors).
- Basic familiarity with the Power Query ribbon interface.
Step-by-Step
Method 1: Using the Replace Errors Interface
This is the fastest method for users who want to replace every error in a specific column with a static value without writing code. Note that menu labels may vary slightly depending on your version of Excel or Power BI.
- Open the Power Query Editor by selecting Data > Get Data > Launch Power Query Editor in Excel, or by clicking Transform Data in Power BI.
- Right-click the header of the column that contains the errors.
- Select Replace Errors from the context menu.
- In the dialog box that appears, enter the value you want to use as the replacement. For example, enter 0 for numeric columns or "Unknown" for text columns.
- Click OK. Power Query will add a "Replaced Value" step to your Applied Steps list on the right.
Method 2: Using the "try...otherwise" Formula
When you need logic-based error handling—such as using a value from a different column if the first one errors out—you must use a Custom Column. This utilizes the M language's internal error-handling mechanism.
- Go to the Add Column tab on the top ribbon.
- Select Custom Column.
- In the New column name field, give your column a descriptive name (e.g., "Cleaned Price").
- In the Custom column formula box, use the following syntax:
try [ColumnName] otherwise [ReplacementValue]. - For example, if you want to try to use the "Price" column but use 0 if it errors, write:
try [Price] otherwise 0. - If you want to use a value from another column as the backup, write:
try [Price] otherwise [DefaultPrice]. - Click OK. A new column will be created. You can then right-click the original error-prone column and select Remove to clean up your table.
Method 3: Removing Rows with Errors
If the data in an errored row is completely useless and should not be part of your analysis, it is safer to remove the row entirely than to replace the value.
- Select the column containing the errors.
- Go to the Home tab on the ribbon.
- Click the Remove Rows dropdown menu.
- Select Remove Errors.
- Verify your row count in the bottom left corner to see how many records were deleted.
Common Mistakes
One of the most frequent mistakes is applying "Replace Errors" before setting the correct data type. If a column is set to "Any" and contains a text string where a number should be, Power Query may not register it as an "Error" until you explicitly change the data type to "Decimal Number" or "Integer." Always define your data types first; otherwise, the Replace Errors function will have nothing to target.
Another common error is confusing a "null" value with an "error." A null is the absence of data, whereas an error is a failure to process data. The "Replace Errors" tool will not affect nulls. To handle nulls, you must use the "Replace Values" tool specifically. If you use try...otherwise, remember that it only triggers on actual errors. If the cell is null, the try part succeeds (because null is a valid value), and the otherwise part is ignored.
Users also frequently forget that Power Query is case-sensitive. If you are writing a custom M formula and misspell a column name or a keyword (e.g., writing "Try" instead of "try"), the formula itself will return an error, creating a loop of errors that can be frustrating to debug.
Tips
To identify exactly where errors are occurring before you fix them, use the "Keep Errors" feature. Go to Home > Remove Rows > Keep Errors. This creates a temporary table showing only the problematic rows. You can use this to investigate the source of the data corruption. Once you understand the pattern, delete this "Keep Errors" step from your Applied Steps list to return to your full dataset.
When using try...otherwise, you can create more complex logic by nesting the statements. For instance, if you have three potential sources for a piece of data, you can write a formula that tries the first, then the second, and finally defaults to a static value if both fail. This ensures maximum data retention.
For large datasets, prefer the built-in "Replace Errors" ribbon command over custom columns. The ribbon command is often more optimized for the engine's folding capabilities, meaning the work might be pushed back to the SQL server (if applicable) rather than being processed locally on your machine, which significantly speeds up refresh times.
Troubleshooting
If you have applied "Replace Errors" but still see "Error" in your cells, check the data type of the column. If the column is set to "Text" but you replaced errors with a number (like 0), Power Query may struggle to render the result. Ensure the replacement value matches the destination column's data type.
If your custom column formula try...otherwise is returning an error for every single row, check your brackets. Column names must be enclosed in square brackets [ ]. If your column name contains spaces, the brackets are mandatory. Ensure there is a space between try, the column reference, otherwise, and the fallback value.
If the "Replace Errors" option is greyed out, ensure you have actually selected a column. This feature cannot be applied to the entire table at once; it must be targeted at a specific column. If you need to apply it to multiple columns, you must repeat the process for each one or write a custom M function to loop through the columns.
If the data refresh still fails after replacing errors, the error may be occurring at the "Source" or "Navigation" steps rather than within the data rows. Check the Applied Steps list. If the error is in the very first step, it usually means the file path has changed or the source database is unreachable. In this case, error handling within the table won't help; you must update the Data Source Settings.
Frequently Asked Questions
Can I replace errors with a value from another column?
Yes, but you cannot do this using the "Replace Errors" dialog box. You must use a Custom Column with the formula try [ColumnA] otherwise [ColumnB].
What is the difference between Replace Errors and Replace Values?
Replace Values looks for a specific known piece of data (like changing "USA" to "United States"). Replace Errors specifically looks for cells that have failed a calculation or type conversion (like a text string in a date column).
Will removing errors delete the entire row?
Only if you use the "Remove Errors" command. If you use "Replace Errors," the row is kept, and only the specific cell containing the error is changed.
Does Power Query handle errors automatically?
No. By default, if Power Query encounters an error during a refresh, it will either load the table with the error cells visible or, in some cases, fail the entire refresh process depending on how the data is being loaded into the model.
Can I use a conditional IF statement instead of try...otherwise?
An IF statement checks for a condition (e.g., if [Price] < 0 then 0 else [Price]). It cannot "catch" a system error. If the cell contains a genuine Power Query Error, the IF statement itself will fail. You must use try to handle actual errors.