Sub topic 5
SWITCH: mapping exact values to results
This chapter assumes you can enter Google Sheets formulas, use cell references such as A2, and write basic IF formulas. You will use SWITCH when one cell contains a code or category and you need to return a matching label.
How SWITCH works
SWITCH evaluates one expression, then compares its result with a list of exact case values. When it finds a match, it returns the result paired with that case.
=SWITCH(expression, case1, result1, case2, result2, default)The arguments work in pairs:
expressionis the value to test.- Each
caseis a possible exact value. - Each
resultis returned when the preceding case matches. defaultis returned when no case matches.
Suppose A2 contains a two-letter state code:
| A | B |
|---|---|
| NY | New York |
Enter this formula in B2:
=SWITCH(A2,"NY","New York","CA","California","TX","Texas","Unknown")If A2 contains NY, the formula returns New York. If it contains TX, it returns Texas. If it contains another code, it returns Unknown.
The expression is evaluated once. This often makes SWITCH easier to read than repeating the same cell reference in several nested IF tests.
Exact matches, not ranges
SWITCH is designed for discrete values such as codes, statuses, and categories. It does not interpret >=, <, or other comparison operators as range tests in its ordinary form.
For example, this formula checks whether A2 contains the number 1, 2, or 3:
=SWITCH(A2,1,"Low",2,"Medium",3,"High","Invalid level")A value of 2 returns Medium. A value of 2.5 does not match either 2 or 3, so the formula returns Invalid level.
The case values and the expression should use compatible types. A number and text that looks like that number can behave differently. If a cell contains the text "2" rather than the number 2, test the actual value in your sheet and normalize the data when necessary.
Make the formula maintainable
Use a default result when unmatched input is possible. Without a default, SWITCH returns #N/A if no case matches. A clear default helps identify unexpected input without leaving a cryptic error in the output.
Keep related cases together and use result labels that explain the meaning of each code. For longer mappings, a lookup table may be easier to maintain than a very long formula. SWITCH is most useful when the mapping is short and stable.
Choosing SWITCH instead of nested IF
A nested IF is useful when each decision involves a different logical test. For example, score bands require comparisons such as B2>=90. SWITCH is clearer when one expression must map to exact alternatives:
=IF(A2="NY","New York",IF(A2="CA","California","Unknown"))The equivalent SWITCH formula puts the tested expression in one place:
=SWITCH(A2,"NY","New York","CA","California","Unknown")This structure is easier to scan when the number of exact values grows. Use IFS in a later step when the decision depends on ordered conditions or ranges rather than exact matching.
Exercise
Write a formula that converts the value in C2 from "P", "F", or "I" to "Passed", "Failed", or "Incomplete", and returns "Unknown status" for any other value.
Show answer
=SWITCH(C2,"P","Passed","F","Failed","I","Incomplete","Unknown status")