Use the SWITCH function in your formulas to create several different conditional expressions as an alternative to nesting IF THEN ELSE functions. This article describes the function and provides examples. For a list of all Explore functions, see Explore functions reference.
This article contains the following topics:
About the SWITCH function
The SWITCH function uses the following format:
SWITCH testedElement { CASE value1: returnValue CASE value2: returnValue DEFAULT: defaultReturnValue }
SWITCH tests if the case values exist within the tested element. Your tested element can be a metric, attribute, or calculation. If your case value exists within the tested element, then the return value is displayed. If it does not, your default value is displayed. If your case value does not exist in the tested element and there is no entered default value, "NULL" is returned.
SWITCH is often used to insert metrics such as targets and goals into datasets. They can then be used in other calculations or in comparison visuals such as KPI, bullet, and gauge charts.
Using the SWITCH function
This example uses the SWITCH function to show first reply time targets for each ticket type. You can duplicate this example using any attribute and target number.
To insert numbers into your data
- Open a new report using the Support: Tickets dataset.
- In the calculations menu (), click Standard calculated metric.
- Name your calculated metric. This example uses Target FTR.
- Under Functions, click Add.
- Select the SWITCH function and click +.
- Double-click _tested_element to highlight it.
- Select the attribute containing the values you want to test from the Fields drop-down list or type in the attribute name. This example uses the Ticket type attribute.
- In CASE _value1, enter the value you're testing for.
- In _return_value, enter the result returned if the value is true. This example uses the value of the Ticket type attribute to test and the first reply time targets as the returned result.
- Enter your other cases. If you have more than two cases, you will need to manually
type out additional CASE expressions. The formula for this example looks like the image
below.
- Click Save.
- Click the + button on metrics to add your calculated metric to your report.
- If your tested element is an attribute, click the + button on any attribute location and select your tested element.