This article introduces logical functions and their uses in Tableau. It also demonstrates how to create a logical calculation using an example.
Why use logical calculations
Logical calculations allow you to determine if a certain condition is true or false [boolean logic]. For example, you might want to quickly see if sales for each country you distribute your merchandise to were above or below a certain threshold.
The logical calculation might look something like this:
SUM[Sales] > 1,000,000
Logical functions available in Tableau:
Function
Syntax
Description
IN IN
Returns TRUE if any value in matches any value in
.
The values in can be a set, list of literal values, or combined field.
Examples:
SUM[[Cost]] IN [1000, 15, 200]
[Field] IN [SET]
AND
IF AND THEN END
Performs a logical conjunction on two expressions.
Example:
IF [ATTR[[Market]] = "New Business" AND SUM[[Sales]] > [Emerging Threshold] ]THEN "Well Performing"
CASE WHEN THEN WHEN THEN ... ELSE END
Performs logical tests and returns appropriate values. The CASE function evaluates IN
0, compares it to a sequence of values, IN
1, IN
2, etc., and returns a result. When a value that matches IN
0 is encountered, CASE returns the corresponding return value. If no match is found, the default return expression is used. If there is no default return and no values match, then Null is returned.
CASE also supports WHEN IN construction, such as
IN
4
The values that WHEN IN compare to must be a set, list of literal values, or combined field.
Additional notes
- CASE versus IF: CASE is often easier to use than IIF or IF THEN ELSE. Typically, an IF function performs a sequence of arbitrary tests, and a CASE function searches for a match to an expression. But a CASE function can always be rewritten as an IF function, although the CASE function will generally be more concise.
- CASE versus groups: Many times you can use a group to get the same results as a complicated CASE function. You may want to see which is more performant for your scenario.
Examples:
IN
5
IN
6
IN
7Tests a series of expressions returning the value for the first true .
Example:
IN
8
IN
9Tests a series of expressions returning the value for the first true .
Example:
0
IN
9Tests a series of expressions returning the value for the first true . Must be placed at the end of an expression.
Example:
0
IN
9Tests a series of expressions returning the value for the first true .
Example:
0
5Returns if it is not null, otherwise returns .
Example:
6
7Checks whether a condition is met, and returns one value if TRUE, another value if FALSE, and an optional third value or NULL if unknown.
Example:
8
9Returns true if a given string is a valid date.
Example:
0
1Returns true if the expression is NULL [does not contain valid data].
Example:
2
3 or
4Returns the maximum of a single expression across all records or the maximum of two expressions for each record.
Example:
5
6 or
7Returns the minimum of an expression across all records or the minimum of two expressions for each record.
Example:
8
9Performs logical negation on an expression.
Example:
0
1Performs a logical disjunction on two expressions.
Example:
2
3Tests a series of expressions returning the value for the first true .
Example:
4
5Finds the first that matches and returns the corresponding .
Example:
6
7Returns if it is not null, otherwise returns zero.
Example:
8
Note: some of these are actually logical operators and appear in black, not blue. For more information, see .
Create a logical calculation
Follow along with the steps below to learn how to create a logical calculation.
In Tableau Desktop, connect to the Sample - Superstore saved data source, which comes with Tableau.
Navigate to a worksheet.
From the Data pane, drag State to the Rows shelf.
From the Data pane, drag Category to the Rows shelf and place it to the right of State.
From the Data pane, drag Sales to the Columns shelf.
Select Analysis > Create Calculated Field.
In the calculation editor that opens, do the following:
Name the calculated field, KPI.
Enter the following formula:
9
This calculation quickly checks if a member is great than zero. If so, it returns true; if not, it returns false.
When finished, click OK.
The new calculated field appears under Measures in the Data pane. Just like your other fields, you can use it in onr or more visualizations.