How to Concatenate in Tableau: Quick Guide With Examples

by | Tableau

Are you ready to concatenate!?

When processing data, a common operation that you’ll often perform is concatenation.

It refers to the process of combining two or more fields into a single field. In Tableau, concatenation is often used to merge or combine strings, such as a customer’s first and last name, to create a unified field.

To concatenate in Tableau:

  1. Go to the Analysis menu and select Create Calculated Field.
  2. Name the new calculated field to your preference.
  3. Enter the concatenation formula, for example, [FirstName] + [LastName].
  4. Click OK to create the new concatenated field.
How to Concatenate in Tableau

This article will guide you through the process of concatenating fields in Tableau. There will be examples to help you better understand the concepts. As you work on more complex projects, you’ll notice that mastering concatenation becomes an essential skill.

Let’s get started!

How to Concatenate Using the ‘+’ Operator

To start concatenating using the ‘+’ operator, open up Tableau and import the dataset you’d like to analyze.

How to Concatenate Using the '+' Operator

After loading a dataset, follow the steps given below to concatenate multiple fields in Tableau:

Step 1: Navigate to the Formula Field

The first thing you need to do is to create a calculated field. You can do this by right-clicking in the Data pane and selecting “Create Calculated Field.”

Navigating to the formula field to use arithmetic and statistical functions

Step 2: Use the ‘+’ Operator

In the formula field, input the fields or strings you want to concatenate, separated by the ‘+’ operator.

For example, if want to combine multiple string fields named ‘Customer Name’ and ‘Order ID’ with a space in between, you’d write:

[Customer Name] + " " + [Order ID]

The demonstration is shown below:

Using the '+' Operator

Step 3: Finish and Test

After finishing the formula, click OK. When Tableau is done combining multiple fields, drag your newly created field into a view to check the results.

Finishing and Testing

It’s important to ensure that any fields you’re trying to concatenate are in the string format or converted to string format using STR() function to avoid errors.

In the next section, we’ll take a look at how you should deal with non-string data types.

How to Deal With Non-String Data Types

In data visualization and analysis, it’s common to encounter a mix of data types, from numbers and dates to geographic locations.

When it comes to concatenating multiple fields in Tableau, not all data types play seamlessly together.

Specifically, Tableau will throw an error if you try to concatenate non-string data directly with string data using the ‘+’ operator.

In this section, we’ll discuss the following:

  1. Using the STR() function for numbers
  2. Handling Date and DateTime fields
  3. Handling NULL values
How to Deal With Non-String Data Types

1. How to Use the STR() Function for Numbers

The STR() function is your go-to tool to convert numeric data fields into strings for concatenation.

In your calculated field, wrap the numeric field with the STR() function before concatenation.

For instance, if you want to concatenate a string field ‘Product Name’ with a numeric field ‘Quantity’, the formula would be:

[Product Name] + " has " + STR([Quantity]) + " units."

The demonstration is shown below:

Using the string conversion function str() on non string values

2. How to Handle Date And DateTime Fields

If you have dates as the field values and you want to convert them into a string, you can use the DATE() function.

For example:

STR(DATE([Your Date Field])).

The demonstration is shown below:

Handling Date and DateTime fields from multiple data sources

If you’re dealing with a DateTime field, you can split the date and time or use formatting functions like DATEPART() to extract specific parts (like just the year or month) before converting with STR().

3. How to Handle NULL Values

When concatenating, NULL values can result in unexpected outcomes.

You can use the IFNULL() function to handle these, replacing NULLs with a default string or value.

For example, when combining product names with their prices, you can use:

"Price of " + [Product Name] + " is $" + STR([Price]).

Now that we’ve gone over the fundamentals, we’ll take a look at some practical use cases for concatenation in the next section.

Practical Use Cases for Concatenation in Tableau

Concatenation might seem like a rudimentary operation, but it often serves as the foundation for enhanced data visualization and storytelling in Tableau.

Combining fields or values into customized strings allows you to add clarity, provide context, or simplify information presentation.

Let’s explore some practical scenarios where concatenation truly shines.

Practical Use Cases for Concatenation in Tableau

1. Creating Dynamic Titles or Headers

With concatenated fields, you can create dynamic titles for your charts or dashboards that change based on user filters or selections.

For example, if you have a line chart showing sales over time, the title could dynamically display the range:

"Sales from " + STR(DATE(MIN([Date]))) + " to " + STR(DATE(MAX([Date]))).

The demonstration is shown below:

Creating dynamic titles or headers after connecting multiple data sources

2. Custom Tooltips for Enhanced Context

Tooltips offer additional insights when users hover over specific data points. You can craft tailored messages that convey more than raw data through concatenation.

For example, when you hover over a bar in a sales chart could, you can configure it to display:

[Product] + " had sales of $" + STR([Sales]) + " in " + STR(DATE([Month]))

3. Generating Combined Keys for Data Joining

Sometimes, data sources lack a unique identifier for joins.

Concatenation allows you to create composite keys by merging multiple fields, facilitating more accurate data blending.

For example, if you are joining on ‘City’ and ‘State’, a combined key can be:

[City] + ", " + [State]

The demonstration is shown below:

Generating combined keys for particular context based purpose to combine multiple fields

The above are just a few use cases of concatenation and there are numerous others that you’ll explore and utilize over time.

If you’d like a deep dive into the future of data tech, check out the following video:

Final Thoughts

Mastering concatenation becomes crucial as you delve deeper into the world of data visualization with Tableau. It’s a powerful data visualization tool that allows you to craft a clear narrative from your data, making your visuals more intuitive and engaging.

By learning and using concatenation, you’re equipping yourself with a tool that bridges data points and enhances the richness of your stories.

By embracing this skill, not only do you make your work in Tableau more efficient, but you also amplify the value of your visualizations. Happy analysis!

Frequently Asked Questions

In this section, you’ll find some frequently asked questions you may have when concatenating in Tableau.

Coworkers discussing an analytics report

How can I concatenate fields with null values in Tableau?

To concatenate fields with null values in Tableau, you can use the IFNULL function.

This function helps replace null values with a default value or an empty string, allowing for seamless concatenation.

Here’s an example:

IFNULL([Field1],'') + IFNULL([Field2],'')

This expression concatenates Field1 and Field2, replacing null values with empty strings.

What’s the best approach to concatenate dimension values in Tableau?

Concatenating dimension values in Tableau can be done using the plus sign (+) in a calculated field.

Simply create a new calculated field and enter the following formula:

[Dimension1] + [Dimension2]

This formula concatenates the values from Dimension1 and Dimension2.

How can I concatenate rows into a single string in Tableau?

Concatenating rows into a single string in Tableau is not feasible through built-in functions, as Tableau is designed for visualizing data and not text manipulation.

However, you can explore options like reshaping your data in the data source or using custom scripts to achieve this purpose.

What is the method to convert numbers to strings in Tableau?

To convert numerical values to strings in Tableau, you should use the STR() function.

Here’s an example of how to convert the numeric field “Sales” to a string:

STR([Sales])

This formula will convert the Sales field to a string type, making it possible to concatenate with other string values.

How can I combine two values into one in Tableau?

To combine two values into one in Tableau, you can choose to concatenate string fields or perform arithmetic operations on numeric fields, based on your requirements.

For concatenating string fields, you can use the plus sign (+) in a calculated field:

[Field1] + [Field2]

For performing arithmetic operations on numeric fields, simply use the desired arithmetic operators (+, -, *, /) in the calculated field:

[Field1] + [Field2]

What is the process of concatenating cases in Tableau?

Concatenating cases in Tableau can be achieved using a combination of the CASE or IF statements and concatenation operations.

This allows you to conditionally concatenate values based on specific criteria. Here’s an example using the CASE statement:

CASE [Category]
  WHEN 'A' THEN [Field1] + [Field2]
  WHEN 'B' THEN [Field1] + [Field3]
  ELSE NULL
END

This expression concatenates different fields based on the value of the Category column.

Related Posts