Tableau Logical and String Functions
Logical functions let Tableau make decisions. String functions let Tableau clean and reshape text. Together they turn raw columns into labels, flags, and categories that charts can use directly.
Logical Functions
IF, THEN, ELSE
The IF function tests a condition and returns one result when true and another when false.
IF [Profit] > 0 THEN "Profitable" ELSE "Loss" END
Add ELSEIF to test several conditions in order. Tableau stops at the first true condition.
IF [Sales] >= 1000 THEN "High" ELSEIF [Sales] >= 500 THEN "Medium" ELSE "Low" END
Decision Diagram
Sales >= 1000? --Yes--> "High"
|
No
|
Sales >= 500? --Yes--> "Medium"
|
No
|
v
"Low"
IIF
IIF writes a simple two-way decision on one line: IIF([Profit] > 0, "Gain", "Loss"). It accepts a third result for unknown values.
CASE
CASE matches one field against a list of exact values. The code reads cleaner than a long chain of IF statements.
CASE [Region] WHEN "East" THEN "Zone A" WHEN "West" THEN "Zone B" ELSE "Zone C" END
AND, OR, NOT
Combine conditions with AND, OR, and NOT. The expression [Sales] > 500 AND [Discount] = 0 returns true only when both parts hold.
Handle Missing Values
- ISNULL([Field]) returns true when a value is missing.
- ZN([Field]) converts a missing number to zero.
- IFNULL([Field], "Unknown") replaces a missing value with a backup.
String Functions
Measure and Trim Text
- LEN("Tableau") returns 7.
- TRIM(" Tableau ") removes extra spaces at both ends.
Change Case
- UPPER("sales") returns SALES.
- LOWER("SALES") returns sales.
Extract Pieces of Text
Value: "NY-10025" LEFT([Code], 2) -> "NY" RIGHT([Code], 5) -> "10025" MID([Code], 4, 5) -> "10025"
Find and Replace
- CONTAINS([Product], "Chair") returns true when the word appears inside the text.
- STARTSWITH([Product], "Office") checks the beginning of the text.
- REPLACE([Name], "Inc.", "") removes unwanted text.
Split Text Into Parts
SPLIT breaks a string at a separator and returns the chosen piece. SPLIT("Ana Smith", " ", 1) returns Ana, and token number 2 returns Smith. A menu option helps too: right-click a string field and choose Transform, then Split.
Combine Both Types
Nested functions solve real tasks. A flag for premium chairs needs both a text check and a number check.
IF CONTAINS([Product Name], "Chair") AND [Sales] > 300 THEN "Premium Chair" ELSE "Other" END
Practice Ideas
- Build a profit status field with IF and show it on Color.
- Build a short customer initial field using LEFT.
- Build a clean category label by combining TRIM and UPPER.
Common Errors
Missing END
Every IF and CASE statement closes with END. A missing END produces a red error message in the calculation editor.
Mixed Result Types
All branches must return the same data type. A text result in one branch and a number in another triggers an error.
Case-Sensitive Matches
Text comparisons can differ by capitalization depending on the data source. Wrap fields in LOWER to compare safely.
Summary
IF, IIF, and CASE create categories and flags from conditions. ISNULL, ZN, and IFNULL handle gaps in data. String functions such as LEFT, MID, TRIM, CONTAINS, and SPLIT clean and reshape text. Nest them together to build custom fields without touching the source data.
