01 / 20
fx

Basic Calculated
Fields in Tableau

Learn to create custom calculations using math, string, date, and boolean logic to unlock deeper insights in your visualizations.

Tableau

What Is a Calculated Field?

A calculated field is a new column you create in Tableau using a formula. It does not exist in the original data source: Tableau computes it on the fly from existing fields, constants, and functions.

Source Data Sales, Quantity, Category ... fx Calculated Field New Column

Why Use Calculated Fields?

Derive Metrics
Create profit margins, growth rates, or unit prices that are not in the raw data.
Clean Data
Fix inconsistent text, extract substrings, or parse dates without editing the source.
Add Logic
Classify rows with IF/THEN logic, flag outliers, or create custom groups on the fly.

The Four Calculation Types

Calculated Field fx Math
\( + \enspace - \enspace \times \enspace \div \enspace \% \)
String LEFT UPPER CONTAINS Date DATEDIFF DATEPART TODAY Boolean IF AND OR NOT

Every calculated field uses one or more of these categories. Many real-world formulas combine them.

Math Calculations

Use standard arithmetic operators to derive new numeric fields from existing ones.

// Profit Ratio
[Profit] / [Sales]

// Revenue per Unit
[Sales] / [Quantity]

// Discount Amount
[Sales] * [Discount]

String Calculations

Manipulate text fields: extract parts, change case, search for patterns, or combine values.

// Standardize to uppercase
UPPER([Category])

// First 3 characters of Product ID
LEFT([Product ID], 3)

// Combine first and last name
[First Name] + " " + [Last Name]

Date Calculations

Work with dates to compute durations, extract components, or shift time periods.

// Days since order
DATEDIFF('day', [Order Date], TODAY())

// Extract the year
YEAR([Order Date])

// Add 30 days to ship date
DATEADD('day', 30, [Ship Date])

Boolean Logic

Use conditional logic to classify, filter, or flag rows based on rules you define.

// Profitable or not?
IF [Profit] \(> 0\) THEN "Profitable"
ELSE "Not Profitable"
END

// Multi-condition
IF [Sales] \(> 1000\) AND [Profit] \(> 200\)
THEN "High Value"
ELSE "Standard"
END

IF vs CASE: Key Distinction

IF / ELSEIF / END CASE / WHEN / END
Tests Any condition (ranges, \(\gt\), \(\lt\), AND/OR) Exact match on a single field
Flexibility High: different fields per branch Lower: one field, many values
Readability Good for 2-3 branches Cleaner for many categories
Example Use IF \([Profit] > 0\) ... CASE [Region] WHEN ...

How to Create a Calculated Field

1
Open the editor: Right-click in the Data pane → "Create Calculated Field..." or use Analysis → Create Calculated Field.
2
Name your field: Give it a clear, descriptive name like "Profit Ratio" or "Days Since Order".
3
Write the formula: Type or drag fields into the editor. Tableau provides autocomplete and syntax highlighting.
4
Check validity: Look for the green checkmark at the bottom. If it is red, fix any syntax errors.
5
Use it: Drag the new field onto Rows, Columns, Color, or Filters like any other field.

Example 1: Profit Margin

Goal: Calculate profit as a percentage of sales for each row.

// Profit Margin
[Profit] / [Sales]
Sales Profit Profit Margin $500 $125 0.25 (25%) $1,200 $360 0.30 (30%) $800
\(-$40\)
\(-0.05\) \((-5\%)\)
\( \text{Profit} \div \text{Sales} = \text{Profit Margin} \)

Example 2: Region Grouping with CASE

Goal: Map four regions to two broader zones: "East" and "West".

CASE [Region]
  WHEN "East" THEN "Eastern Zone"
  WHEN "South" THEN "Eastern Zone"
  WHEN "West" THEN "Western Zone"
  WHEN "Central" THEN "Western Zone"
END
East South West Central Eastern Zone Western Zone

Example 3: Combining Types

Goal: Flag orders that shipped more than 5 days after the order date and label them by priority.

IF DATEDIFF('day', [Order Date], [Ship Date]) \(> 5\)
THEN "Late: " + STR(
  DATEDIFF('day', [Order Date], [Ship Date])
) + " days"
ELSE "On Time"
END
Date
DATEDIFF computes the gap between Order Date and Ship Date.
Boolean
IF checks whether the gap exceeds 5 days.
String
STR converts the number to text so it can be concatenated into a label.

Common Mistakes

✕ Missing END
Every IF and CASE statement must close with END. Forgetting it causes a syntax error.
✕ Type Mismatch
You cannot add a string to a number directly. Use STR() to convert numbers or INT() for strings.
✕ Division by Zero
If the denominator can be zero, wrap with: IF \([Quantity] \neq 0\) THEN \([Sales]/[Quantity]\) ELSE NULL END.
✕ Wrong Date Part
DATEDIFF('day', ...) is not the same as DATEDIFF('month', ...). Choose the right granularity.

Summary Cheat Sheet

Type Key Functions Example
Math \(+\), \(-\), \(*\), \(/\), \(\%\), ABS, ROUND \([Profit] / [Sales]\)
String UPPER LEFT CONTAINS REPLACE TRIM UPPER([Category])
Date DATEDIFF DATEADD YEAR MONTH TODAY DATEDIFF('day', [Order], TODAY())
Boolean IF CASE IIF AND OR NOT IF \([Profit] > 0\) THEN ... END

Question 1 of 5

Which Tableau function would you use to find the number of days between two dates?

Question 2 of 5

What is the output of LEFT("Tableau", 3)?

Question 3 of 5

You write: IF \([Sales] > 1000\) THEN "High". Tableau shows a syntax error. What is missing?

Question 4 of 5

You need to calculate \([Sales] / [Quantity]\), but some rows have \([Quantity] = 0\). Which approach prevents errors?

Question 5 of 5

Which statement is best suited for mapping a single field to many specific values (e.g., mapping region codes to full names)?