Sub topic 9
A cleaner option: the IFS function
This chapter assumes you can write a basic IF formula and a nested IF, use comparison operators such as >= and <, and copy a formula down a column with the fill handle. It builds directly on the grading example from the previous chapter, where nested IF calls were stacked inside one another.
Why nested IF gets hard to read
A nested IF handles several outcomes by placing one IF inside the false branch of another. It works, but every extra outcome adds another IF( at the start and another closing parenthesis at the end. Here is the grading formula again.
=IF(B2>=90,"A",IF(B2>=80,"B",IF(B2>=70,"C","F")))With four outcomes, you must count three closing parentheses on the right. With seven outcomes, you must count six, and one missing parenthesis produces an error or, worse, a wrong result. The test and its result are also separated by other IF calls, so it is harder to see which result belongs to which test.
The IFS function removes the nesting. You list the tests and results as pairs, one after another, and Excel returns the result for the first test that is true.
IFS syntax
IFS is a function that checks a list of conditions in order and returns the value that goes with the first one that is true.
=IFS(test1, value1, test2, value2, test3, value3, ...)Each test is followed by the value to return when that test is true. You can supply up to 127 test and value pairs. All the tests sit at the same level, so there is only one closing parenthesis at the end, however many outcomes you have.
Here is the grading formula rewritten with IFS.
=IFS(B2>=90,"A",B2>=80,"B",B2>=70,"C",TRUE,"F")Read it from left to right:
- If B2 is 90 or more, return "A".
- Otherwise, if B2 is 80 or more, return "B".
- Otherwise, if B2 is 70 or more, return "C".
- Otherwise, return "F".
The last pair, TRUE,"F", needs explanation. It is covered in the next section.
The TRUE default
Unlike IF, IFS has no separate "value if false" argument. IF always gives you one final fallback. IFS does not. If none of the tests is true, IFS returns the error #N/A.
To get a fallback, add a last test that is always true. The logical value TRUE is always true, so a final pair of TRUE, "something" catches every value that did not match an earlier test. This pair plays the same role as the last false branch in a nested IF.
Compare what happens with and without it. Suppose B2 contains 65.
=IFS(B2>=90,"A",B2>=80,"B",B2>=70,"C")This returns #N/A, because 65 fails all three tests and nothing handles it. Adding the default fixes it.
=IFS(B2>=90,"A",B2>=80,"B",B2>=70,"C",TRUE,"F")Now 65 returns "F".
There is a second way that #N/A can appear. If B2 is empty, Excel treats it as 0 in a comparison, so it fails all the grade tests and falls through to the TRUE default. That returns "F" for a blank row, which is probably not what you want. The section on blank cells later in this chapter deals with that.
Order still matters
IFS checks the pairs from left to right and stops at the first true test. This is the same rule as nested IF, so the order of your tests matters in the same way.
Consider this version with the tests in the wrong order.
=IFS(B2>=70,"C",B2>=80,"B",B2>=90,"A",TRUE,"F")A score of 95 passes the first test, B2>=70, so the formula returns "C" and never looks at the later tests. A score of 85 also returns "C". Only scores below 70 reach the default. Every score of 70 or higher gets "C".
When your tests use >=, put the highest threshold first. When they use <= or <, put the lowest threshold first. Each test then only has to ask about its own boundary, because any value that passed an earlier test has already been handled.
The table below shows how a score of 85 moves through the correct formula.
| Test | Result for 85 | What happens |
|---|---|---|
| `B2>=90` | FALSE | Move to the next pair |
| `B2>=80` | TRUE | Return "B" and stop |
| `B2>=70` | Not checked | Skipped |
| `TRUE` | Not checked | Skipped |
The same sequence can be drawn as a flow, which is the same logic a nested IF follows.
Mixing in AND, OR, and NOT
Each test in an IFS formula can be any expression that evaluates to TRUE or FALSE, including AND, OR, and NOT. This is useful when the outcomes depend on more than one cell.
Suppose a shipping rule depends on the order total in B2 and the customer tier in D2:
- Gold or Platinum customers ship free.
- Other customers ship free when the order is 100 or more.
- Everyone else pays a flat 8.
=IFS(OR(D2="Gold",D2="Platinum"),0,B2>=100,0,TRUE,8)The first test covers the two tiers. The second test covers large orders from anyone who did not match the first. The default handles the remaining customers. Because the first two pairs return the same value, you could combine them into one test with OR, which is shorter and easier to maintain.
=IFS(OR(D2="Gold",D2="Platinum",B2>=100),0,TRUE,8)With only two outcomes, though, a plain IF is the better tool.
=IF(OR(D2="Gold",D2="Platinum",B2>=100),0,8)This shows an important point. IFS is not a replacement for every IF. It earns its place when there are three or more outcomes.
Returning calculations
The value in each pair does not have to be a fixed label. It can be a calculation, just as in IF. Here is a tiered commission rate applied to sales in B2.
=IFS(B2>=10000,B2*0.1,B2>=5000,B2*0.07,B2>=1000,B2*0.04,TRUE,0)A sale of 6,000 passes the second test, so the result is 6,000 × 0.07 = 420. A sale of 800 reaches the default and returns 0.
Hard-coded rates inside a formula are difficult to change later. A better design is to keep thresholds and rates in cells and refer to them with absolute references, as you did with a single threshold cell earlier in this tutorial.
=IFS(B2>=$F$1,B2*$G$1,B2>=$F$2,B2*$G$2,B2>=$F$3,B2*$G$3,TRUE,0)Now you can change a rate in one cell and every formula that depends on it updates. If the table grows beyond a handful of tiers, a lookup function is usually a better choice than IFS, but that is outside this chapter.
Handling blank cells with IFS
An empty cell compared with a number is treated as 0. In a grading formula, that makes a blank score return "F". Put a blank test first so that it is checked before anything else.
=IFS(A2="","",B2>=90,"A",B2>=80,"B",B2>=70,"C",TRUE,"F")Here, A2="" is true when A2 is empty. The formula returns an empty string and stops. The test refers to A2, the name column, so a row without a name is skipped. You could test B2 instead if the score column is the one that may be empty. Choose the cell whose emptiness means that the row has no data.
Blank tests belong at the start because IFS stops at the first true test. If the blank test came last, a blank score would already have matched TRUE,"F" or an earlier numeric test.
IFS compared with nested IF
Both approaches produce the same results when written correctly. The differences are mainly about readability and compatibility.
| Aspect | Nested IF | IFS |
|---|---|---|
| Reading order | Tests are buried inside false branches | Tests and values are listed in pairs |
| Closing parentheses | One per IF | Only one |
| Default result | The final false argument | A final `TRUE` test, which you must add yourself |
| No match | Cannot happen, as the last IF always has a false value | Returns `#N/A` if you omit the `TRUE` default |
| Maximum outcomes | Up to 64 nested levels | Up to 127 pairs |
| Excel versions | All versions | Excel 2019, Excel 2021, and Microsoft 365 only |
As a rule of thumb, use IF for one decision, IFS for three or more outcomes in a single column, and nested IF when your workbook has to open in Excel 2016 or earlier.
Common mistakes with IFS
- Leaving out the default. A value that matches no test returns
#N/A. AddTRUE, valueas the last pair. - Putting tests in the wrong order. With
>=tests, a lower threshold placed first captures higher values too. Sort from highest to lowest. - Supplying an odd number of arguments. IFS needs complete pairs. If you write a test with no value after it, Excel reports that you have entered too few arguments for this function.
- Forgetting quotes around text results.
B2>=90,Ais read as a reference to a name called A, and gives#NAME?. Write"A". - Using IFS in a shared file for older Excel versions. The recipient sees
#NAME?. Use nested IF if you cannot be sure of their version. - Trying to use
ELSE. IFS has no such keyword. TheTRUEpair does that job.
Practice formula: a ticket price
To put the pieces together, consider a rule for ticket prices based on age in B2. Children under 5 are free, those under 18 pay 6, those under 65 pay 12, and everyone else pays 8. A blank cell should leave the result empty.
=IFS(B2="","",B2<5,0,B2<18,6,B2<65,12,TRUE,8)Because the tests use <, they run from the lowest threshold to the highest. An age of 30 fails the blank test, fails <5, fails <18, and passes <65, so the result is 12. An age of 70 fails all of those and reaches the default, returning 8.
Exercise
A worksheet has scores in B2. You write =IFS(B2>=90,"A",B2>=80,"B",B2>=70,"C") and copy it down. A student with a score of 55 shows #N/A. What is the cause, and how do you fix the formula so that 55 returns "F"?
Show answer
IFS returns #N/A when none of its tests is true, and 55 fails all three. Add a final always-true pair as the default: =IFS(B2>=90,"A",B2>=80,"B",B2>=70,"C",TRUE,"F").
Exercise
Rewrite =IFS(B2>=70,"C",B2>=80,"B",B2>=90,"A",TRUE,"F") so that it grades correctly. What does the original return for a score of 95, and why?
Show answer
The original returns "C" for 95, because 95 passes the first test, B2>=70, and IFS stops at the first true test. Put the highest threshold first: =IFS(B2>=90,"A",B2>=80,"B",B2>=70,"C",TRUE,"F").
Exercise
Write an IFS formula for a delivery fee based on the distance in km in B2. Distances up to 5 cost 0, distances over 5 and up to 20 cost 4, distances over 20 and up to 50 cost 9, and anything farther costs 15. A blank B2 should return an empty string.
Show answer
=IFS(B2="","",B2<=5,0,B2<=20,4,B2<=50,9,TRUE,15). The blank test comes first, and the tests run from the lowest threshold to the highest because they use <=.