Logical Functions
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 | <expr1> IN <expr2>
|
Returns TRUE if any value in The values in Examples:
|
AND |
IF <expr1> AND <expr2> THEN <then> END
|
Performs a logical conjunction on two expressions. Example:
|
CASE | CASE <expression> WHEN <value1> THEN <return1> WHEN
<value2> THEN <return2> ... ELSE <default return> END
|
Performs logical tests and returns
appropriate values. The CASE function evaluates CASE also supports WHEN IN construction, such as
The values that WHEN IN compare to must be a set, list of literal values, or combined field. Additional notes
Examples:
|
ELSE | IF <expr> THEN <then> ELSE <else> END
|
Tests a series of expressions returning the <then> value for the first true <expr>. Example:
|
ELSEIF | IF <expr> THEN <then> [ELSEIF <expr2> THEN <then2>...] [ELSE <else>] END
|
Tests a series of expressions returning the <then> value for the first true <expr>. Example:
|
END | IF <expr> THEN <then> [ELSEIF <expr2> THEN <then2>...] [ELSE <else>] END
|
Tests a series of expressions returning the <then> value for the first true <expr>. Must be placed at the end of an expression. Example:
|
IF | IF <expr> THEN <then> [ELSEIF <expr2> THEN <then2>...] [ELSE <else>] END
|
Tests a series of expressions returning the <then> value for the first true <expr>. Example:
|
IFNULL | IFNULL(expr1, expr2)
|
Returns <expr1> if it is not null, otherwise returns <expr2>. Example:
|
IIF | IIF(test, then, else, [unknown])
|
Checks 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:
|
ISDATE | ISDATE(string)
|
Returns true if a given string is a valid date. Example:
|
ISNULL | ISNULL(expression)
|
Returns true if the expression is NULL (does not contain valid data). Example:
|
MAX | MAX(expression) or Max(expr1, expr2) |
Returns the maximum of a single expression across all records or the maximum of two expressions for each record. Example:
|
MIN | MIN(expression) or MIN(expr1, expr2) |
Returns the minimum of an expression across all records or the minimum of two expressions for each record. Example:
|
NOT | IF NOT <expr> THEN <then> END
|
Performs logical negation on an expression. Example:
|
OR | IF <expr1> OR <expr2> THEN <then> END
|
Performs a logical disjunction on two expressions. Example:
|
THEN | IF <expre> THEN <then> [ELSEIF ,expr2> THEN <then2>...] [ELSE <else>] END
|
Tests a series of expressions returning the <then> value for the first true <expr>. Example:
|
WHEN | CASE <expr> WHEN <Value1> THEN <return1> ... [ELSE <else>] END
|
Finds the first <value> that matches <expr> and returns the corresponding <return>. Example:
|
ZN | ZN(expression)
|
Returns <expression> if it is not null, otherwise returns zero. Example:
|
Note: some of these are actually logical operators and appear in black, not blue. For more information, see Operator syntax.
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:
SUM([Profit]) > 0
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.
-
From the Data pane, drag KPI to Color on the Marks card.
You can now see which categories are losing money in each state.
See Also
Visualize Key Progress Indicators
Logic Calculations free video tutorial