Conditional Expressions
This cheat sheet provides a quick reference guide for various conditional expressions commonly used in data analysis and programming. Each expression is accompanied by its syntax, a sample usage, and a brief description.
IF
The IF function in programming and spreadsheet formulas provides a way to perform conditional operations. It evaluates a condition and returns a value if the condition is TRUE
, or another value if the condition is FALSE
.
Syntax
IF(expr, successCase, elseCase)
Sample
IF({field} > 1, Value1, Value2)
Output
- `Value1` if `{field} > 1` evaluates to TRUE
- `Value2` otherwise
SWITCH
The SWITCH function is a versatile tool for handling multiple cases. It evaluates the given expression (expr) against a series of patterns and returns the corresponding value of the first matching pattern. If none match, it returns the default value.
Syntax
SWITCH(expr, [pattern, value, ..., default])
Sample
SWITCH({field}, 1, 'One', 2, 'Two', '--')
Output
Switch case value based on the output of `{field}`:
- `'One'` if `{field} = 1`
- `'Two'` if `{field} = 2`
- `'--'` for the default case
AND
The AND function is a logical operator that returns TRUE only if all its conditions are true.
Syntax
AND(expr1, [expr2,...])
Comparison operators : ==
, !=
, >
, <
, >=
, <=
Sample
AND({field} > 2, {field} < 10)
Output
TRUE if both `{field} > 2` and `{field} < 10` evaluate to TRUE
OR
The OR function is another logical operator, returning TRUE if at least one of its conditions is true.
Syntax
OR(expr1, [expr2,...])
Comparison operators : ==
, !=
, >
, <
, >=
, <=
Sample
OR({field} > 2, {field} < 10)
Output
TRUE if at least one of the conditions `{field} > 2` or `{field} < 10` evaluates to TRUE
Logical operators, along with Numerical operators can be used to build conditional expressions
.
Examples:
IF({marksSecured} > 80, "GradeA", "GradeB")
SWITCH({quarterNumber},
1, 'Jan-Mar',
2, 'Apr-Jun',
3, 'Jul-Sep',
4, 'Oct-Dec',
'INVALID'
)