Overview of Tableau Functions
As every car has various functions like brakes, accelerator, clutch, wipers, headlights, AC, Music System, Left Right Indicators, etc. which helps us to drive a car comfortably and appropriately. Similarly, in Tableau, we have different function categories, and in each category, we have a number of functions that help us to perform calculations and visualize the data as per our requirements, that too easily and expeditiously.
On the basis of the type of the data which needs to be visualized, aggregation and logic operation required in the visualization the tableau functions are divided into different categories. Each category has functions for a specific purpose which is explained below.
Types of Tableau Functions
- String Function
- Number Function
- Date Function
- Aggregate Function
- Logical Function
- Type Conversion Functions
How to Apply the Tableau Function?
To apply Tableau function, we need to create a calculated field that can be created by clicking on the Analysis tab in tableau worksheet, and then on “Create calculated field option” from the drop-down menu as shown in Figure 1 below and highlighted in red.
After clicking on Create calculates field option you will find a drop-down menu that contains a list of all tableau functions with their description and example. You can select function from the drop-down menu or you can search in the text box as shown in Figure 2. Make sure you name your calculated field before clicking on Apply and OK. In figure 2 I have named my calculated field as “Contains”.
Tableau calculation field editor also let us know whether our calculation is valid or not, you can see this indicator at the bottom of the editor.
After clicking on OK you can drag and drop the calculated field from measure/Dimension in the Sheet to see the results.
String Functions allow us to manipulate the String data according to our requirements. For Example, we can use the string function to fetch the last or first names of employees from the employee name column from the employee table and put it into the new field or column. We can also use this function to concatenate city and country details and populate it into a single column etc.
List of popular String functions in Tableau is as follows:
It returns the left most substring in the string.
Syntax: LEFT (string, number)
Example: LEFT (“MR. Ghoradkar”,2) = “MR”. ”2” represents the length of the substring.
In the below screenshot we can see when the calculation field “LEFT Fn” was drag and dropped in the sheet we got the result as MRS only as get wrote ‘3’ in our function.
RIGHT is very similar to LEFT in right it returns the rightmost substring in the string.
Mid is also similar to the LEFT and Right function. MID returns substring from a specified index position to the specified length.
Syntax: MID(string, number, number)
Example: MID(“umbrella”,2,4). Result: “brel”
Note: The index in a string starts from “0”.
It returns the ASCII code of the first character of the string.
4.5 (2,184 ratings)
Example: ASCII(“A”). Result: 65
Note: Make sure you select “Attribute” by right-clicking on the calculated field to get the desired result. As shown in the picture below.
It is the exact opposite of ASCII; It returns the character encoded by the ASCII code number.
Example: CHAR (65) = ‘A’.
Returns ‘TRUE’ if the given string contains a specified substring.
Syntax: CONTAINS(string, substring).
Example: CONTAINS (“Maharashtra”, “rash”). = “TRUE”.
Use Case: You can put a dimension in place ‘Maharashtra’ and visualize the data. For example, if you have a dimension named “NAME” which contains the name of all customers of your bank and if you want to get the customer IDs of all employees whose name contains “as” in their name then you can use below query in your text editor. You also need to apply a filter so that you get customer IDs with the result as ‘True’.
CONTAINS([Name],”as”). The result is shown in the picture below.
Returns true if the given string ends with given substring
Syntax: ENDSWITH (string, substring).
Example: ENDSWITH(“DONKEY”,”KEY”) = True.
STARTSWITH is very similar to ENDSWITH, it returns true if the string starts with the given substring.
Return the index position of the substring in the string.
Syntax: FIND (string, substring, [start]). We need to specify the starting point to search in the string in the third parameter.
Example: FIND (“maximum”, ”m”, 2). = 5.
Returns the length of the string.
Syntax: LEN (string).
Example: LEN (“Helicopter”) =10.
Return string, with all characters uppercase.
Syntax: UPPER (string).
Example: UPPER (“BlackHat”) = “BLACKHAT”.
LOWER is very similar to UPPER, It returns a string with all the characters lowercase.
Returns the maximum of a and b.
Example: MAX (“Tiger”, ”Cat”) =”Tiger”.
MIN is very similar to MAX, it returns the minimum of a and b.
Returns the string with no leading and trailing spaces.
Syntax: TRIM (string).
Example: TRIM (“ vegetables ”) =”vegetables”.
LTRIM and RTRIM are very similar to TRIM, it returns the string with leading spaces removed and It returns the string with trailing spaces removed, respectively.
Returns a substring from the input, using a delimiter to divide it.
Syntax: SPLIT (string, delimiter, token).
Example: SPLIT (‘a-b-c-d’,’-‘, 2) =’b’
Note: Here token number is the position of the substring after excluding delimiter. We can also use -2 or -3 etc. to get the result from the end of the string.
Number functions allow us to perform the calculation on the data. It can only be used with measures i.e. field containing numeric values. Number functions can help us to calculate square, square root, power, exponential, etc. of the numeric data. For example, if we have a data set of banks in which we want to round off the balance of all our customers then we can use the ROUND function. List of popular Number functions in Tableau is as follows:
Returns the absolute value of the given number.
Syntax: ABS (number).
Example: ABS ([10.75]). = 11
Returns the maximum of the two arguments of the same type.
Syntax: MAX (number, number).
Example: If revenue and Expense are the two measures then the max function will return maximum between them. MAX (“Revenue”, ”Expense”). Another example in the below picture.
MIN is very similar to MAX; it returns the minimum of the two arguments of the same type.
Rounds a number to the nearest integer of equal or greater value.
Syntax: CEILING (number).
Example: CEILING (4.2654) =5.
FLOOR is very similar to CEILING, Rounds a number to the nearest integer of equal or lesser value.
Example: FLOOR (4.2654) =4.
Rounds the number to the specific number of digits.
Syntax: ROUND (number, [decimal]).
Example: ROUND (6.66667, ) =6.67.
Returns the sine of an angle. Specify the angle in the radian.
Example: SIN(PI ( ) /4) =0.707106.
COS, TAN, and COT are very similar to SIN. Returns the cosine, tangent, and cotangent of an angle, respectively.
Returns the numeric constant PI: 3.14159.
Syntax: PI ().
Converts the given number from radians to degrees.
Syntax: DEGREE (number).
Example: DEGREE (PI () /4) =45.
RADIANS is very similar to DEGREE. Converts the given number from degree to radians.
Returns the square of a number.
Syntax: SQUARE (number).
Example: SQUARE (5) =25.
SQRT is very similar to SQUARE. It returns the square root of a number.
Returns e raised to the power of the given number.
Syntax: EXP (number).
Example: EXP (- [Growth Rate] * [Time])
Raises the number to the specified power.
Syntax: POWER (number, power).
Example: POWER (4, 2) = 4^2 =16.
Returns the integer part of a quotient, where integer1 is divided by integer2.
Syntax: DIV (Integer1, Integer2).
Example: DIV (22, 2) =10.
Date Functions allow us to manipulate data related to time, year, date and days. It can be handy to convert days in the hours or to generate the “Last modified” date during data visualization. List of popular Date functions in Tableau is as follows:
Returns the current date.
Syntax: TODAY( ).
Example: TODAY( ) = 2019-09-26.
Returns the day of the given date as an integer.
Example: DAY(#2019-09-26#) =26.
MONTH and YEAR are very similar to DAY. It returns the month and year of the given date as an integer, respectively.
Returns the current date and time.
Syntax: NOW( ).
Example: NOW( ) =2019-09-26 07:26:21 PM.
Returns the difference between date1 and date2.
Syntax: DATEDIFF (date_part, date1, date2, [start_of_week])
Example: DATEDIFF(‘week’, #2019-09-22#, #2019-09-25#, ‘Monday’) =2.
Returns true if the given string is a valid date.
Example: ISDATE(“may 24, 2019”) =True.
Type Conversion Functions
Type Conversion Functions allow us to modify the data type of the source data and convert it from one type to another. Using these functions, we can convert Dates into strings and vice versa, we can also convert numbers in to strings and strings into numbers to prevent or activate aggregation by Tableau. List of popular Type Conversion functions in Tableau is as follows:
Returns a date given a number, string or date.
Example: DATE(“May 24, 2019”) =#May 24, 2019#.
Casts its argument as a floating-point number.
Example: FLOAT(3) =3.000.
Casts its argument as an integer.
Example: INT(8.0/3.0) =2.
Casts its argument as a string.
Example: STR([Rank]). In this example, you can cast all numeric values in the Rank measure into a string. Once you do this the calculated field will appear in dimension instead of measure.
Aggregate Function allows us to summarize the data by performing various operations like SUM, COUNT, PERCENTILE, AVG, etc. It also helps us to apply aggregate functions across regions, countries, etc. For example, we can calculate the average, sum, etc. of a particular fields like Sales, Profit, Balance, etc. across a country or state.
Returns the average of all the value in the expression.
Example: AVG(Balance). As shown in the below picture where the average balances across Region is taken.
Returns the number of items in a group.
Returns the maximum expression in all the records.
MIN is very similar to MAX, Returns the minimum expression in all the records.
Returns the sum of all value of the expression.
Logical functions allow us to take decisions based on Boolean logic i.e. True or False.
Tests an expression returning <then> value for a true <expr>.
Syntax: IF <expr> THEN <then> [ELSEIF <expr2> THEN <then2> …] [ELSE <else>] END.
Example: IF [Profit] > 0 THEN ‘profitable’ ELSE ‘loss’ END.
Return <expr1> if it is not null, otherwise return <expr2>.
Syntax: IFNULL(expr1, expr2).
Example: IFNULL([sales], 0).
Checks condition; returns first value if TRUE, second if FALSE and third if unknown.
Syntax: IFF(test, then, else, [unknown]).
Example: IFF([profit] > 0, ‘profit’, ‘loss’).
Returns true if expression is null.
Example: ISNULL(89). = False.
Performs logical negation on an expression.
Syntax: IF NOT <expr> THEN <then> END.
Example: IF NOT [loss] < 0, THEN “unprofitable” END.
Performs a logical ‘and’ on two expressions.
Syntax: IF <expr1> AND <expr2> THEN <then> END.
Example: IF (ATTR([market]) = “Africa” AND SUM([sales]) > [Emerging Threshold] THEN “Well Performing”),
Tests a series of expressions returning the <then> value for a true <expr>.
Syntax: IF <expr> THEN <then> ELSE <else> END.
Example: IF [profit] > 0 THEN ‘profitable’ ELSE ‘loss’ END.
Tableau has provided various functions to manipulate the data according to our requirement, these functions help us to perform required calculations, make certain decisions, change the data type, combine different column, etc.
Tableau Functions plays a vital role in data visualization and can help us to enhance the look and feel of the data and also helps us to create the dashboards efficiently.
This is a guide to Tableau Functions. Here we discuss all the Tableau Functions built into Tableau Software. You may also have a look at the following articles to learn more –