Sometimes you need to customize data fields that are text, like combining or separeting feilds. Somewhat like numeric fields and you can add text or string data fields in tableau. String data is any data that’s text, rather than numbers,dates or locations. Also, Strings are often used for names of people or objects. In this lesson, we will learn hot concatenate string fields in tableau.
To concatenate strings means to add them together. Let’s look at Superstore, which has sales data. For our view, we want city, state and zip code shown together as one field. We can do this, by creating a calculated field. To learn more detail about calculated field visit previous article.
Let’s start our concatenated field by adding just city and state. Don’t forget to add plus sign between city and state. Then you will see “The calculation valid” in the corner of the window. But the expression of The calculation is valid is not enough. You have to be awake in every part of your data analysis. Then drag the new calculated field to rows. Did you see? there is something wrong. It consist of missing space. We will go back and edit our calculation.
Edit Calculated Field
As you may have noticed, tableau ignores spaces between fields and operators in calcuations. We just put spaces in the calculation to make it easier to read. If you want to add spaces and other punctuation to the result of the calculation. You need to close them in quotations to mark them as string text. Since the standard format is to have comma and a space between city and state. Let’s add that to our calculation. Since we want to add this fixed string to our field, And a string that doesn’t change when the data changes, we will use the plus sign again and wrap our “comma space” in quotes. It seems a bit of comlicated but dont worry. You can see the calculation in below as a picture.
Click to apply and see what will be happen.
Now let’s add the postal code to this field using the plus operator. Once we add that field, tableau tells us there is an error. If you hove over the red underline, tableau tells us that it cannot combine string and integer data. Keep in mind, that while the postal code is geographic data, it’s fundamentally a number. When adding fields together the fields must be the same data types. We can’t concatenate these fields without converting postal code to a string. When converting the data type within a calculation, we’re just changing the data type for this instance of postal code. Also, remember tableau is read only so we’re not writing this change back to the database.
Learn Data Type Conversion
Let’s look at some of the data type conversions we can do, by looking at the type conversion functions in the editor.
You can ask tableau to convert data into any data type. For our calculation, we will use the string function with the postal code field selected, we can just double click “STR” and it will bring over the brackets and correct formatting.
I like to rely on tableau’s auto-complete functions as much as possible. Now that postal code is converted tableau removes the red underline and tells us our calculation is valid. Click apply and ok to see what will happen.
And its done. In the format we expected : city, state, zip code. In this section we have learned how to maipulate the data types to make fit according to your analysis. I hope that this article will help you. Don’t forget to share and like this article. we will be meet in the next article.