Sub topic 8
Handling several outcomes with nested IF
A single IF gives two outcomes. Many decisions need more, such as turning a score into a letter grade. This chapter shows how to nest IF functions to cover three or more outcomes. It assumes you can write a basic IF formula, use comparison operators, and copy a formula down a column.
The idea: an IF inside an IF
The third argument of IF is the value returned when the test is false. That value can itself be another IF. Each extra IF adds one more question for the cases that failed the previous test.
=IF(B2>=90,"A",IF(B2>=80,"B",IF(B2>=70,"C","F")))Excel reads this formula from left to right:
- If B2 is 90 or more, return "A" and stop.
- Otherwise, if B2 is 80 or more, return "B" and stop.
- Otherwise, if B2 is 70 or more, return "C" and stop.
- Otherwise, return "F".
Three IF calls produce four outcomes. In general, N outcomes need N-1 IF functions, and the last one supplies the default.
Order matters
Excel stops at the first test that is true. That is why the grading formula starts at 90 and works down. A score of 95 passes every test, but it returns "A" because the 90 test is checked first.
Now look at the same tests in the wrong order:
=IF(B2>=70,"C",IF(B2>=80,"B",IF(B2>=90,"A","F")))A score of 95 passes the first test (>=70) and returns "C". The "B" and "A" tests are never reached. The formula raises no error, so this mistake is easy to miss.
When you test with >=, start with the highest threshold. When you test with <, start with the lowest.
Counting parentheses
Each IF opens one parenthesis, and all of them close at the end of the formula. The formula above ends with ))) because it contains three IF calls. A missing closing parenthesis makes Excel show an error message or offer to fix the formula.
A good habit is to write the formula from the inside out. First write the last IF, then wrap the earlier tests around it. You can also press Alt+Enter inside the formula bar to put each IF on its own line, which makes the structure easier to read. Excel ignores the line breaks when it calculates.
A worked example with mixed results
Nested IF can return numbers as well as text. Suppose a shop pays a commission rate that depends on sales in B2:
=IF(B2>=10000,B2*0.1,IF(B2>=5000,B2*0.06,IF(B2>=1000,B2*0.03,0)))| Sales in B2 | Test that is true first | Result |
|---|---|---|
| 12000 | B2>=10000 | 1200 |
| 6000 | B2>=5000 | 360 |
| 1500 | B2>=1000 | 45 |
| 400 | None, so the default | 0 |
The default value sits at the very end. Always decide what should happen when no test matches, and put that value in the last false argument.
When IFS is cleaner
Long nested formulas are hard to read and edit. IFS lists the test and result pairs one after another, without the extra parentheses:
=IFS(B2>=90,"A",B2>=80,"B",B2>=70,"C",TRUE,"F")IFS is available in Excel 2019 and later and in Microsoft 365. It is not in older versions. The final TRUE,"F" pair works as the default, because TRUE is always true. Without it, a score below 70 would return a #N/A error. The next chapter covers IFS in more detail.
For two or three outcomes, nested IF is fine. Past about four levels, consider IFS or a lookup table.
Common problems
- Wrong order of tests. The formula returns a valid but wrong result. Check that the thresholds run in one direction.
- Missing quotes around text.
"A"is text. A bareAis read as a name, and Excel returns #NAME?. - Mismatched parentheses. Count the opening IF calls and make sure the same number of closing parentheses appear at the end.
- No default. If you omit the final false value in the last IF, Excel returns FALSE for unmatched cases. State the default on purpose.
- Too many levels. Excel allows 64 nested levels, but formulas past three or four are hard to maintain.
Exercise
Write a nested IF formula that reads a temperature in B2 and returns "Hot" for 30 or higher, "Warm" for 20 or higher, "Cool" for 10 or higher, and "Cold" otherwise. Why would it return wrong results if you tested B2>=10 first?
Show answer
=IF(B2>=30,"Hot",IF(B2>=20,"Warm",IF(B2>=10,"Cool","Cold"))). If B2>=10 were first, every value of 10 or more, including 35, would return "Cool", because Excel stops at the first true test and never reaches the higher thresholds.
Exercise
A formula =IF(B2>=90,"A",IF(B2>=80,"B","C") shows an error when you press Enter. What is wrong, and how do you fix it?
Show answer
It has two IF calls but only one closing parenthesis at the end, so a parenthesis is missing. Add a second one: =IF(B2>=90,"A",IF(B2>=80,"B","C")).