---
schema_version: "1"
id: a1e17f4a60a8a5ef6d9a2d1f375f4c74378bb9dc4e8cd2c7e4bf7c009224ee31
title: Logical Functions
collection: tableau
version: current
locale: en-us
portal_url: https://docs.salesforce.com/tableau/current/en-us/public/desktop/functions_functions_logical.md
canonical_url: https://help.tableau.com/current/public/desktop/en-us/functions_functions_logical.htm
source_site: help.tableau.com
content_type: documentation
content_hash: 16441a542c8ba634e60c83fd1ee52a23d41d90094d7de2caa1c9b5173731fc54
description: Reference for Tableau logical functions such as IF, CASE, and AND.
---
[Skip to main content](#main-content)

# 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 categorize values based on certain cutoffs.

A logical calculation might look something like this:

IF \[Profit\] > 0   
THEN 'Profitable'   
ELSEIF \[Profit\] = 0 THEN 'Break even'   
ELSE 'Loss'   
END

## Logical functions and operators available in Tableau

### AND

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code>&lt;expr1&gt; AND &lt;expr2&gt;</code>
                            </td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>Performs a logical conjunction on two expressions. (If both sides are true, the logical test returns true.)</td>
                        </tr>
                        <tr>
                            <td>Output</td>
                            <td>Boolean (true or false)</td>
                        </tr>
                        <tr>
                            <td>Example</td>
                            <td><pre>IF [Season] = "Spring" AND "[Season] = "Fall" <br>THEN "It's the apocalypse and footwear doesn't matter" <br>END</pre>
                                <p><i>"If both (Season = Spring) and (Season = Fall) are true simultaneously, then return It's the apocalypse and footwear doesn't matter".</i>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p>Often used with <a href="#IF" class="xref">IF</a> and <a href="#IIF" class="xref">IIF</a>. See also <a href="#NOT" class="xref">NOT</a> and <a href="#OR" class="xref">OR</a>. </p>
                                <p>If both expressions are <code>TRUE</code> (that is, not <code>FALSE</code> or <code>NULL</code>), then the result is <code>TRUE</code>. If either expression is <code>NULL</code>, then the result is <code>NULL</code>. In all other cases, the result is <code>FALSE</code>.
</p>
                                <p>If you create a calculation in which the result of an <code>AND</code> comparison is displayed on a worksheet, Tableau displays <code>TRUE</code> and <code>FALSE</code>. If you would like to change this, use the Format area in the format dialog.
</p>
                                <p class="note"><b>Note</b>:&nbsp;The <code>AND</code> operator employs <i>short circuit evaluation</i>. This means that if the first expression is evaluated to be <code>FALSE</code>, then the second expression is not evaluated at all. This can be helpful if the second expression results in an error when the first expression is <code>FALSE</code>, because the second expression in this case is never evaluated.</p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### CASE

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code><b>CASE</b> &lt;expression&gt; 
                        <br>WHEN &lt;value1&gt; THEN &lt;then1&gt; 
                        <br>WHEN &lt;value2&gt; THEN &lt;then2&gt; 
                        <br>...
                        <br>[ELSE &lt;default&gt;]<br>END</code>
                            </td>
                        </tr>
                        <tr>
                            <td>Output</td>
                            <td>Depends on data type of the <code>&lt;then&gt;</code>&nbsp;values. </td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>
                                <p>Evaluates the <code>expression</code> and compares it to the specified options (<code>&lt;value1&gt;</code>, <code>&lt;value2&gt;</code>, etc.). When a <code>value</code> that matches expression is encountered, CASE returns the corresponding <code>return</code>. If no match is found, the (optional) default is returned. If there is no default and no values match, then Null is returned.</p>
                            </td>
                        </tr>
                        <tr>
                            <td>Example</td>
                            <td><pre xml:space="preserve"><code><b>CASE</b> [Season] <br>WHEN 'Summer' THEN 'Sandals' <br>WHEN 'Winter' THEN 'Boots' <br>ELSE 'Sneakers' <br>END</code></pre>
                                <p><i>"Look at the Season field. If the value is Summer, then return Sandals. If the value is Winter, then return Boots. If none of the options in the calculation match what is in the Season field, return a Sneakers."</i>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p>See also <a href="#IF" class="xref">IF</a> and <a href="#IIF" class="xref">IIF</a>. </p>
                                <p>Used with <a href="#WHEN" class="xref">WHEN</a>, <a href="#THEN" class="xref">THEN</a>, <a href="#ELSE" class="xref">ELSE</a>, and <a href="#END" class="xref">END</a>. </p>
                                <blockquote><p>[!TIP]</p><p>Many times you can use a group to get the same results as a complicated CASE function, or use CASE to replace native grouping functionality, such as in the previous example. You may want to test which is more performant for your scenario.</p></blockquote>
                            </td>
                        </tr>
                    </tbody>
                </table>

### ELSE

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code>CASE &lt;expression&gt; 
							<br>WHEN &lt;value1&gt; THEN &lt;then1&gt; 
							<br>WHEN &lt;value2&gt; THEN &lt;then2&gt; 
							<br>...
						<br>[<b>ELSE</b> &lt;default&gt;]<br>END</code>
                            </td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>An optional piece of an <code>IF</code>&nbsp;or <code>CASE</code> expression used to specify a default value to return if none of the tested expressions are true. </td>
                        </tr>
                        <tr>
                            <td>Example</td>
                            <td><pre xml:space="preserve">IF [Season] = "Summer"&nbsp;THEN&nbsp;'Sandals' <br>ELSEIF&nbsp;[Season] = "Winter"&nbsp;THEN&nbsp;'Boots' <br><b>ELSE</b>&nbsp;'Sneakers'&nbsp;<br>END</pre><pre>CASE [Season] <br>WHEN 'Summer' THEN 'Sandals' <br>WHEN 'Winter' THEN 'Boots' <br><b>ELSE</b> 'Sneakers' <br>END</pre>
                            </td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p>Used with <a href="#CASE" class="xref">CASE</a>, <a href="#WHEN" class="xref">WHEN</a>, <a href="#IF" class="xref">IF</a>, <a href="#ELSEIF" class="xref">ELSEIF</a>, <a href="#THEN" class="xref">THEN</a>,  and <a href="#END" class="xref">END</a></p>
                                <p><code>ELSE</code> is optional with <code>CASE</code> and <code>IF</code>. In a calculation where <code>ELSE</code> is not specified, if none of the <code>&lt;test&gt;</code>are true, the overall calculation will return null. </p>
                                <p><code>ELSE</code> does not require a condition (such as <code>[Season] = "Winter"</code>) and can be thought of as a form of null handling. </p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### ELSEIF

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code>[<b>ELSEIF</b> &lt;test2&gt; THEN &lt;then2&gt;]</code>
                            </td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>An optional piece of an <code>IF</code>&nbsp;expression used to specify additional conditions beyond the initial IF.</td>
                        </tr>
                        <tr>
                            <td>Example</td>
                            <td><pre xml:space="preserve">IF [Season] = "Summer" THEN 'Sandals' <br><b>ELSEIF</b> [Season] = "Winter" THEN 'Boots' <br><b>ELSEIF</b> [Season] = "Spring" THEN 'Sneakers' <br><b>ELSEIF</b> [Season] = "Autumn" THEN 'Sneakers'<br>ELSE 'Bare feet' <br>END</pre>
                            </td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p>Used with <a href="#IF" class="xref">IF</a>,  <a href="#THEN" class="xref">THEN</a>, <a href="#ELSE" class="xref">ELSE</a>, and <a href="#END" class="xref">END</a></p>
                                <p><code>ELSEIF</code> can be thought of as additional <code>IF</code> clauses. <code>ELSEIF</code> is optional and can be repeated multiple times. </p>
                                <p>Unlike <code>ELSE</code>, <code>ELSEIF</code> requires a condition (such as <code>[Season] = "Winter"</code>).</p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### END

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Definition</td>
                            <td>Used to close an <code>IF</code>&nbsp;or <code>CASE</code> expression. </td>
                        </tr>
                        <tr>
                            <td>Example</td>
                            <td><pre xml:space="preserve">IF [Season] = "Summer"&nbsp;THEN&nbsp;'Sandals' <br>ELSEIF&nbsp;[Season] = "Winter"&nbsp;THEN&nbsp;'Boots' <br>ELSE&nbsp;'Sneakers'&nbsp;<br><b>END</b></pre>
                                <p><i>"If Season = Summer, then return Sandals. If not, look at the next expression. If Season = Winter, then return Boots. If neither of the expressions are true, return Sneakers."</i>
                                </p><pre>CASE [Season] <br>WHEN 'Summer' THEN 'Sandals' <br>WHEN 'Winter' THEN 'Boots' <br>ELSE 'Sneakers' <br><b>END</b></pre>
                                <p><i><i>"Look at the Season field. If the value is Summer, then return Sandals. If the value is Winter, then return Boots. If none of the options in the calculation match what is in the Season field, return a Sneakers."</i></i>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p>Used with <a href="#CASE" class="xref">CASE</a>, <a href="#WHEN" class="xref">WHEN</a>, <a href="#IF" class="xref">IF</a>, <a href="#ELSEIF" class="xref">ELSEIF</a>, <a href="#THEN" class="xref">THEN</a>, and <a href="#ELSE" class="xref">ELSE</a>.</p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### IF

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code><b>IF</b> &lt;test1&gt; THEN &lt;then1&gt; <br>[ELSEIF &lt;test2&gt; THEN &lt;then2&gt;...]<br>[ELSE &lt;default&gt;] <br>END</code>
                            </td>
                        </tr>
                        <tr>
                            <td>Output</td>
                            <td>Depends on data type of the <code>&lt;then&gt;</code>&nbsp;values. </td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>
                                <p>Tests a series of expressions  and returns the <code>&lt;then&gt;</code> value for the first true <code>&lt;test&gt;</code>.</p>
                            </td>
                        </tr>
                        <tr>
                            <td>Example</td>
                            <td><pre xml:space="preserve">IF [Season] = "Summer"&nbsp;THEN&nbsp;'Sandals' <br>ELSEIF&nbsp;[Season] = "Winter"&nbsp;THEN&nbsp;'Boots' <br>ELSE&nbsp;'Sneakers'&nbsp;<br><b>END</b></pre>
                                <p><i>"If Season = Summer, then return Sandals. If not, look at the next expression. If Season = Winter, then return Boots. If neither of the expressions are true, return Sneakers."</i>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p>See also <a href="#IF" class="xref">IF</a> and <a href="#IIF" class="xref">IIF</a>. </p>
                                <p>Used with <a href="#ELSEIF" class="xref">ELSEIF</a>, <a href="#THEN" class="xref">THEN</a>, <a href="#ELSE" class="xref">ELSE</a>, and <a href="#END" class="xref">END</a></p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### IFNULL

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code>IFNULL(expr1, expr2)</code>
                            </td>
                        </tr>
                        <tr>
                            <td>Output</td>
                            <td>Depends on the data type of the <code>&lt;expr&gt;</code> values. </td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>
                                <p>Returns <code>&lt;expr1&gt;</code> if it's non-null, otherwise returns <code>&lt;expr2&gt;</code>.</p>
                            </td>
                        </tr>
                        <tr>
                            <td>Example</td>
                            <td><pre xml:space="preserve">IFNULL([Assigned Room], "TBD")</pre>
                                <p><i>"If the Assigned Room field isn't null, return its value. If the Assigned room field is null, return TBD instead."</i>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p>Compare with <a href="#ISNULL" class="xref">ISNULL</a>. <code>IFNULL</code> always returns a value. <code>ISNULL</code>&nbsp;returns a boolean (true or false). </p>
                                <p>See also <a href="#ZN" class="xref">ZN</a>. </p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### IIF

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code><b>IIF</b>(&lt;test&gt;, &lt;then&gt;, &lt;else&gt;, [&lt;unknown&gt;])</code>
                            </td>
                        </tr>
                        <tr>
                            <td>Output</td>
                            <td>Depends on the data type of the values in the expression. </td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>Checks whether a condition is met (<code>&lt;test&gt;</code>), and returns <code>&lt;then&gt;</code>if the test is true, <code>&lt;else&gt;</code>&nbsp;if the test is false, and an optional  value for <code>&lt;unknown&gt;</code> if the test is null. If the optional unknown isn't specified, <code>IIF</code> returns null. </td>
                        </tr>
                        <tr>
                            <td>Example</td>
                            <td><pre xml:space="preserve">IIF([Season] = 'Summer', 'Sandals', 'Other footwear')</pre>
                                <p><i>"If Season = Summer, then return Sandals. If not, return Other footwear"</i>
                                </p><pre xml:space="preserve">IIF([Season] = 'Summer', 'Sandals', <br>   IIF('Season' = 'Winter', 'Boots',  'Other footwear')<br>)</pre>
                                <p><i>"If Season = Summer, then return Sandals. If not, look at the next expression. If Season = Winter, then return Boots. If neither are true, return Sneakers."</i>
                                </p><pre xml:space="preserve">IIF('Season' = 'Summer', 'Sandals', <br>   IIF('Season' = 'Winter', 'Boots',  <br>      IIF('Season' = 'Spring', 'Sneakers', 'Other footwear')<br>   )<br>)</pre>
                                <p><i>"If Season = Summer, then return Sandals. If not, look at the next expression. If Season = Winter, then return Boots. If none of the expressions are true, return Sneakers."</i>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p>See also <a href="#IF" class="xref">IF</a> and<a href="#CASE" class="xref">CASE</a>. </p>
                                <p><code>IIF</code> doesn't have an equivalent to <code>ELSEIF</code> (like <code>IF</code>)&nbsp;or repeated <code>WHEN</code> clauses (like <code>CASE</code>). Instead, multiple tests can be evaluated sequentially by nesting <code>IIF</code> statements as the <code>&lt;unknown&gt;</code>&nbsp;element. The first (outermost) true  is returned. </p>
                                <p>That is to say, in the calculation below,  the result will be Red, not Orange, because the expression stops being evaluated as soon as A=A is evaluated as true: </p>
                                <p> <code>IIF('A' = 'A', 'Red', IIF('B' = 'B', 'Orange',  IIF('C' = 'D', 'Yellow', 'Green')))</code></p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### IN

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code>&lt;expr1&gt; IN &lt;expr2&gt;</code>
                            </td>
                        </tr>
                        <tr>
                            <td>Output</td>
                            <td>Boolean (true or false)</td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>	
Returns <code>TRUE</code> if any value in <code>&lt;expr1&gt;</code> matches any value in <code>&lt;expr2&gt;</code>.</td>
                        </tr>
                        <tr>
                            <td>Example</td>
                            <td><pre xml:space="preserve">SUM([Cost]) IN&nbsp;(1000, 15, 200)</pre>
                                <p><i>"Is the value of the Cost field 1000, 15, or 200?"</i>
                                </p><pre xml:space="preserve">[Field] IN&nbsp;[Set]</pre>
                                <p><i>"Is the value of the field present in the set?"</i>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p>The values in <code>&lt;expr2&gt;</code> can be a set, list of literal values, or combined field.</p>
                                <p>See also  <a href="#WHEN" class="xref">WHEN</a>. </p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### ISDATE

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code>ISDATE(string)</code>
                            </td>
                        </tr>
                        <tr>
                            <td>Output</td>
                            <td>Boolean (true or false)</td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>Returns true if a <code>&lt;string&gt;</code> is a valid date. The input expression must be a string (text)&nbsp;field. </td>
                        </tr>
                        <tr>
                            <td>Example</td>
                            <td><pre xml:space="preserve">ISDATE("2018-09-22")</pre>
                                <p><i>"Is the string 2018-09-22 a properly formatted date?"</i>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p>What is considered a valid date depends on the <a href="https://help.tableau.com/current/server/en-us/lang_intro.htm" target="_blank">locale</a> of the system evaluating the calculation. For example:</p>
                                <p>In the USA: </p>
                                <ul>
                                    <li><code>ISDATE("2018-09-22") = TRUE</code>
                                    </li>
                                    <li><code>ISDATE("2018-22-09") = FALSE</code>
                                    </li>
                                </ul>
                                <p>In the UK: </p><ul><li><code>ISDATE("2018-09-22") = FALSE</code> </li><li><code>ISDATE("2018-22-09") = TRUE</code></li></ul><p></p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### ISNULL

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code>ISNULL(expression)</code>
                            </td>
                        </tr>
                        <tr>
                            <td>Output</td>
                            <td>Boolean (true or false)</td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>
                                <p>Returns true if the <code>&lt;expression&gt;</code> is NULL (does not contain valid data).</p>
                            </td>
                        </tr>
                        <tr>
                            <td>Example</td>
                            <td><pre xml:space="preserve">ISNULL([Assigned Room])</pre>
                                <p><i>"Is the Assigned Room field null?"</i>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p>Compare with <a href="#IFNULL" class="xref">IFNULL</a>. <code>IFNULL</code> always returns a value. <code>ISNULL</code> returns a boolean.</p>
                                <p>See also <a href="#ZN" class="xref">ZN</a>. </p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### MAX

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code>MAX(expression)</code> or <code>MAX(expr1, expr2)</code></td>
                        </tr>
                        <tr>
                            <td>Output</td>
                            <td>Same data type as the argument, or <code>NULL&nbsp;</code>if any part of the argument is null. </td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>
                                <p>Returns the maximum of the two arguments, which must be of the same data type. </p>
                                <p><code>MAX</code> can also be applied to a single field as an aggregation.</p>
                            </td>
                        </tr>
                        <tr>
                            <td>Example</td>
                            <td><pre xml:space="preserve">MAX(4,7) = 7<br>MAX(#3/25/1986#, #2/20/2021#) = #2/20/2021# <br>MAX([Name]) = "Zander"</pre>
                            </td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p><b>For strings</b>
                                </p>
                                <p><code>MAX</code> is usually the value that comes last in alphabetical order. </p>
                                <p class="note">For database data sources, the <code>MAX</code> string value is highest in the sort sequence defined by the database for that column.</p>
                                <p><b>For dates</b>
                                </p>
                                <p>For dates, the <code>MAX</code> is the most recent date. If <code>MAX</code> is an aggregation, the result will not have a date hierarchy. If <code>MAX</code> is a comparison, the result will retain the date hierarchy. </p>
                                <p><b>As an aggregation</b>
                                </p>
                                <p><code>MAX(expression)</code> is an aggregate function and returns a single aggregated result. This displays as <code>AGG(expression)</code> in the viz. </p>
                                <p><b>As a comparison</b>
                                </p>
                                <p><code>MAX(expr1, expr2)</code> compares the two values and returns a row-level value.</p>
                                <p>See also <code><a href="#MIN" class="xref">MIN</a></code>.</p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### MIN

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code>MIN(expression)</code> or <code>MIN(expr1, expr2)</code></td>
                        </tr>
                        <tr>
                            <td>Output</td>
                            <td>Same data type as the argument, or <code>NULL&nbsp;</code>if any part of the argument is null. </td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>
                                <p>Returns the minimum of the two arguments, which must be of the same data type. </p>
                                <p><code>MIN </code>can also be applied to a single field as an aggregation.</p>
                            </td>
                        </tr>
                        <tr>
                            <td>Example</td>
                            <td><pre xml:space="preserve">MIN(4,7) = 4<br>MIN(#3/25/1986#, #2/20/2021#) = #3/25/1986#<br>MIN([Name]) = "Abebi"</pre>
                            </td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p><b>For strings</b>
                                </p>
                                <p><code>MIN</code> is usually the value that comes first in alphabetical order. </p>
                                <p class="note">For database data sources, the <code>MIN</code> string value is lowest in the sort sequence defined by the database for that column.</p>
                                <p><b>For dates</b>
                                </p>
                                <p>For dates, the <code>MIN</code> is the earliest date. If <code>MIN</code> is an aggregation, the result will not have a date hierarchy. If <code>MIN</code> is a comparison, the result will retain the date hierarchy. </p>
                                <p><b>As an aggregation</b>
                                </p>
                                <p><code>MIN(expression)</code> is an aggregate function and returns a single aggregated result. This displays as <code>AGG(expression)</code> in the viz. </p>
                                <p><b>As a comparison</b>
                                </p>
                                <p><code>MIN(expr1, expr2)</code> compares the two values and returns a row-level value.</p>
                                <p>See also <code><a href="#MAX" class="xref">MAX</a></code>.</p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### NOT

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code>NOT&nbsp;&lt;expression&gt;</code>
                            </td>
                        </tr>
                        <tr>
                            <td>Output</td>
                            <td>Boolean (true or false)</td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>Performs logical negation on an expression.</td>
                        </tr>
                        <tr>
                            <td>Example</td>
                            <td><pre xml:space="preserve">IF <b>NOT</b> [Season] = "Summer" <br>THEN 'Don't wear sandals' <br>ELSE&nbsp;'Wear sandals'&nbsp;<br><b>END</b></pre>
                                <p><i>"If Season doesn't equal Summer, then return Don't wear sandals. If not,  return Wear sandals."</i>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p>Often used with <a href="#IF" class="xref">IF</a> and <a href="#IIF" class="xref">IIF</a>. See also <a href="#top" class="xref">AND</a> and <a href="#OR" class="xref">OR</a>.</p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### OR

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code>&lt;expr1&gt; OR &lt;expr2&gt;</code>
                            </td>
                        </tr>
                        <tr>
                            <td>Output</td>
                            <td>Boolean (true or false)</td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>Performs a logical disjunction on two expressions.</td>
                        </tr>
                        <tr>
                            <td>Example</td>
                            <td><pre xml:space="preserve">IF [Season] = "Spring"&nbsp;<b>OR</b>&nbsp;[Season] = "Fall" <br>THEN&nbsp;"Sneakers" <br>END</pre>
                                <p><i>"If either (Season = Spring)&nbsp;or (Season = Fall)&nbsp;is true, then return Sneakers."</i>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p>Often used with <a href="#IF" class="xref">IF</a> and <a href="#IIF" class="xref">IIF</a>. See also <a href="#top" class="xref">AND</a> and <a href="#NOT" class="xref">NOT</a>.</p>
                                <p>If either expression
							is <code>TRUE</code>, then the result is <code>TRUE</code>.
							If both expressions are <code>FALSE</code>, then the result
							is <code>FALSE</code>. If both expressions are <code>NULL</code>,
						then the result is <code>NULL</code>. </p>
                                <p>If you create
							a calculation which displays the result of an <code>OR</code> comparison
							on a worksheet, Tableau displays TRUE and FALSE. If
							you would like to change this, use the Format area in the format
						dialog. </p>
                                <p class="note"><b>Note</b>:&nbsp;The <code>OR</code> operator employs <em>short circuit
							evaluation.</em> This means that if the first expression is evaluated
							to be <code>TRUE</code>, then the second expression is not evaluated
							at all. This can be helpful if the second expression results in
							an error when the first expression is <code>TRUE</code>, because
					the second expression in this case is never evaluated.</p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### THEN

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code>IF &lt;test1&gt; <b>THEN</b> &lt;then1&gt; 
							<br>[ELSEIF &lt;test2&gt; <b>THEN</b> &lt;then2&gt;...]
							<br>[ELSE &lt;default&gt;] 
						<br>END</code>
                            </td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>A required part of an <code>IF</code>, <code>ELSEIF</code>, or <code>CASE</code> expression, used to define what result to return if a specific value or test is true. </td>
                        </tr>
                        <tr>
                            <td>Example</td>
                            <td><pre xml:space="preserve">IF [Season] = "Summer"&nbsp;<b>THEN</b>&nbsp;'Sandals' <br>ELSEIF&nbsp;[Season] = "Winter"&nbsp;<b>THEN</b>&nbsp;'Boots' <br>ELSE&nbsp;'Sneakers'&nbsp;<br>END</pre>
                                <p><i>"If Season = Summer, then return Sandals. If not, look at the next expression. If Season = Winter, then return Boots. If neither of the expressions are true, return Sneakers."</i>
                                </p><pre>CASE [Season] <br>WHEN 'Summer' <b>THEN</b> 'Sandals' <br>WHEN 'Winter' <b>THEN</b> 'Boots' <br>ELSE 'Sneakers' <br>END</pre>
                                <p><i><i>"Look at the Season field. If the value is Summer, then return Sandals. If the value is Winter, then return Boots. If none of the options in the calculation match what is in the Season field, return a Sneakers."</i></i>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p>Used with <a href="#CASE" class="xref">CASE</a>, <a href="#WHEN" class="xref">WHEN</a>, <a href="#IF" class="xref">IF</a>, <a href="#ELSEIF" class="xref">ELSEIF</a>, <a href="#THEN" class="xref">THEN</a>, <a href="#ELSE" class="xref">ELSE</a>, and <a href="#END" class="xref">END</a></p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### WHEN

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code>CASE &lt;expression&gt; 
							<br><b>WHEN</b> &lt;value1&gt; THEN &lt;then1&gt; 
							<br><b>WHEN</b> &lt;value2&gt; THEN &lt;then2&gt; 
							<br>...
						<br>[ELSE &lt;default&gt;]<br>END</code>
                            </td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>A required part of a <code>CASE</code> expression. Finds the first <code>&lt;value&gt;</code> that matches <code>&lt;expression&gt;</code> and returns the corresponding <code>&lt;then&gt;</code>.</td>
                        </tr>
                        <tr>
                            <td>Example</td>
                            <td><pre>CASE [Season] <br><b>WHEN</b> 'Summer' THEN 'Sandals' <br><b>WHEN</b> 'Winter' THEN 'Boots' <br>ELSE 'Sneakers' <br>END</pre>
                                <p><i>"Look at the Season field. If the value is Summer, then return Sandals. If the value is Winter, then return Boots. If none of the options in the calculation match what is in the Season field, return a Sneakers."</i>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p>Used with <a href="#CASE" class="xref">CASE</a>, <a href="#THEN" class="xref">THEN</a>, <a href="#ELSE" class="xref">ELSE</a>, and <a href="#END" class="xref">END</a>. </p>
                                <p><code>CASE</code> also supports <code>WHEN IN</code> construction, such as:</p><pre xml:space="preserve">CASE &lt;expression&gt; <br><b>WHEN IN</b> &lt;set1&gt; THEN &lt;then1&gt; <br><b>WHEN IN</b> &lt;combinedfield&gt; THEN &lt;then2&gt; <br>... <br>ELSE &lt;default&gt; <br>END</pre>
                                <p>The values that <code>WHEN IN</code> compare to must be a set, list of literal values, or combined field. See also <a href="#IN" class="xref">IN</a>.</p>
                            </td>
                        </tr>
                    </tbody>
                </table>

### ZN

<table class="table--plain" style="width: 100%;">
                    <colgroup><col style="width: 20%;">
                    <col>
                    </colgroup><tbody>
                        <tr>
                            <td>Syntax</td>
                            <td><code>ZN(expression)</code>
                            </td>
                        </tr>
                        <tr>
                            <td>Output</td>
                            <td>Depends on the data type of the <code>&lt;expression&gt;</code>, or 0.</td>
                        </tr>
                        <tr>
                            <td>Definition</td>
                            <td>Returns &lt;expression&gt; if it isn't null, otherwise returns zero.</td>
                        </tr>
                        <tr>
                            <td>Example</td>
                            <td><pre xml:space="preserve">ZN([Test Grade])</pre>
                                <p><i>"If the test grade isn't null, return its value. If the test grade is null, return 0."</i>
                                </p>
                            </td>
                        </tr>
                        <tr>
                            <td>Notes</td>
                            <td>
                                <p><code>ZN</code> is a specialized case of <a href="#IFNULL" class="xref">IFNULL</a> where alternative if the expression is null is always 0 rather than being specified in the calculation. </p>
                                <p><code>ZN</code> is especially useful when performing additional calculations and a null would render the entire calculation null. However, use caution interpreting these results as null is not always synonymous with 0 and could represent missing data. </p>
                                <p>See also <a href="#ISNULL" class="xref">ISNULL</a>. </p>
                            </td>
                        </tr>
                    </tbody>
                </table>  

**Note**: some of these are actually logical operators and appear in black, not blue. For more information, see [Operator syntax](https://help.tableau.com/current/public/desktop/en-us/functions_operators.htm#Operators).

Note on CASE, IF, and IIF

CASE is often easier to use than IF or IIF. In many instances, IF, IIF, and CASE can be used interchangeably. A CASE statement can always be rewritten as an IF statement, although the CASE function will generally be more concise and may be easier to understand. However, not all IF statements can be written as CASE statements, because each ELSEIF could refer to a different field.

**Tip**: Let's compare an example using the same logic across these three functions:  

<table style="width: 100%;" class="table--plain"><colgroup><col style="width: 33%;"><col style="width: 33%;"><col></colgroup><tbody><tr><td>CASE</td><td>IF</td><td>IIF</td></tr><tr><td><pre>CASE [Region]<br>WHEN 'West' THEN 1<br>WHEN 'East' THEN 2<br>WHEN 'South' THEN 3<br>WHEN 'North' Then 4<br>ELSE 5<br>END</pre></td><td><pre>IF [Region] = 'West' THEN 1<br>ELSEIF [Region] = 'East' THEN 2<br>ELSEIF [Region] = 'South' THEN 3<br>ELSEIF [Region] = 'North' THEN 4<br>ELSE 5<br>END</pre></td><td><pre>IIF([Region] = 'West', 1,<br> IIF([Region] = 'East', 2,<br>  IIF([Region] = 'South', 3,<br>   IIF([Region] = 'North', 4,<br>   5, 0)<br>  )<br> )<br>)</pre></td></tr><tr><td><p>The CASE structure is very simple and easy to write and understand. However, the expression (here, [Region]) cannot be a logical operation, unlike with IF or IIF. </p><p>Nulls are handled the same for CASE as they are for IF. </p></td><td><p>The IF&nbsp;THEN&nbsp;structure permits multiple ELSEIF clauses, which makes multiple evaluations much easier than with IIF. </p><p><b>Null handling</b>:&nbsp;any unknown (null)&nbsp;results are handled by the same ELSE clause as false results. Here, any region other than the four specified (including null)&nbsp;will be assigned a value of 5. </p></td><td><p>The IIF structure handles unknown results differently than false results and has a different syntax than IF. The tradeoff for specific null handling is nesting, as above, which can be clumsy to write and understand. </p><p><b>Null handling</b>:&nbsp;any non-null region other than the four specified will be assigned a value of 5, but a null value will be assigned a 0. </p></td></tr></tbody></table>

**Note**: Many times you can use a group to get the same results as a complicated case function. Test to see if one option is more performant than the other.

### Examples

CASE functions can be useful for scenarios such as realiasing: 

CASE LEFT(DATENAME('weekday',\[Order Date\]),3)  
WHEN 'Sun' THEN 0  
WHEN 'Mon' THEN 1  
WHEN 'Tue' THEN 2  
WHEN 'Wed' THEN 3  
WHEN 'Thu' THEN 4   
WHEN 'Fri' THEN 5   
WHEN 'Sat' THEN 6  
END

or giving the end user the ability to select which measure to view in a chart when used with a [parameter](https://www.tableau.com/about/blog/2015/11/using-parameter-change-fields-45866):

CASE \[Choose a Measure\]  
WHEN "Sales" THEN SUM(\[Sales\])  
WHEN "Profit" THEN AVG(\[Profit\])  
WHEN "Quantity" THEN COUNT(\[Quantity\])  
WHEN "Shipping Cost" THEN MEDIAN(\[Shipping Cost\])  
END

## Create a logical calculation

Follow along with the steps below to learn how to create a logical calculation.

1. In Tableau Desktop, connect to the **Sample - Superstore** saved data source, which comes with Tableau.
2. Navigate to a worksheet.
3. From the **Data** pane, drag State to the Rows shelf.
4. From the **Data** pane, drag Category to the Rows shelf and place it to the right of State.
5. From the **Data** pane, drag Sales to the Columns shelf.
6. Select Analysis \> Create Calculated Field.
7. 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.

9. From the **Data** pane, drag **KPI** to **Color** on the Marks card.
    
    You can now see which categories are losing money in each state.
    
    ![Logical function applied to a bar chart to highlight the product categories that are losing money in each state.](/tableau/current/en-us/public/desktop/en-us/Img/logicalfunctions1.png)

## See Also

[Visualize Key Progress Indicators](https://help.tableau.com/current/public/desktop/en-us/kpi.htm)

[Functions in Tableau](https://help.tableau.com/current/public/desktop/en-us/functions.htm)

[Tableau Functions (by Category)](https://help.tableau.com/current/public/desktop/en-us/functions_all_categories.htm)

[Tableau Functions (Alphabetical)](https://help.tableau.com/current/public/desktop/en-us/functions_all_alphabetical.htm)