Using Calculated Fields to Work With Strings in Tableau

by Adam Sultanov

There are a ton of functions in Tableau for working with strings in calculated fields. This is a list of all the functions you might want to use, with brief descriptions for each and some examples to make things clear.

ASCII() returns the ASCII code value of the first character in a string. CHAR() does the opposite, turning an ASCII code back into a character.

CONTAINS() returns a Boolean (true/false) value, true if the substring exists in the string. STARTSWITH() and ENDSWITH() do the same operation on either end of the given string. They all follow the format CONTAINS(string, substring).

FIND() and FINDNTH() both return the integer position within a string that a substring is found, or 0 if it's not found. FINDNTH() also takes a number to return the nth instance of the found substring. Example:

FINDNTH('banana', 'na', 2) = 5

LEFT(), MID(), and RIGHT() take a string and then the number of characters you want returned from the string. LEFT() takes those characters from the beginning of the string, RIGHT() takes those characters from the end, and for MID() you include an extra number indicating where in the string to start from, with the length being optional. Example:

MID('Hello World', 7) = 'World'

LEN() simply returns the number of characters in a given string.

LOWER(), UPPER(), and PROPER() all transform the strings that you give them. LOWER() converts a string to all lowercase, UPPER() to all uppercase, and PROPER() capitalizes the first letter of each word while making the rest lowercase.

TRIM(), LTRIM(), and RTRIM() all remove whitespace from strings. LTRIM() removes whitespace from the beginning, RTRIM() removes it from the end, and TRIM() removes it from both sides.

REGEXP_MATCH(), REGEXP_REPLACE(), and REGEXP_EXTRACT() are all string functions for working with regular expressions, which are outside the scope of this blog post. They all take a string and a regular expression pattern. REGEXP_MATCH() returns true if a match is found, REGEXP_REPLACE() takes an additional string to replace a matched pattern, and REGEXP_EXTRACT() returns a substring if it is found.

SPACE() returns a string of the length you give it, made up of empty spaces.

SPLIT() takes a string, a given delimiter, and a token number. The string is split wherever the delimiter is found, which could result in any number of parts. The token number refers to which one of those parts to return. Example:

SPLIT('abc-123-xyz', '-', 2) = '123'

Tue 28 Nov 2023

Mon 27 Nov 2023

Wed 22 Nov 2023

2 mins read

Mon 16 Oct 2023