07.5IFERROR
In Excel
| Excel | IFERROR |
|---|---|
| Match |
Sometimes a formula fails on part of the values, and an error shows up in your visual. IFERROR replaces it with a result of your choice, like IFERROR in Excel.
Imported data sometimes holds a text where a number is expected, like “New York” in a price column. The formula that converts this price into a number then fails, and the visual shows an error message in place of the figures.
07.5.1The syntax of IFERROR
IFERROR returns a fallback result when an expression produces an error.
If the expression is calculated without an error, IFERROR returns its result. Otherwise, it returns the second argument.
07.5.2An example
The VALUE function turns a text into a number. It succeeds with a text made of digits, and fails with a text made of letters:
This expression returns 250.
This one produces an error, since “New York” is not a number.
With IFERROR, the error is replaced with 0:
This expression returns 0. With "250", it would return 250.
| [With 250] | [With New York] | |
|---|---|---|
| 1 | 250 | 0 |
This is a reproduction of the Power BI interface for teaching purposes. It differs from the real interface.
Practice
Each exercise is solved with the function you just discovered. Two exercises per function, a direct use and a combined use, from easiest to hardest, on data other than the course data. Pick the right formula out of five.
Direct useCalculated columnDifficulty
A shop imported its prices from an old file. In the “Imports” table, the “Price” column is text, and one row holds “to check” instead of a price. Add to the “Imports” table the calculated column “Price as Number,” which converts the price with VALUE, and returns 0 when the conversion fails.
Imports Item Price Lamp 40 Rug to check Vase 25 Which formula should you write for “Price as Number”?
Combined useMeasureDifficulty
A library records in the “Donations” table the number of books donated, as text. One row holds “box” instead of a number. Without adding a column, write the “Books Donated” measure, the total of the books, counting 0 for the row that holds no number.
Donations Donor Books Alma 12 Bilal box Chloé 8
07.5.3For a division, prefer DIVIDE
A division by zero written with the / operator returns infinity, minus infinity when the numerator is negative, or NaN, “not a number,” for 0 / 0. IFERROR treats them as errors, and replaces them.
This expression returns 0.
For a division, use DIVIDE, seen in “Six basic functions.” Its third argument sets the result when the denominator is zero or blank:
This expression returns 0.
07.5.4Use it sparingly
IFERROR hides the error, but not its cause. A formula that fails often most often points to data that needs fixing, or to a condition to test before the calculation, with IF.
IFERROR also has a cost. On a large table, it slows down the calculations, and Microsoft advises using it as little as possible. When you can test the cause with IF, or use DIVIDE, prefer that.
Quiz
The questions often rely on the course tables. Keep them in view in their floating window, which you can move and enlarge.
Question 1
The “Stock” table has a “Quantity” column as text, with "8", "?", "12" and "5". You add to it the column Quantity as Number = IFERROR( VALUE( Stock[Quantity] ), 0 ). What does the sum of this new column return?
Question 2
Without IFERROR or DIVIDE, what does [Revenue] / 0 return?
Question 3
The target of some cities is zero. For the “Rate” measure, which divides the “Revenue” measure by the “Target” measure and shows 0 in that case, which formula should you prefer?
- IFERROR( <expression>, <value_if_error> ) returns the second argument when the expression produces an error.
- A division by zero written with / returns infinity, which IFERROR replaces. For a division, prefer DIVIDE.
- IFERROR slows down the calculations on large tables. When you can, test the cause with IF.