Tableau Calculated Fields

Most dashboards need a number or label that the data source does not store, such as a profit ratio, revenue after shipping, or a tag that sorts orders into size bands. A calculated field is a new field you define with a formula built from existing fields, functions and operators. Tableau saves the formula with the workbook's data source and lists the result in the Data pane beside the original fields. The underlying table is not changed. You need a calculated field as soon as a business question cannot be answered by dragging an existing field onto the view.

Building a calculated field

Choose Analysis > Create Calculated Field, or right-click a field in the Data pane and choose Create > Calculated Field. The editor has a name box and a formula box. Field names go in square brackets, text values go in double quotes, and anything after // is a comment.


Plain text
// Revenue left after paying for shipping
[Sales] - [Shipping Cost]


Below the formula, the editor reports either that the calculation is valid or which part contains an error. Tableau infers the result's data type (number, string, date or Boolean) from the formula. Numeric results normally appear with the measures. String and Boolean results appear with the dimensions. Because the field is a formula and not stored values, it recomputes automatically whenever the data refreshes or the view changes.

Row-level and aggregate calculations

The most consequential choice in a formula is when Tableau evaluates it.

  • A row-level calculation uses unaggregated fields, such as [Profit] / [Sales]. Tableau computes it once for every row of the data source and then aggregates the results like any other measure, using SUM by default.
  • An aggregate calculation wraps fields in aggregation functions, such as SUM([Profit]) / SUM([Sales]). Tableau first aggregates each field to the marks in the view and then applies the formula to those totals.

For sums and differences the two approaches agree when there are no nulls: summing [Sales] - [Shipping Cost] row by row gives the same total as subtracting the two sums. For ratios they do not. Consider two orders:


Order

Sales ($)

Profit ($)

Row-level [Profit] / [Sales]

A

200

40

0.20

B

800

40

0.05


With its default SUM, the row-level field shows 0.20+0.05=0.250.20 + 0.05 = 0.25, or 25%, which is meaningless as a margin. Switching to AVG gives 12.5%, which gives the small order the same weight as the large one. The aggregate formula gives the true combined margin:


SUM(Profit)SUM(Sales)=40+40200+800=801000=0.08=8%\frac{\text{SUM(Profit)}}{\text{SUM(Sales)}} = \frac{40 + 40}{200 + 800} = \frac{80}{1000} = 0.08 = 8\%


Two flow diagrams compare dividing profit by sales on each row before aggregating with totalling profit and sales before dividing.


Feature

Row-level

Aggregate

Example

[Profit] / [Sales]

SUM([Profit]) / SUM([Sales])

Evaluated

Once per data row

Once per mark in the view

Result changes when view dimensions change

Only through the later aggregation

Yes, the totals are recomputed

Good for

Per-row arithmetic, row labels and flags

Ratios, rates and conditions on totals



Logic, nulls and data types

Conditional logic uses IF, which must close with END:


Plain text
IF [Sales] >= 1000 THEN "Large"
ELSEIF [Sales] >= 250 THEN "Medium"
ELSE "Small"
END


CASE [Region] WHEN "West" THEN ... END is a compact choice when you test one field against fixed values, and IIF(test, value_if_true, value_if_false) handles a single test in one line. An IF without ELSE returns null for rows that fail every test. That is useful inside an aggregation, because SUM and AVG ignore nulls.


Nulls also spread through arithmetic: if [Shipping Cost] is null on a row, [Sales] - [Shipping Cost] is null on that row. When a null genuinely means zero, wrap the field that may be null in ZN(), which returns zero for null: [Sales] - ZN([Shipping Cost]).


Operators need compatible types. The + operator joins strings, so a label such as "Qty: " + STR([Quantity]) converts the number with STR() first. INT(), FLOAT() and DATE() convert in the other directions.



What to remember

  • A calculated field is a formula stored with the workbook. It adds a field and leaves the source data unchanged.
  • Row-level formulas run on every data row and are aggregated afterwards. Aggregate formulas run on the totals for each mark.
  • Build ratios as aggregates, such as SUM([Profit]) / SUM([Sales]).
  • Never mix aggregated and unaggregated fields in one formula.
  • Close every IF with END, and use ZN() where a null should count as zero.

Example: Profit ratio, order flags and a regional total

An online retailer's Orders table has one row per order. An analyst is building a Tableau summary from these three rows:


Order ID

Region

Sales ($)

Profit ($)

1001

West

100

50

1002

East

400

20

1003

West

500

30


a) The analyst creates [Ratio Row] with the formula [Profit] / [Sales] and shows it for all three orders combined. What value appears with the default SUM aggregation, and what appears with AVG? Write a calculated field that gives the correct overall profit ratio, and compute it.


[Ratio Row] is a row-level calculation, so Tableau divides on each row first:


1001: 50/100=0.501002: 20/400=0.051003: 30/500=0.06\begin{aligned} 1001&: \ 50 / 100 = 0.50 \\ 1002&: \ 20 / 400 = 0.05 \\ 1003&: \ 30 / 500 = 0.06 \end{aligned}


With SUM the view shows 0.50+0.05+0.06=0.610.50 + 0.05 + 0.06 = 0.61, or 61%. With AVG it shows 0.61/3=0.2030.61 / 3 = 0.203, or 20.3%. Neither value is the business's profit ratio. The average gives the $100 order the same weight as the $500 order.


The correct field is an aggregate calculation:


Plain text
SUM([Profit]) / SUM([Sales])


Tableau totals each field for the mark first and then divides:


50+20+30100+400+500=1001000=0.10=10%\frac{50 + 20 + 30}{100 + 400 + 500} = \frac{100}{1000} = 0.10 = 10\%


The overall profit ratio is 10%.


b) Create a field [Order Size] that labels an order "Large" when its sales are at least $300 and "Standard" otherwise. Then write a measure that counts large orders, and state its value.


The label describes each order, so it is row-level:


Plain text
IF [Sales] >= 300 THEN "Large" ELSE "Standard" END


It returns a string, so Tableau lists it with the dimensions. Order 1001 ($100) is ________, and orders 1002 ($400) and 1003 ($500) are ________.


To count large orders, convert the test to 1 or 0 on each row and sum the results:


Plain text
SUM(IIF([Sales] >= 300, 1, 0))


The row values are ________, so the measure shows 0+1+1=20 + 1 + 1 = 2 large orders.


c) For West sales, the analyst writes IF [Region] = "West" THEN SUM([Sales]) END. Tableau reports Cannot mix aggregate and non-aggregate arguments with this function. Explain the error, fix the formula, and compute West sales.


[Region] = "West" is tested row by row, but SUM([Sales]) is a total over many rows. Tableau cannot evaluate a single formula at both levels.


Put the row-level test inside the aggregation:


Plain text
SUM(IF [Region] = "West" THEN [Sales] END)


The IF has no ELSE, so it returns the sales for West rows and null for the rest: ________. SUM ignores nulls:


100+500=600100 + 500 = 600


West sales are $600.

An analyst connects Tableau to a company's sales database and creates a calculated field named Net Sales with the formula [Sales] - [Discount Amount]. Which statement describes the result?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A retailer is preparing customer names and product codes for analysis in Tableau. Match each function to its behavior when used in a calculated field with non-null text inputs.

Match each term
A.

Counts the characters in a text value, including spaces.

B.

Removes spaces from the beginning and end of a text value.

C.

Extracts a specified number of characters from the beginning of a text value.

D.

Converts the letters in a text value to capitals.

LEFT

UPPER

LEN

TRIM

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A view shows one mark for the North region. The data source contains two North orders:


Order

Sales ($)

Profit ($)

N1

250

25

N2

750

150


The analyst created a calculated field [Margin] with the formula [Profit] / [Sales]. They dragged it to the view with its default aggregation, SUM, and formatted it as a percentage with one decimal place. What value does the North mark display?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice