Sub topic 4
IFS order and a TRUE fallback
This chapter covers two habits that decide whether an IFS formula returns the right answer: the order of the conditions, and a final catch-all condition. It assumes you can enter and edit formulas in Google Sheets and use cell references such as B2. It also assumes you know the comparison operators (=, <>, >, >=, <, <=) and the basic IF function. You should have seen IFS as a list of condition and value pairs.
How IFS reads its arguments
IFS evaluates its condition and value pairs from left to right. It stops at the first condition that is true and returns the value paired with it. It never looks at the pairs after that one.
That rule has two consequences.
- Order matters when more than one condition can be true for the same input.
- If no condition is true, IFS has nothing to return, so it returns an error.
Here is the grade formula from the previous chapter, with the conditions listed from highest to lowest:
=IFS(B2>=90,"A",B2>=80,"B",B2>=70,"C",TRUE,"F")Suppose B2 holds 85. The test B2>=90 is false, so IFS moves on. The test B2>=80 is true, so IFS returns "B". It does not evaluate B2>=70, even though 85 is also at least 70.
Why the order of conditions changes the result
When the conditions overlap, the first true condition wins. A score of 95 is at least 90, at least 80, and at least 70. All three tests are true, but only the first one in the list counts.
Compare the correct formula with one that has the same conditions in the opposite order:
=IFS(B2>=70,"C",B2>=80,"B",B2>=90,"A",TRUE,"F")Now take B2 = 95. The first test, B2>=70, is true. IFS returns "C" and stops. The "B" and "A" conditions are unreachable for any score, because anything that passes B2>=80 or B2>=90 has already passed B2>=70.
The formula shows no error and no warning. It simply returns wrong values, which makes this one of the harder mistakes to notice.
| Score in B2 | Highest-first order | Lowest-first order |
|---|---|---|
| 95 | A | C |
| 85 | B | C |
| 72 | C | C |
| 50 | F | F |
Only the scores that fall below 70 come out right in both versions. That is why a quick test with one low score can make a broken formula look fine.
A rule for ordering
For thresholds, order the conditions from the most restrictive to the least restrictive.
- With "greater than or equal to" tests, list the highest threshold first.
- With "less than or equal to" tests, list the lowest threshold first.
- If the conditions never overlap (for example,
A2="Open"andA2="Closed"), the order does not change the result. You can then order them for readability or by how common each case is.
Here is a "less than" version that works in ascending order:
=IFS(B2<60,"F",B2<70,"C",B2<80,"B",B2<90,"B+",TRUE,"A")Each test here assumes the earlier ones failed. When B2<70 is checked, the score is already known to be 60 or higher. This chaining means each condition can be shorter than a stand-alone test. It also means you cannot move a line around without checking what it relied on.
Note that the boundary operators must match your intent. A score of exactly 90 returns "A" with >=90 but would fall through to "B" with >90. Decide which side of each boundary an exact value belongs to before you write the formula.
What happens when nothing matches
IFS does not have a built-in default. If every condition is false, it returns #N/A. Look at this formula:
=IFS(B2>=90,"A",B2>=80,"B")A score of 85 returns "B", and 95 returns "A". A score of 60 matches neither condition, so the cell shows #N/A. The cell is not empty and it is not "wrong" in the sense of a typo. The formula has no instruction for that case.
The same happens with unexpected inputs:
- A blank cell. In a comparison, a blank is treated as 0, so
B2>=90is false. - Text such as "absent". Text compared with a number using
>=gives a result that is not a passing score, so the tests do not match as you intended. - A score above a limit you did not plan for.
The TRUE fallback
The fix is to add a final pair whose condition always holds. TRUE is a condition that is true for every input, so as the last pair it catches whatever the earlier conditions did not.
=IFS(B2>=90,"A",B2>=80,"B",B2>=70,"C",TRUE,"F")Now a score of 60 reaches the last pair, TRUE is true, and IFS returns "F". There is no input left over that can produce #N/A.
Two details about the fallback:
- It must be last. Because IFS stops at the first true condition, a
TRUEplaced earlier would catch every input and the pairs after it would never run. - The fallback value should say what the leftover case means. "F" is correct for a grade scale where everything below 70 fails. For messy data you may prefer a visible marker so you can find problem rows later.
=IFS(B2>=90,"A",B2>=80,"B",B2>=70,"C",B2>=0,"F",TRUE,"Check score")In this version, "F" applies only to scores from 0 to 69. Anything else, such as a negative number, goes to "Check score". Text and blank cells need care. In Google Sheets a blank cell compares as 0, so a blank reaches B2>=0 and returns "F". If you want blanks to be flagged, test for them first:
=IFS(B2="","No score",B2>=90,"A",B2>=80,"B",B2>=70,"C",B2>=0,"F",TRUE,"Check score")Here the blank test comes first for the same ordering reason as before: a blank would otherwise pass a later numeric test and be graded.
Choosing the fallback deliberately
A TRUE fallback turns an error into a value. That is useful when the leftover case has one clear meaning, as with "everything below 70 is F". It is less useful when the leftover case means "this data is wrong". In that case a loud result is better than a quiet default:
| Situation | Fallback to use |
|---|---|
| Every unmatched value has the same meaning | `TRUE,"F"` |
| Unmatched values mean bad data | `TRUE,"Check score"` |
| You want errors to stay visible during building | No fallback, accept `#N/A` |
Leaving off the fallback while you build is a legitimate choice. The #N/A tells you a case is uncovered. Add the fallback once you know what that case should return.
Overlapping ranges and gaps
Two design problems appear in longer IFS formulas.
Overlaps. If two conditions can both be true, order decides the winner. Check each boundary value and make sure the result is what you want.
Gaps. A gap is an input that no condition covers. With the TRUE fallback there is no #N/A, but a gap can still produce the wrong label. Suppose you write:
=IFS(B2>=90,"A",B2>=80 ,"B",B2>=70,"C",B2<60,"F",TRUE,"Check score")A score of 65 is not at least 70 and not below 60, so it falls to "Check score". That might be the result of a typo (<60 instead of <70). The fallback hides the typo by returning a plausible-looking label. To test a formula, try the boundary values on both sides: 59, 60, 69, 70, 79, 80, 89, 90.
Test your formula
A short test table makes ordering errors easy to see. Put sample scores in column B and the formula in column C:
| Row | A | B | C |
|---|---|---|---|
| 1 | Name | Score | Grade |
| 2 | Ana | 95 | `=IFS(B2>=90,"A",B2>=80,"B",B2>=70,"C",TRUE,"F")` |
| 3 | Ben | 90 | same formula, row 3 |
| 4 | Cai | 89 | same formula, row 4 |
| 5 | Dee | 70 | same formula, row 5 |
| 6 | Eli | 69 | same formula, row 6 |
Expected results are A, A, B, C, and F. If row 3 returns "B", check whether you typed >90 instead of >=90. If row 2 returns anything other than "A", check whether the conditions are in descending order.
You can also fill the formula down a whole column with one ARRAYFORMULA wrapper, but IFS does not always handle full-range inputs the way IF does, so test it on a small range first.
Common mistakes
- Wrong order for overlapping conditions. The formula returns a valid but wrong value, with no error.
- No fallback. Any unmatched input returns
#N/A. TRUEplaced before other pairs. Everything after it is unreachable.- Odd number of arguments. IFS needs pairs. A condition without a value returns an error about the wrong number of arguments.
- Boundary operator mismatch.
>and>=differ for exact values. - Text in a numeric column. A score entered as text, such as "85" with a leading apostrophe, may not compare the way you expect. Check the cell with
ISNUMBER(B2)if results look odd.
Summary
IFS returns the value of the first true condition, so list overlapping conditions from most restrictive to least restrictive. Add TRUE with a fallback value as the last pair so that unmatched input does not return #N/A. Choose the fallback to match what the leftover case means, and test boundary values on both sides of each threshold.
Exercise
A formula assigns shipping tiers: =IFS(C2>=0,"Standard",C2>=50,"Free",C2>=100,"Free + gift",TRUE,"Invalid"). An order of 120 returns "Standard". Explain why, and write a corrected formula that returns "Invalid" for negative values.
Show answer
IFS returns the value for the first true condition. For 120, C2>=0 is true, so IFS returns "Standard" and never reaches the other tests. Put the most restrictive threshold first: =IFS(C2>=100,"Free + gift",C2>=50,"Free",C2>=0,"Standard",TRUE,"Invalid"). Negative values fail all three tests and reach the TRUE fallback, which returns "Invalid".
Exercise
What does =IFS(B2>=90,"A",B2>=80,"B") return when B2 is 72, and what is the smallest change that makes it return "C" for scores from 70 to 79 and "F" for everything else?
Show answer
It returns #N/A, because no condition is true and IFS has no default. Add two pairs at the end: =IFS(B2>=90,"A",B2>=80,"B",B2>=70,"C",TRUE,"F"). The B2>=70 pair returns "C" for 70 to 79 (higher scores were already matched earlier), and the TRUE pair returns "F" for the rest.