Sub topic 6
SWITCH default values and unmatched input
This chapter assumes you can enter formulas in Google Sheets, use cell references such as A2 and ranges such as A:A, and use the basic IF function. You should also have read the previous chapter on how SWITCH compares one expression with exact values. Here the focus is on what happens when nothing matches.
What SWITCH does when nothing matches
SWITCH takes one expression and then a list of pairs. Each pair is a case value and a result. The function compares the expression with each case value from left to right and returns the result of the first match.
=SWITCH(expression, case1, result1, case2, result2, ...)If no case matches and you gave no default, SWITCH returns #N/A. This formula has no default:
=SWITCH(A2,"NY","New York","CA","California","TX","Texas")If A2 contains FL, the cell shows #N/A. The formula is not broken. It is reporting that it had no answer for FL. In a column of 5,000 rows, a few of these errors can also spread into any total or lookup that depends on the column.
The default value
A default is one extra argument placed at the very end of the list. It has no case value in front of it. SWITCH returns it when nothing else matched.
=SWITCH(A2,"NY","New York","CA","California","TX","Texas","Unknown")With this formula, FL returns Unknown, and NY still returns New York. The formula now returns a value for every possible input.
How Sheets recognizes the default
Sheets does not use a keyword for the default. It counts arguments. After the expression, the arguments normally come in pairs. If one argument is left over at the end, SWITCH treats it as the default.
| Arguments after the expression | Meaning |
|---|---|
| 4 (two pairs) | No default. Unmatched input returns `#N/A`. |
| 5 (two pairs and one extra) | The last argument is the default. |
| 6 (three pairs) | No default. |
| 7 (three pairs and one extra) | The last argument is the default. |
This counting rule causes a common mistake. If you forget one result in the middle of the list, the pairs shift. A case value can then be read as a result, and the last result can be read as a default. The formula still runs, but it returns wrong values. If a SWITCH formula behaves strangely, count the arguments after the expression. An even count means there is no default, and an odd count means there is one.
The default must be the last argument. You cannot put it in the middle, because SWITCH would read it as a case value.
The next diagram shows the order of evaluation.
What the default can be
The default is not limited to text. It can be a number, an empty string, a cell reference, or a full formula. Each choice suits a different job.
A fixed label or number
A fixed value is the simplest choice. This formula gives a standard shipping fee by region code, with a flat fee for everything else:
=SWITCH(A2,"US",5,"CA",8,"UK",12,20)An empty string
If you want unmatched rows to look blank, use "" as the default:
=SWITCH(A2,"NY","New York","CA","California","")Be careful with this choice. A blank result looks the same as a row you intentionally left empty, so a bad input becomes hard to see. It is acceptable for presentation sheets and risky for working data.
The original input
A default can pass the input through unchanged. Using the expression itself as the default replaces the codes you know and leaves the rest as they were:
=SWITCH(A2,"NY","New York","CA","California",A2)This works well when you are cleaning data in stages. A value that is still a short code after the formula runs is one you have not mapped yet.
A visible flag
A default can also build a message that shows the unmatched value:
=SWITCH(A2,"NY","New York","CA","California","Unmapped: "&A2)If A2 is FL, the cell shows Unmapped: FL. You can search the column for Unmapped and see exactly which values need a new case. For data you plan to rely on, this is usually better than a silent Unknown.
Defaults with other functions
The expression can be any formula, so the default applies to the result of that formula. This example classifies days of the week:
=SWITCH(WEEKDAY(A2),1,"Sunday",7,"Saturday","Weekday")WEEKDAY returns 1 for Sunday and 7 for Saturday by default. The cases handle those two days, and the default "Weekday" covers the other five. Writing one default instead of five more cases shortens the formula. This approach only works when the default describes a real group. If you later add a case for another day, the default no longer means what its label says, so check it when you edit the formula.
The same idea works for any grouping where one or two values are special and the rest share a result.
Grouping several inputs under one result
SWITCH has no way to give several case values the same result in one pair. Each case needs its own pair, so the result is repeated:
=SWITCH(A2,"NY","East","NJ","East","CA","West","OR","West","WA","West","Other")For long lists, this repetition gets heavy. Two alternatives are a lookup table with VLOOKUP or XLOOKUP, or SWITCH(TRUE, ...) with a test such as OR. Use the table approach when the mapping has more than about a dozen entries or when other people need to edit it without touching a formula. The SWITCH form is better for a short, stable list that you want to read at a glance inside the cell.
Unmatched input that is not really unmatched
Many #N/A results from SWITCH and many Unknown results do not mean the value is missing from your list. The value is there, but it does not equal any case. Four causes are common.
Extra spaces
"NY " with a trailing space is not equal to "NY". Data pasted from other systems often has these spaces. Wrap the expression in TRIM:
=SWITCH(TRIM(A2),"NY","New York","CA","California","Unknown")Different capitalization
If your source mixes ny, Ny, and NY, normalize the text instead of relying on how Sheets compares case. Using UPPER and writing every case value in capitals makes the result predictable:
=SWITCH(UPPER(TRIM(A2)),"NY","New York","CA","California","Unknown")Numbers stored as text
The number 1 and the text "1" look the same in a cell but are different types, so a case value of 1 does not match text "1". Check by comparing a cell with =ISNUMBER(A2). If it returns FALSE for a value that looks like a number, convert it with VALUE(A2) or fix the source column.
Blank cells
An empty cell is another input that may not match what you expect. If blanks are common, test them first rather than leaning on the default. Wrap the formula in an IF:
=IF(A2="","",SWITCH(TRIM(A2),"NY","New York","CA","California","Unknown"))Now blank rows stay blank, and Unknown appears only for a real value you have not mapped.
Errors in the expression are not caught by the default
The default handles one situation: the expression produced a value and no case matched. If the expression itself returns an error, SWITCH passes that error through. The default does not replace it.
Suppose A2 contains #N/A because of a failed lookup. In that case, this formula returns #N/A, not "Unknown":
=SWITCH(A2,"NY","New York","CA","California","Unknown")To handle errors in the expression, wrap the formula in IFERROR or IFNA. The later chapters on those functions cover this in detail. For now, keep the two ideas separate. A default covers an unmatched value, and an error wrapper covers a failed calculation.
Choosing a default on purpose
The default is a decision about how your sheet should behave when it meets data you did not plan for. Three questions help you pick it:
- Should bad input stop the work or be tolerated? If a wrong value would cause wrong totals, prefer a loud default such as
"Unmapped: "&A2, or even leave out the default so#N/Ashows. - Will a person read the cell, or will another formula use it? A text label inside a column that feeds a
SUMor a chart can distort results. A number column should usually have a number default, or an empty string only if every downstream formula expects blanks. - Is the default itself a valid category? A default of
"Other"is a real group and fits a report. A default of"Unknown"says that something went wrong, and it fits a data-cleaning sheet.
Leaving out the default is a legitimate choice. In a sheet where every input must be in your list, #N/A is a useful alarm. A friendly default would hide the problem, which is the same hazard described later for IFERROR.
A worked example
You have order codes in column A, and you want a status label in column B. The statuses are P for pending, S for shipped, and D for delivered. Some rows are blank, and some contain lowercase letters or extra spaces. Put this formula in B2 and fill it down:
=IF(A2="","",SWITCH(UPPER(TRIM(A2)),"P","Pending","S","Shipped","D","Delivered","Unmapped: "&A2))Here is what each input returns:
| A2 | B2 |
|---|---|
| `P` | `Pending` |
| ` s ` | `Shipped` |
| `d` | `Delivered` |
| `X` | `Unmapped: X` |
| (blank) | (blank) |
The formula cleans the input first, handles blanks separately, maps the known codes, and flags everything else. A short check such as =COUNTIF(B:B,"Unmapped*") then tells you how many rows still need attention.
Exercise
A formula reads =SWITCH(A2,"gold",0.2,"silver",0.1,"bronze",0.05). Cell A2 contains "platinum". (a) What does the formula return? (b) Rewrite it so that unmatched tiers get a 0 discount. (c) A2 now contains "Gold " with a trailing space and a capital G, and you still get the default. Which two functions should wrap A2 to fix it?
Show answer
(a) It returns #N/A, because no case matches and there is no default. (b) =SWITCH(A2,"gold",0.2,"silver",0.1,"bronze",0.05,0). The final 0 is the unpaired last argument, so it is the default. (c) Wrap A2 in TRIM to remove the trailing space and in LOWER to match the lowercase case values: =SWITCH(LOWER(TRIM(A2)),"gold",0.2,"silver",0.1,"bronze",0.05,0).
Exercise
You build =SWITCH(B2,1,"Low",2,"Medium",3,"High","Unknown"), but B2 contains the text "2" imported from a CSV file. What do you get, and how can you fix it without changing the case values?
Show answer
You get "Unknown", because the text "2" is not equal to the number 2. Convert the input to a number by using VALUE: =SWITCH(VALUE(B2),1,"Low",2,"Medium",3,"High","Unknown"). Fixing the source column so it holds real numbers also works.