SWITCH
The SWITCH
function in Google Sheets tests an expression against a list of cases and returns the corresponding value of the first matching case, with an optional default value if nothing else is met. It is a powerful tool to perform conditional operations on your data. Learn how to effectively use the SWITCH
function with this comprehensive guide.
Function Syntax and Parameters
Syntax: SWITCH(expression, case1, value1, [default or case2, value2], ...)
Parameters:
expression
: The value or cell reference that will be tested against the cases.case1
,case2
, ...: The values that will be compared against the expression.value1
,value2
, ...: The corresponding values to return if the expression matches the respective case.
Step-by-Step Tutorial
-
Using
SWITCH
with multiple cases:- Example:
=SWITCH(A1, "apple", 1, "orange", 2, "banana", 3)
- Result: Returns 1 if A1 equals "apple", 2 if A1 equals "orange", 3 if A1 equals "banana". If none of the cases match, it returns an error.
- Example:
-
Using
SWITCH
with a default value:- Example:
=SWITCH(A1, "apple", 1, "orange", 2, "banana", 3, "other")
- Result: Returns 1 if A1 equals "apple", 2 if A1 equals "orange", 3 if A1 equals "banana", and "other" if none of the cases match.
- Example:
Use Cases and Scenarios
- Grade Conversion: Convert numeric scores to letter grades.
- Product Categorization: Assign categories based on product attributes.
- Customer Segmentation: Assign customers to specific segments based on their demographics.