How to Concatenate in Tableau Calculated Field
Combining text in Tableau lets you create dynamic labels, descriptive tooltips, and clearer fields in your visualizations. The process, known as concatenation, is surprisingly simple once you learn the basic formula and a couple of key functions. This tutorial will walk you through exactly how to concatenate strings in Tableau, from simple combinations to handling different data types and pesky null values.
What is Concatenation and Why Use It in Tableau?
Concatenation is just a technical term for joining text strings together, end-to-end. If you have "First Name" and "Last Name" in separate columns, concatenating them lets you create a new "Full Name" column. Think of it as creating a more complete and context-rich piece of information from separate data points.
Here are a few common scenarios in Tableau where concatenation is incredibly useful:
- Creating Full Names: The most classic example. Combining
[First Name]and[Last Name]fields into a single[Full Name]. - Building Dynamic Labels for Charts: Instead of a static chart title like "Sales," you can create a dynamic label that changes with your filters, such as "Sales for Texas: $50,432."
- Crafting Informative Tooltips: You can combine multiple fields to give users rich context when they hover over a data point, such as "Product: T-Shirt | Amount: $25 | Order Date: 1/1/2023."
- Making Unique IDs: Sometimes you need to create a unique identifier by joining two fields, like combining an
[Order ID]and a[Product ID]to get a unique[Order Line Item ID]. - Formatting Calculations for Readability: Concatenating lets you add descriptive text to numbers, like adding " days" after a calculation that measures shipping time.
The Simplest Method: Using the '+' Operator
Tableau makes basic text concatenation straightforward with the plus (+) operator. To get started, you’ll need to create a calculated field.
How to Create a Calculated Field for Concatenation
- In the Data pane on the left side of your Tableau worksheet, right-click and select Create Calculated Field.
- A dialog box will open. Give your new field a descriptive name (e.g., "Customer Full Name").
- In the formula box, you'll type your concatenation logic.
- Click OK when you're done. Your new field will appear in the Data pane for you to use in your viz.
Let's use this process to combine a first and last name. Imagine you have a field called [First Name] and another called [Last Name].
At first, you might try this formula:
[First Name] + [Last Name]While this technically works, it will produce results like "JohnSmith" or "JaneDoe" because we didn't tell Tableau to add a space. To fix this, you need to add a space as its own text string (enclosed in double quotes) in the middle.
The correct formula is:
[First Name] + " " + [Last Name]Now, your result will be nicely formatted: "John Smith" or "Jane Doe". You can join any strings this way. The key is that anything you want to add literally - like a space, a comma, or a hyphen - must be enclosed in quotation marks.
Handling Different Data Types with the STR() Function
The biggest hurdle you'll encounter is trying to combine text with numbers, dates, or other non-string data types. If you try to join a string and a number with the + operator, Tableau will give you an error message like "Can't add string and integer values."
This is where the STR() function becomes your best friend. The STR() function converts any data type into a string, so Tableau can then happily concatenate it with another string.
Example: Combining Text and Numbers
Let's say you want to create a label that combines a customer's name with their customer ID. You have the fields [Customer Name] (a string) and [Customer ID] (a number).
Incorrect formula that will cause an error:
[Customer Name] + " (ID: " + [Customer ID] + ")"To fix this, you just need to wrap the numeric field, [Customer ID], in the STR() function:
[Customer Name] + " (ID: " + STR([Customer ID]) + ")"This formula tells Tableau: "Take the customer's name, add the literal string ' (ID: ', then convert the [Customer ID] number to a string, then add a closing parenthesis." The output will look clean and error-free: "John Smith (ID: 12345)".
Example: Combining Text and Dates
The same logic applies to dates. If you want to create a dynamic sentence like "This order was shipped on [Ship Date]", you must convert the date field into a string.
Incorrect formula:
"Shipped on: " + [Ship Date]Correct formula using STR():
"Shipped on: " + STR([Ship Date])This will produce output like "Shipped on: 2023-11-20", allowing you to dynamically add dates into your view titles, labels, or tooltips.
Dealing with NULL Values in Concatenation
NULL values can throw a wrench into your concatenation efforts. A NULL in data means a value is missing or unknown. If any single field in your concatenation formula is NULL, the entire result will become NULL. This can leave you with frustrating blank spots in your dashboards.
Imagine you're creating a full name again, but this time you have fields for [First Name], [Middle Name], and [Last Name]. Many of your contacts might not have a middle name, meaning the [Middle Name] field is NULL.
This formula will not work as expected:
[First Name] + " " + [Middle Name] + " " + [Last Name]If Jane Doe doesn't have a middle name, the [Middle Name] field is NULL. The entire formula evaluates to NULL, and "Jane Doe" disappears from your list. This is not what we want.
Tableau gives us functions to gracefully handle NULLs. The most flexible is IFNULL().
Using IFNULL() to Manage NULL Values
The IFNULL() function checks if a field is null and lets you substitute it with a different value if it is. The syntax is simple:
IFNULL(expression_to_check, value_to_substitute)To fix our middle name problem, we can use IFNULL() to tell Tableau: "If the middle name exists, use it. If it's NULL, substitute it with an empty string ('')."
To make our logic more robust, we can get even smarter. We want to include the space after the middle name only if the middle name exists.
Here’s the advanced, clean solution:
[First Name] + IFNULL(" " + [Middle Name], "") + " " + [Last Name]Let's break that down:
[First Name] + ...: It always starts with the first name.IFNULL(" " + [Middle Name], ""): This is the important part. It tries to create the string " [Middle Name]". If[Middle Name]is NOT NULL, this works perfectly (e.g., it produces " A."). If[Middle Name]IS NULL, this entire expression insideIFNULL()becomes NULL, triggering the fallback. The fallback is an empty string,"". So, it adds nothing.+ " " + [Last Name]: It finishes with a space and the last name.
This formula correctly produces "John F. Kennedy" when there's a middle name and "Jane Doe" when there isn't, all without leaving blank rows.
Specifically for Numbers: The ZN() function
When dealing with numbers that might be NULL, the ZN() function is a handy shortcut. It stands for "Zero NULL" and does exactly that: it returns a 0 if the field is NULL, otherwise, it returns the field's value. This is cleaner than writing IFNULL([Sales], 0).
For example, if you want to display "Profit: $0" instead of just "Profit: " when the profit field is NULL:
"Profit: $" + STR(ZN([Profit]))More Practical Examples and Tips
Now that you have the building blocks, let's explore some more advanced use cases.
1. Creating Dynamic Labels with Line Breaks
Sometimes you want your tooltips or labels to have multiple lines for readability. You can create a line break within a calculated field using CHAR(10).
Let's say you want a tooltip to show the Customer Name on the first line and their Sales on the second.
[Customer Name] + CHAR(10) + "Total Sales: $" + STR([Sales])Drag this calculated field to the Tooltip mark. Now, when a user hovers over the data point, they will see a neatly formatted tooltip: John Smith Total Sales: $1250
2. Formatting Numbers Inside a Concatenated String
The standard STR() function offers no formatting options. If your number is 1234.5678, STR() will convert the entire thing. To control this, you can nest functions. Use the ROUND() function on your number before converting it to a string.
To show a profit ratio as a percentage with one decimal place:
"Profit Ratio: " + STR(ROUND([Profit Ratio] * 100, 1)) + "%"This formula first multiplies the ratio (e.g., 0.253) by 100 to get 25.3, rounds it to one decimal place, converts that number to a string, and then adds the percent sign at the end.
3. Building a Dynamic URL for Action Filters
A powerful application of concatenation is creating dynamic URLs for dashboard actions. You can build a clickable URL that passes a value from your dashboard into a web search.
For instance, to create a URL that Googles a product name from your viz:
"https://www.google.com/search?q=" + [Product Name]You can then use this calculated field in a URL dashboard action. When a user clicks a product in your dashboard, it will open a new browser tab with a Google search for that product.
Final Thoughts
Mastering string concatenation in Tableau unlocks a new level of customization and clarity for your dashboards. At its core, it's about joining data with the + operator. You just need to remember to convert non-strings with STR() and gracefully handle potential blanks with IFNULL() and ZN(). With these functions, you can build dynamic titles, rich tooltips, and useful calculated dimensions that make your data stories much more compelling.
While writing formulas is powerful, the process of finding the right function, managing different data types, and troubleshooting NULL values takes time. At Graphed, we've designed a way for you to get these answers without writing code or formulas. Instead of building a series of calculated fields, you can just ask a question like, "create a chart showing sales by state with a label that says 'Sales for [State] topped $[Sales]'" and our AI data analyst builds it instantly. By connecting your data sources directly to Graphed, you can replace the tedious parts of data prep with simple, natural language questions, letting you stay focused on the insights, not the syntax.
Related Articles
How to Enable Data Analysis in Excel
Enable Excel's hidden data analysis tools with our step-by-step guide. Uncover trends, make forecasts, and turn raw numbers into actionable insights today!
What SEO Tools Work with Google Analytics?
Discover which SEO tools integrate seamlessly with Google Analytics to provide a comprehensive view of your site's performance. Optimize your SEO strategy now!
Looker Studio vs Metabase: Which BI Tool Actually Fits Your Team?
Looker Studio and Metabase both help you turn raw data into dashboards, but they take completely different approaches. This guide breaks down where each tool fits, what they are good at, and which one matches your actual workflow.