Sub topic 5
SWITCH: mapping exact values to results
This chapter covers the SWITCH function: how it works, how it compares values, and where it goes wrong. It assumes you can enter and edit formulas in Google Sheets, use cell references such as A2 and ranges such as A:A, know the comparison operators, and have used IF. Chapter 3 introduced IFS for range-based conditions. SWITCH solves a different problem: turning exact values into other values.
The problem SWITCH solves
Suppose column A holds two-letter state codes and you want the full state name. With IF you would write one test per code and nest them:
=IF(A2="NY","New York",IF(A2="CA","California",IF(A2="TX","Texas","Unknown")))Every branch repeats A2=. Only the code and the result change. SWITCH states the tested value once and then lists pairs of value and result:
=SWITCH(A2,"NY","New York","CA","California","TX","Texas","Unknown")Both formulas return the same thing. The SWITCH version has no nesting and no closing-parenthesis chain, and you can read it as a lookup table.
Syntax
=SWITCH(expression, case1, result1, [case2, result2, ...], [default])expressionis the value to test. It can be a cell reference, a literal, or another formula.case1,case2, and so on are the values to compare against the expression.result1,result2, and so on are what SWITCH returns when the matching case is found.defaultis optional. It is a final argument that has no case before it.
SWITCH evaluates the expression once, then compares it to each case in order. It returns the result paired with the first case that equals the expression and stops looking.
The diagram shows that flow.
A worked example
Here is a small order sheet. Column B holds a status code, and you want a readable label in column C.
| Row | A (Order) | B (Status code) | C (Label, formula result) |
|---|---|---|---|
| 2 | 1001 | P | Pending |
| 3 | 1002 | S | Shipped |
| 4 | 1003 | D | Delivered |
| 5 | 1004 | X | Unknown status |
The formula in C2, filled down:
=SWITCH(B2,"P","Pending","S","Shipped","D","Delivered","Unknown status")Count the arguments. There are five after the expression: three case and result pairs, plus one lone value at the end. An even number of arguments after the expression means every case has a result and there is no default. An odd number means the last one is the default. When a SWITCH formula gives unexpected output, count the arguments first.
How matching works
SWITCH uses an equality test, like = in a comparison. It cannot test "greater than" or "less than." That limit defines when SWITCH is the right tool. Several details follow from it.
Text matching ignores case
In Google Sheets, "ny", "NY", and "Ny" all match the case "NY". If you need a case-sensitive match, SWITCH is the wrong tool. Wrap the comparison in EXACT and use IFS or IF.
Types must agree
The number 7 and the text "7" are different values. If column A holds numbers that were imported as text, a case of 7 will not match them. The formula falls through to the default or returns #N/A, and nothing in the output tells you the cause was a type mismatch.
=SWITCH(A2,1,"Low",2,"Medium",3,"High","Other")If A2 contains the text "2" instead of the number 2, this returns "Other". To tolerate both, convert the input first:
=SWITCH(VALUE(A2),1,"Low",2,"Medium",3,"High","Other")VALUE turns numeric text into a number. It returns #VALUE! if the text is not numeric, so use it only when you know the column should hold numbers.
Spaces count
"NY " with a trailing space does not equal "NY". Data pasted from other systems often has stray spaces. Wrapping the expression in TRIM fixes this:
=SWITCH(TRIM(A2),"NY","New York","CA","California","Unknown")Blank cells
A blank cell is not equal to "X", but it can equal an empty string. If you want to handle blanks explicitly, add a case for "":
=SWITCH(A2,"","(blank)","NY","New York","CA","California","Unknown")Without that case, a blank cell goes to the default.
The expression can be a formula
The expression does not have to be a plain cell. Because SWITCH evaluates it once, you can put another function there and match against its result. WEEKDAY returns 1 for Sunday through 7 for Saturday:
=SWITCH(WEEKDAY(A2),1,"Sunday",7,"Saturday","Weekday")This groups the two weekend days under their names and sends the other five days to the default. Because the expression is evaluated one time, a costly formula there is not recalculated for every case. Compare that with an IF chain, where WEEKDAY(A2) would be repeated in each test.
Cases and results can be references
Cases and results are not limited to typed constants. They can be cell references, which lets you keep the mapping visible on the sheet instead of buried in a formula:
=SWITCH(B2,$F$2,$G$2,$F$3,$G$3,$F$4,$G$4,"Unknown status")This works for a handful of codes, but it gets long. For more than five or six pairs, a lookup table with VLOOKUP, XLOOKUP-style INDEX/MATCH, or a similar function scales better, because adding a row to the table needs no formula edit. SWITCH is best when the list is short and stable.
Results can also be calculations, not only labels:
=SWITCH(B2,"Gold",A2*0.8,"Silver",A2*0.9,A2)Here A2 is a price. Gold customers pay 80 percent, Silver customers pay 90 percent, and everyone else pays the full price, which the default supplies.
No match and no default
If no case matches and you did not supply a default, SWITCH returns #N/A. The next chapter covers defaults in detail, but you should know the failure mode now:
=SWITCH(B2,"P","Pending","S","Shipped","D","Delivered")For a status of "X", this returns #N/A. That can be useful, since it flags data you did not expect. It is also a common cause of error values appearing in a report. Decide deliberately which behavior you want.
SWITCH compared with IF and IFS
| Need | Best fit | Why |
|---|---|---|
| One yes/no decision | IF | Simplest form |
| Ranges such as score bands | IFS | SWITCH only tests equality |
| Several exact values of one cell | SWITCH | The tested value is written once |
| Different cells tested in each branch | IFS | SWITCH tests a single expression |
| Many codes that change often | Lookup table | Edit data, not the formula |
The full decision process comes in a later chapter. For now, remember the key distinction. SWITCH asks "what does this one value equal?" and IFS asks "which of these separate conditions is true?"
Common mistakes
- Using a comparison operator as a case.
=SWITCH(B2,">=90","A","F")compares B2 to the literal text">=90". It does not test whether B2 is at least 90. Use IFS for ranges. - Expecting every case to be tested. SWITCH stops at the first match. If the same case appears twice, the second result is never used.
- Forgetting that types matter. Text "5" and number 5 do not match. Check with
=ISNUMBER(A2)if a numeric case seems not to work. - Leaving out the default and then being surprised by #N/A on new data.
- Mixing result types. A formula may return text for some cases and numbers for others. Sheets allows this, but sorting, summing, and formatting that column later gets harder. Keep results consistent when you can.
Testing a SWITCH formula
Before you fill a formula down hundreds of rows, test each branch. Create a few rows with one example for every case, plus one unexpected value and one blank. Check that each returns what you expect. Then check the count of results with a quick formula such as =COUNTIF(C2:C100,"Unknown status"), which tells you how many rows hit the default. A high count often means a data problem such as stray spaces or text-formatted numbers, and the SWITCH formula itself is fine.
Exercise
Column B holds product codes. Write a formula for C2 that returns "Small" for "S", "Medium" for "M", and "Large" for "L". Any other code, including codes with stray spaces such as "M ", should return "Check code" only if it is truly not one of the three. Which formula does that?
Show answer
=SWITCH(TRIM(B2),"S","Small","M","Medium","L","Large","Check code") — TRIM removes leading and trailing spaces so "M " matches "M", and the final lone argument "Check code" is the default for anything else. The match is not case-sensitive, so "m" also returns "Medium".
Exercise
A colleague writes =SWITCH(A2,"NY","New York","CA","California","TX") and says it returns #N/A for Texas rows. What does it actually return for A2 = "TX" and for A2 = "FL", and how would you fix it?
Show answer
It returns "TX" for both. The lone final argument "TX" is treated as the default, so every unmatched value, including "TX" and "FL", returns the text "TX". It never returns #N/A. The fix is to add the missing result and a real default: =SWITCH(A2,"NY","New York","CA","California","TX","Texas","Unknown").
Exercise
Why can =SWITCH(B2,">=90","A","F") not grade a score of 95, and which function should you use instead?
Show answer
SWITCH tests for equality only. It compares B2 to the literal text ">=90", which is never equal to 95, so the formula returns "F". Use IFS instead, for example =IFS(B2>=90,"A",TRUE,"F").