How to Use and Write IF Statements in Tableau

IF Statements are a fundamental building block of building dashboards, reports, and performing data analysis in Tableau. We’ll explain how to create formulas, and several different methods of creating IF statements ranging from a basic IF statement to ones that can be used to evaluate multiple conditions at one time.

Tableau supports multiple IF statement syntax, including IF, IIF, ELSEIF and all of the most widely used operators when creating formulas for Calculated Fields.

Tableau refers to formulas as Calculated Fields because data is being used to create new sets of data. When you want to create new data conditionally, it requires the use of an IF Statement.

Tableau supports several methods of deploying IF Statements, ranging from a standard one, to nested IF statements, ELSEIF statements and conditional operators. We’ll explain them all.

Let’s roll!

Creating Calculated Fields in Tableau

To create a new calculated field in Tableau, you can either right click on a measure name in the data panel of a sheet, and select, Create, Calculated Field or you can right click on a field from the Data Source view and select Create Calculated Field.

Both methods launch the Tableau Formula Editor. Our preferred method is using the Data Source view because it lets us easily preview the data that’s being generated to make sure that it is acting as expected.

Screenshot of creating a new calculated fields in Tableau Desktop

Once you select Create Calculated Field a text box will appear that you can type formulas into. Here are a few things to be aware of when getting start writing formulas in Tableau that will help you get started and write formulas faster.

  • Rename the field using the box at the top left of the formual window.
  • Tableau will suggest ways to auto complete formulas.
  • Press TAB to accept auto suggested terms, and the keyboard to move up down to select fields.
  • You can expand the formula box by pressing the arrow at the left to see formula explanations.

The screenshot below shows the expanded formula view that explains formula syntax along with Tableau automatically suggesting the next part of a formula.

Example of the expanded formula box for creating new calculated fields showing auto complete and formula suggestions and explanations.

There is also a handy search feature that lets you search for a formula if you don’t know the exact name of it.

Formulas are typed into the box on the left side of the Tableau Formuala editor.

Let’s look at how to create IF Statements!

Understanding the Basics of IF Statements

IF statements in Tableau evaluates a condition and returns one of two values based on whether the condition is true or false. This conditional logic is super helpful when creating custom calculations, or assigning values to specific categories that can be used to group or filter on in other parts of your Tableau dashboard.

Here are a few ways to write IF statements in Tableau.

Basic Tableau IF Statements

The most straightforward way to use an IF statement is as follows:

IF [Condition] THEN [True] ELSE [False] END

Replace [Condition] with the logical expression you want to evaluate. If the condition is true, Tableau will return [True], otherwise, it will return [False].

The values being returned can either be values in other columns, or text. Columns are referenced with square brackets around them while text is referenced with quotes around it. There is no need to add quotes around numbers.

Here’s an example:

IF [Profit] > 0 THEN "Income" ELSE "Loss" END

Tableau will evaluate every row of the Profit column and return the words “Income” or “Loss” based on whether the condition is met.

The syntax of IF THEN ELSE END is very common within programming languages, but if you are coming from the Excel world it may be a bit difficult to remember. Luckily, Tableau provides an alternative syntax to help you write IF statements faster.

Basic Tableau IIF Statements

The IIF Statement in Tableau acts the same as a regular IF statement, but is faster to write. The syntax for it is similar to Excel and is as follows:

IIF ([condition],[True],[False])

Using the same example as our first example, the IIF formula would look as follows:

IIF ([Profit] > 0,"Income","Loss")

The results are the same as a normal IF statement. IIF is easier and faster to type out. One thing we would caution you on is that using regular IF statements is probably more common and provides you some additional functionality when combined with ELSEIF to evaluate multiple criteria.

Nested IF Statements

Tableau allows you to combine multiple IF statements together into a single formula. If you decide to nest IF statements together, we recommend taking advantage of writing formulas on multiple lines to keep them organized. Try pressing Enter to move to the next line. There is no requirement to write everything on a single line, and it makes formulas easier to read.

Here’s an example of a Nested IF Statement

IF [Sales] > 1000 THEN
    IF [Profit] > 0 THEN "High Sales and Profit"
    ELSE "High Sales but Loss"
ELSE
    "Low Sales"
END

In this example, we first check if sales are high, and then we further categorize based on profit.

Tableau ELSEIF Statements

Rather than having to write multiple IF statements for various criteria or create complex nested IF statements Tableau supports the ELSEIF function. Using an ELSEIF allows you to include as many different criteria and potential results as you want into a single formula while keeping everything organized.

The following example of an ELSEIF categorizes different possible States into Regions based on their location.

IF [State] = "California" THEN "West Region"
ELSEIF [State] = "Maine" THEN "East Region"
ELSE "Other Region"
END

When we write ELSEIF statements, we recommend placing each ELSEIF criteria on its own row in the formula box to make it easier to understand, though its not required.

Using Logical Operators with Tableau IF Statements

In Tableau, operators are used within calculations to perform various calculations. Logical operators such as, AND, OR, NOT allow you to combine multiple criteria into a single evaluation. They’re extremely useful in creating calculated fields.

Here’s an example that checks both the category and sales condition before categorizing.

IF [Category] = "Electronics" AND [Sales] > 500 THEN "High Sales in Electronics"
ELSE "Other Categories"
END

Note: When working with Logical Operators in Tableau, you have to define each condition using the column name. If you try to write an IF statement that evaluations whether a column meets multiple criteria you will get a notification like the one below that the calculation contains errors.

Example of a formula error in Tableau due to a bad IF statement syntax using multiple criteria.

An alternative for saving time when evaluating if a column includes multiple values is to use an IN statement to list the values in comma separated quotes within a parenthesis.

Best Practices for Writing IF Statements

To avoid errors and to make sure that others, including yourself can manage the report at a later date here are some best practices to make things easier.

  • Write for the Future – Even if a formula makes sense today, it may not if you have to update it in a year.
  • Use Descriptive Field Names – Give your formulas easy to remember and recognize names to identify them.
  • Formatting – Use indentation, spaces, extra rows when possible to spread out formulas making them easier to read.
  • Documentation – Type // into a row of a Tableau formula box to escape the formula and write comments.
  • Break Formulas Apart – When writing complex formulas it can be easier to create multiple calculated fields that break a formula down into pieces instead of trying to write one large complex formula.

By using these methods you’ll make reports easier to manage when business needs change and they need to be updated. It can also make the lives of others easier if you move on to a different position or organization.

Conclusion

Mastering IF statements in Tableau is a key skill for data analysts and business intelligence professionals. By understanding the various methods and syntax options, you can create calculated fields quickly and easily. Remember to follow best practices to maintain well-organized and reliable calculations in your Tableau projects to make your life and the life of others easier when a formula has to be updated.

Scroll to Top