Calculations

Edit

In case you have too many calculated fields swimming in a sea of fields, this is a way to quickly find and edit them.

_images/calculate1.png

Adhoc Calculations

Ad-hoc calculations are calculations that you create on the fly, avoiding the need to create a field calculation.

Ad-hoc calculations are supported on the Rows, Columns, Marks, and Measure Values shelves; they are not supported on the Filters or Pages shelves. They can be created by double clicking an existing field or empty space at the shelf.

_images/calculate2.png

_images/calculate3.png

Note that ad-hoc calculations are not available when you create groups, sets, hierarchies, or parameters.

Logical Functions

CASE right(left([Startdate], 7), 2)
  WHEN '01' THEN 'Jan'
  WHEN '02' THEN 'Feb'
  WHEN '03' THEN 'Mar'
  WHEN '04' THEN 'Apr'
  WHEN '05' THEN 'May'
  WHEN '06' THEN 'Jun'
  WHEN '07' THEN 'Jul'
  WHEN '08' THEN 'Aug'
  WHEN '09' THEN 'Sep'
  WHEN '10' THEN 'Oct'
  WHEN '11' THEN 'Nov'
  WHEN '12' THEN 'Dec'
END

LOD

LOD expressions is an important Tableau field calculation that allows calculations to subsets of one or more dimensions. It is similar to SQL’s analytic function, an example being SELECT SUM(field_name) over() partition by fieldname2 from tablename. There are 3 types of LODs: Fixed, Include & Exclude. More in this link.

Below is an example. Red is the desired output. It should give a “Y” for types of Species, e.g., Cat, when just one or more contains a Disease marked by “1”. Original question from here.

alternate text
#-- This will give 1 if any species have at least a disease
{FIXED [Species]:MAX([Disease])}

#-- Change the 1 to 'Y' and 'N' for otherwise
IF {FIXED [Species]:MAX([Disease])} = 1 THEN 'Y' ELSE 'N' END

To force calculations over the entire table, you can avoid putting any dimensions in the LOD.

{FIXED : SUM([Row_Number])}

Others

ATTR

The attribute function ATTR() is a special function in Tableau. It basically returns the result an expression only if there is a single value; when there are multiple values, it returns an asterik (*). You will notice that the field will change from a dimension to measure upon using ATTR().

ATTR can be futher used as an IF ELSE to detect the presence of a single value, rather than a visual cue of (*):

IF COUNTD(fieldname) > 1
  then 'All Towns'
ELSE ATTR([TOWNSTATE])
END

Concat Strings

[StartDay] + '-' + [StartMonth]