10 Dec Tableau – String Calculations
String calculations in Tableau are used to manipulate and analyze text data. Let us see some examples to understand the concept.
Types of String Calculations
The following are some common types of string calculations. Consider fields as Field A, Field B, etc.
Basic String Calculations
- Concatenation: Field A + ‘ ‘ + Field B (combine strings)
- Substring: SUBSTRING(Field A, 1, 5) (extract part of string)
- Length: LEN(Field A) (string length)
- Upper/Lower Case: UPPER(Field A) or LOWER(Field A) (change case)
- Trim: TRIM(Field A) (remove leading/trailing spaces)
Text Manipulation Calculations
- Replace: REPLACE(Field A, ‘old’, ‘new’) (replace substring)
- Split: SPLIT(Field A, ‘,’) (split string into array)
- Join: JOIN(Field A, ‘,’) (combine array into string)
- Parse: PARSE(Field A, ‘format’) (extract data from string)
Regular Expression (RegEx) Calculations
- REGEXP_MATCH: REGEXP_MATCH(Field A, ‘pattern’) (match pattern)
- REGEXP_REPLACE: REGEXP_REPLACE(Field A, ‘pattern’, ‘replacement’) (replace matches)
- REGEXP_EXTRACT: REGEXP_EXTRACT(Field A, ‘pattern’) (extract matched text)
Date and Time String Calculations
- DATEPARSE: DATEPARSE(‘format’, Field A) (convert string to date)
- DATETOSTRING: DATETOSTRING(Field A, ‘format’) (convert date to string)
- TIMESTAMP: TIMESTAMP(Field A) (convert string to timestamp)
Advanced String Calculations
- Find: FIND(Field A, ‘substring’) (find substring index)
- Contains: CONTAINS(Field A, ‘substring’) (check if substring exists)
- StartsWith/EndsWith: STARTSWITH(Field A, ‘substring’) or ENDSWITH(Field A, ‘substring’)
Example
Let us create a new sheet and name it String Calculations in the same Amit_Tableau_Calculations.twb Tableau Workbook. We created the Amit_Tableau_Calculations.twb Tableau workbook in the Numeric Calculations lesson.
Go to the Analysis menu and click on it. After that, click Create Calculated Field:
Click on the arrow to display all the built-in functions. From the dropdown, click String to display only the string functions:
Set the name of the calculation. We will set it to City_C to find the sales of the city that begin with the letter C.
From the right, select the STARTSWITH() function. After selecting, on the left, set the formulae for the cities starting with the letter C i.e., STARTSWITH([City],’C’).
The formulae can be seen and there are no error messages. Click OK:
On the left, the new calculation field is visible i.e., City_C. The STARTSWITH() returns True if the string starts with the mentioned substring i.e., in this case, it was the letter C:
Drag the Sales to the Columns shelf, and the new City to the Rows shelf.
After dragging, the bar chart will be visible as shown below.
Drag the new field City_C to the Filters section. After dragging, only the True field is checked and that is fine for us. Click Ok:
Now, only the True values are visible i.e. the sum of sales for the cities starting with the letter C. Keep the mouse cursor on any of the bars to get the details:
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments