Use IF Statement in Tableau

When I was working on a Tableau report, I needed to highlight customers who spent more than $10,000 in purchases. I tried using filters, but it was not possible to create custom categories and categorize the customers in specific groups using filters.

As a solution for this, I used the IF statement in Tableau. With this, I was able to create a new field that marked such customers as “Premium” and others as “Regular.” This made it easier to use the calculated field in charts, filters, and dashboards.

In this tutorial, I’ll explain what the IF statement is in Tableau and how we can use it with the help of examples.

IF Statement in Tableau

In Tableau, an IF statement is a logical function that creates a calculated field to perform conditional logic on the data, returning one value if a condition is TRUE and another if it’s FALSE.

The syntax of using the IF statement in the Tableau formula is as follows.

IF [condition] THEN [value if true] ELSE [value if false] END

Using this, if-then logic, the formula will return one value if the condition is True, else it will return another value.

Use an IF Statement in Tableau

In the examples below, I will show how we can create a formula using the IF statement for simple logic and by using the IF statement with multiple conditionals for complex logic.

  • Example-1: Classify sales as high or low using an if statement
  • Example-2: Use multiple If conditions with Elseif
  • Example-3: Use Tableau aggregation functions with IF
  • Example-4: Nested IF statements in Tableau
  • Example-5: Use conditional statements with IIF statements

Example-1: Classify Sales as High or Low Using an IF Statement

In this example, we will divide sales into two groups: High Sales above 5000 and Low Sales below or equal to 5000.

  1. To create a calculated field, click on the Analysis tab and select Create Calculated Field.
IF function in Tableau
  1. Enter the formula below in the calculated field to divide the sales into two categories, “High Sales” and “Low Sales“.
IF [Sales] > 2000 THEN "High Sales"
ELSE "Low Sales"
END

In this example, I have labelled the calculated field as Sales Group.

  1. Now, add Sales to Rows and Customer Name to Columns. Here, you can also filter out the customer records.
IF Statement in Tableau

With this, we can see the sales for the selected customers.

  1. Now, using the calculated field, we will divide the customer sales into two categories: high sales and low sales. For this, drag the calculated field to the color card.
Create a calculated field using IF function in Tableau

Now, the customers’ sales bars will be highlighted in categories “High Sales” and “Low Sales” with two different colors.

This way, we can use the IF statements in Tableau formula fields to define the logic, such as classifying a dimension in different categories.

Example-2: Use Multiple If Conditions With Elseif

In Tableau, we can use multiple IF conditions with ELSEIF when we need to define multiple conditions in the calculated field.

In this example, we will create a calculated field that will categorize the Regions into two categories: Area1 and Area2. The East and West region will be in Area 1, and then the Central and South region will be in Area 2.

Now, create a new worksheet and follow the steps below to use multiple If conditions with Elseif in Tableau.

  1. To create a calculated field, click the Analysis tab -> Create Calculated Field.
  1. Enter the formula below in the calculated field that will categorize regions into groups “Area 1” and ” Area 2″.
IF [Region] = "Central" THEN "Area 1"
ELSEIF [Region] = "West" THEN "Area 1"
ELSEIF [Region] = "East" THEN "Area 2"
ELSEIF [Region] = "South" THEN "Area 2"
END
  1. To visualize the data in Tableau, drag the State, Region, and calculated field “Area” to the columns.
  1. Now add the Measures, such as Sales and Profit, to the Columns.

With this, we can see the Sales and Profit for the States/Regions grouped in Area 1 and Area 2.

Multiple If Conditions in Tableau

This way, we can use multiple If conditions in Tableau with Elseif by following the above steps.

Example-3: Use Tableau Aggregation Functions With IF

In Tableau, we also use IF statements along with aggregation functions like SUM, AVG, or COUNT to create calculations. This helps when we need to evaluate totals rather than individual row values.

For example, I created a field to check if the total sum of sales for a region is above 5,10,000. If true, it marks the region as a High Performer; otherwise, it marks the region as a Low Performer.

Now, follow the steps below to create a calculated field by using Tableau Aggregation functions with IF.

  1. In the Tableau navigation bar, click on the Analysis tab and select Create Tableau Calculation.
  1. Enter the formula below in the ‘Region Performance‘ calculated field and click OK.
IF SUM([Sales]) > 100000 THEN "High Performer"
ELSE "Low Performer"
END
  1. To create the bar chart, add the Region to Rows and SUM(Sales) to Columns.
  1. To visualize the data using the calculated field, drag the field Region Performance to the Color card to highlight the results.
Tableau If Else Conditionals

Now we can see bars in two colors, the High performer regions in Blue and the Low performer regions in Orange.

  1. If you want to show the group on the chart, like a region of both categories in separate blocks, then add a calculated field to the Columns also.
Categorize dimensions using conditionals in Tableau

This way, we can use IF statements along with aggregation functions in Tableau.

Example-4: Nested IF Statements in Tableau

In Tableau, we can also use nested IF statements when we need to check one condition inside another. This is helpful when logic depends on multiple nested conditions.

For example, I wanted to group customers based on both their Region and Sales amount. For this, I need to define a formula with the conditions below.

  • If the region is East and the sales are above 8,000, then mark it as “Top East Customer.”
  • If the region is East but sales are less than or equal to 8,000, then mark as “Regular East Customer.
  • Otherwise, mark them as “Other Region Customer.
  1. To create the calculated field, click on the Analysis tab -> Create Calculated Field.
  1. Enter this formula for the calculated field and name it Customer Group.
IF [Region] = "East" THEN
    IF [Sales] > 4000 THEN "Top East Customer"
    ELSE "Regular East Customer"
    END
ELSE "Other Region Customer"
END
  1. Now, add the Customer Name to the Columns and Sales to the Rows. After this, drag the calculated field Customer group to the color card.
Use nested If statements in tableau

In the bar chart, we can figure out the customers in the East region with sales higher than 4000.

This way, we can create multi-level logic in Tableau calculated fields using nested IF statements.

Example-5: Use Conditional Statements With the IIF Function

In Tableau, we also use the IIF function for simple conditional logic. It works like IF, but in a single line, and we can also add a default value.

For example, I created a field to label orders as Profitable if profit is greater than 0, otherwise Not Profitable.

Now, follow the steps to see how we can use the IFF function in Tableau calculated fields.

  1. In the Tableau navigation bar, select Analysis -> Create Table calculation.
  1. Enter the formula below for the calculated field and name it Profit Status.
IIF([Profit] > 0, "Profitable", "Not Profitable")
  1. Now, drag Order ID to Rows and Profit to Columns.
IIF Function conditional in Tableau
  1. Now, to show the profit status, add Profit Status to the Color card in the Marks section.
Use IFF function in Tableau

Now, we can see the Profit less than 0 marked in red and above 0 in green color.

This way, we can use the IIF function for simple conditional logic in Tableau.

Conclusion

In this Tableau tutorial, we have learned how to use the IF statement in different ways. In the above example, we have used true/false logic, then used multiple conditions with ELSEIF, applied aggregation functions, created nested IF statements for multi-level logic, and lastly, we used the IIF function for one-line conditions.

By following these examples, we can create calculated fields to classify data, highlight specific groups in the Tableau charts and dashboards.

You may also like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.