SUMIF in Tableau: How to Create Conditional Sums
In this article
To perform a SUMIF in Tableau, you must create a Calculated Field using an IF statement inside a SUM function. Because Tableau does not have a dedicated "SUMIF" function like Excel, you achieve this by writing a formula that tells Tableau to only include values in the sum if they meet a specific criterion. For example, if you want to sum sales only for the "Technology" category, your formula would look like: SUM(IF [Category] = 'Technology' THEN [Sales] END).
What You'll Need
- A Tableau Desktop or Tableau Public workspace.
- A connected data source containing at least one numeric field (the value you want to sum) and one categorical field (the criteria you want to filter by).
- Basic familiarity with the Tableau interface, specifically the Data pane and the Calculation editor.
Step-by-Step
- Open the Calculation Editor: In the Data pane on the left side of your screen, right-click anywhere in the whitespace or click the small downward arrow next to the search bar. Select Create Calculated Field... from the menu. Note that menu labels may vary slightly depending on your version of Tableau or the operating system you are using.
- Name Your Calculation: In the dialog box that appears, give your field a descriptive name. Instead of calling it "Calculation 1," use something like "Total Technology Sales" or "Conditional Sum of Revenue." This makes it easier to identify the field when building your visualizations later.
- Write the Conditional Logic: In the formula area, start by typing the
IFstatement. The syntax follows a logical flow:IF [Dimension] = 'Value' THEN [Measure] END. For example, if you are looking for sales in the West region, you would write:IF [Region] = 'West' THEN [Sales] END. Ensure that the text value (e.g., 'West') is wrapped in single or double quotes, as Tableau treats unquoted text as field names. - Wrap the Logic in a SUM Function: To turn this into a true "SUMIF," you must wrap the entire IF statement inside a SUM function. This tells Tableau to evaluate the condition for every single row in the underlying data and then aggregate the results. The final formula should look like this:
SUM(IF [Region] = 'West' THEN [Sales] END). - Verify the Calculation: Look at the bottom left of the calculation window. Tableau will display a message saying "The calculation is valid." If you see an error message in red, double-check your brackets and quotes. Once valid, click OK.
- Apply the Field to Your View: Your new calculated field will now appear in the Data pane. Drag this field onto the Rows, Columns, or Marks card (such as Text or Label) to see the conditional sum in your visualization.
- Test with a Filter: To ensure the calculation is working correctly, drag the original dimension (e.g., Region) onto the Filters shelf and select only the value you specified in your formula. The total should match the value produced by your SUMIF calculation.
Common Mistakes
One of the most frequent errors is placing the SUM function inside the IF statement rather than wrapping the IF statement inside the SUM. For example, writing IF [Region] = 'West' THEN SUM([Sales]) END often results in a "Cannot mix aggregate and non-aggregate arguments" error. This happens because [Region] is a row-level dimension, while SUM([Sales]) is an aggregate. To fix this, always ensure the condition is evaluated at the row level first, and then the resulting list of numbers is summed.
Another common mistake is case sensitivity. Tableau is sensitive to the exact string used in the formula. If your data contains "West" but your formula looks for "west" (lowercase), Tableau will return a null value because the match is not exact. Always verify the exact spelling and capitalization of the values in your dimension field.
Users also frequently forget the END keyword. Every IF statement in Tableau must be closed with END. Without it, the calculation will remain invalid, and you will be unable to save the field.
Tips
If you need to sum based on multiple criteria (similar to SUMIFS in Excel), you can use the AND or OR operators within your IF statement. For example: SUM(IF [Region] = 'West' AND [Category] = 'Technology' THEN [Sales] END). This allows you to drill down into very specific segments of your data without needing to create multiple separate filters.
For more complex conditions, consider using the CASE statement. While IF is great for binary or simple logic, CASE is often cleaner when you have many different values for a single dimension. However, for a specific SUMIF-style result, the IF statement remains the most flexible approach.
To make your SUMIF calculations dynamic, you can replace the hard-coded string (like 'West') with a Parameter. By creating a parameter that allows the user to select a region from a dropdown menu, you can update your formula to SUM(IF [Region] = [Region Parameter] THEN [Sales] END). This turns a static calculation into an interactive tool for the end user.
Troubleshooting
If your calculation is returning Null instead of a number, the first thing to check is the data type and the exact string match. Ensure there are no leading or trailing spaces in your data. A value that looks like "West" might actually be "West " (with a space), which will cause the IF statement to fail. You can use the TRIM() function to remove these spaces: SUM(IF TRIM([Region]) = 'West' THEN [Sales] END).
If you encounter the "Cannot mix aggregate and non-aggregate arguments" error, check your parentheses. Ensure the SUM( starts before the IF and the closing parenthesis ) comes after the END. If you must use an aggregate in the condition, you will need to wrap the dimension in an aggregation like ATTR() or MAX(), though this changes how the data is calculated and is usually not recommended for a standard SUMIF replacement.
If the numbers seem unexpectedly high, check if you have duplicate data in your source. Because Tableau sums every row that meets the criteria, any duplicate records in your underlying dataset will be added to the total. You may need to use a FIXED Level of Detail (LOD) expression if you only want to sum unique values.
Frequently Asked Questions
Can I use SUMIF with dates?
Yes. You can use date functions within the IF statement. For example, SUM(IF [Order Date] > #2023-01-01# THEN [Sales] END) will sum all sales occurring after January 1, 2023. Remember to wrap dates in hashtags (#) in Tableau.
What is the difference between a SUMIF calculation and a Filter?
A filter removes all other data from the entire view, meaning you can only see one segment at a time. A SUMIF calculation creates a new measure, allowing you to compare a specific segment (e.g., West Sales) against the total sales or other segments in the same chart.
Does this work with multiple conditions?
Yes, by using AND for all conditions to be true, or OR for at least one condition to be true. You can nest these within the IF statement to create highly specific conditional sums.
Why is my SUMIF calculation showing as a Dimension instead of a Measure?
If you forgot to wrap the IF statement in a SUM() function, Tableau may treat the result as a row-level dimension. Ensure the formula starts with SUM( to force Tableau to recognize it as a measure.
Can I use this to calculate a percentage of the total?
Yes. You can divide your SUMIF calculation by the total sum of the measure. For example: SUM(IF [Region] = 'West' THEN [Sales] END) / SUM([Sales]). This will give you the percentage of total sales contributed by the West region.