Find a course

Module 2: Logic and Error HandlingLesson 4 of 1212 min

Nested IF, IFS and SWITCH

Handle three or more outcomes with nested IF, the cleaner IFS and SWITCH functions, and learn when a lookup table beats any formula.

Step 1 of 10Learn

More than two outcomes

A training company grades its final assessment like this: 90 and up is Distinction, 75–89 is Merit, 60–74 is Pass, and below 60 is Fail. One IF gives two outcomes, so for four you put an IF inside the false branch of another:

=IF(B2>=90,"Distinction",IF(B2>=75,"Merit",IF(B2>=60,"Pass","Fail")))

Excel checks the first test. If it fails, it moves to the next IF, and so on until one passes or it reaches the final fallback, "Fail". Notice you don't need AND(B2>=75, B2<90) for Merit: if the score were 90 or more, Excel would have stopped at the first test.

That shortcut only works if the tests are in the right order. Check the biggest threshold first when using >=. If you start with B2>=60, a score of 95 passes that test and gets labeled "Pass", and the Distinction branch is never reached.

Count the closing parentheses: one for each IF. Excel colors matching pairs while you edit, and a missing one is the most common error message you'll see with nested formulas.

A nested IF checks tests in order and stops at the first TRUEB2>=90?yes: Distinctionelse B2>=75?yes: Meritelse B2>=60?yes: PasselseFailBiggest threshold first: start with >=60 and a 95 is labeled Pass

Step 2 of 10Practice · Put in order

Put the tests in the order a nested IF should check them, using >= comparisons, for these tiers: Gold at 10,000 points, Silver at 5,000, Bronze at 1,000, otherwise Standard.

Drag the items, or use the arrow buttons, to put them in order.

  1. B2>=10000 returns "Gold"
  2. Otherwise "Standard"
  3. B2>=1000 returns "Bronze"
  4. B2>=5000 returns "Silver"

Step 3 of 10Learn

IFS: a flatter way to write it

Microsoft 365 and Excel 2019 or later include IFS, which lists test-and-result pairs without nesting:

=IFS(B2>=90,"Distinction", B2>=75,"Merit", B2>=60,"Pass", TRUE,"Fail")

Excel checks each test in order and returns the result paired with the first one that's TRUE. Order still matters, exactly as with nested IF.

IFS has no built-in "otherwise" argument. If no test is TRUE, it returns #N/A. The standard fix is a final pair whose test is simply TRUE, which always passes, so it acts as the fallback. Forgetting it is the most common IFS bug: a score of 45 returns #N/A instead of "Fail".

IFS is easier to read and edit than four levels of IF, and you can't misplace a closing parenthesis halfway through. The trade-off is compatibility: an older version of Excel shows #NAME? for it. If a file goes to people on Excel 2016 or earlier, stick with nested IF.

Step 4 of 10Practice · Work it out

Shipping costs $12 for orders under $25, $6 for orders from $25 up to $74.99, and $0 from $75. What does =IFS(B2<25,12, B2<75,6, TRUE,0) return when B2 is 74.99?

Step 5 of 10Learn

SWITCH: matching exact values

When the decision depends on matching a value exactly, rather than on ranges, SWITCH is cleaner still:

=SWITCH(expression, value1, result1, value2, result2, ..., [default])

A warehouse uses one-letter region codes in C2. To show the full name:

=SWITCH(C2, "N","North", "S","South", "E","East", "W","West", "Unknown")

Excel evaluates C2 once, compares it with each value in turn, and returns the matching result. The last, unpaired argument is the default, used when nothing matches. Leave it out and an unexpected code returns #N/A.

SWITCH is good for codes, weekday numbers (=SWITCH(WEEKDAY(A2),1,"Sun",7,"Sat","Weekday")) and status labels. It can't test ranges such as "score at least 90" directly, because it compares for equality.

Whichever function you use, remember that text matching ignores case, so "n" matches "N", but it does not ignore spaces. A code typed as "N " with a trailing space matches nothing and falls through to the default. You'll learn to clean that with TRIM later.

Step 6 of 10Compare

Which tool for which decision?

AspectBest forWatch out for
IFTwo outcomesBoundary operators (> vs >=)
Nested IFThree or four range-based tiers, any Excel versionOrder of tests, counting parentheses
IFSSeveral range-based tiers, readableNeeds TRUE as the last test; not in Excel 2016
SWITCHExact codes or labelsCannot test ranges; add a default
Lookup table + XLOOKUPMany tiers or values that changeTable must be sorted or set up correctly

Step 7 of 10Practice · Spot the mistake

This formula should label scores of 90+ "A", 80–89 "B", 70–79 "C" and anything lower "F", but every score of 70 or more comes out "C". Click the test that causes it.

Step 8 of 10Learn

When to stop nesting and use a table

Nested logic has a ceiling. Seven commission tiers in a nested IF is a formula nobody wants to audit, and when the sales director changes one threshold, someone has to edit the formula in every cell.

The professional alternative is a lookup table: thresholds in one column, results in the next, sorted from lowest to highest.

Min salesRate
02%
10,0004%
25,0006%
50,0008%

An approximate-match lookup (which you'll write in the lookups module) finds the largest threshold that doesn't exceed the sales figure and returns its rate. A rep with $31,000 in sales gets 6%. Changing a rate means changing one cell, and adding a tier means adding a row.

A reasonable rule of thumb: two outcomes, use IF; three to five fixed tiers, IFS or nested IF is fine; more tiers than that, or tiers that change, belong in a table. Tables also make the rules visible to the people who own them, which formulas never do.

A lookup table replaces nested tiers: $31,000 matches the 25,000 rowMin salesRate02%10,0004%25,0006%50,0008%

Step 9 of 10Note

Step 10 of 10Practice · Choose one

A score of 45 returns #N/A from =IFS(B2>=90,"A", B2>=80,"B", B2>=70,"C"). What is the best fix?