All articles

Tables & data

Calculate values with formulas

Not every number you print is stored in your data. A line total is quantity times unit price; tax is the subtotal times a rate. A formula works the number out at every render from the record being rendered, so it never goes stale like a typed number. A total does the same for a whole list: its sum, average, count, smallest or largest value.

Before you start

  • In the hosted app, changing a template needs the editor role or higher (roles run viewer, editor, admin, owner). Local mode is single-user and has no roles.
  • Load sample or live data to see results on the canvas.
  • While you are reviewing changes Buddy proposed, the properties panel is paused until you accept or discard them.

Add a formula

A formula is a run — one piece of an element's text, beside fixed words and fields — so it can go in a heading's or paragraph's Content, a list item, a column's Cell content or Footer / total, a group header or subtotal, or a sub-row cell.

  1. Select the element and open the properties panel with the button at the far right of the app bar (tooltip Show properties panel). ⌘. (Ctrl+. on Windows) works too.
  2. Under the runs, press + Formula. A chip marked = appears, holding a field × 1.
  3. Press the chip's ⋯. The formula shows as a small tree: a Calculation with an operation menu, and its two parts beneath it.
  4. Choose the operation: +, −, × or ÷.
  5. For each part, choose Field, Fixed value or Calculation from its menu. The menu also lists Comparison, And / Or and Not, which belong in conditions.
  6. For a Field part, type the path, such as item.Quantity in a table cell or record.Subtotal elsewhere; paths from your data are suggested as you type. In the hosted app, with a data source, you pick the field from the source's list instead, as for a field.
  7. For a Fixed value part, set its type to Number and type the number, such as 0.19.
  8. Choose how the result prints in the format menu (tooltip Format).

The chip now reads like = item.Quantity × item.UnitPrice, and the canvas shows the result. In most places, a field part that does not resolve in the loaded data gets a warning to check for a typo, with a Use … button when Weft finds a close match.

A formula run's options. The chip reads = item.Quantity × item.UnitPrice. Below it, a Calculation set to × holds two Field parts, item.Quantity and item.UnitPrice; the Format menu is set to Currency, and Bold is switched on.

Longer calculations

A part can be another Calculation, which starts as a field × 1. Subtotal plus 19% tax is a + of the field record.Subtotal and a Calculation of record.Subtotal × 0.19. The chip shows nested parts in brackets — = record.Subtotal + (record.Subtotal × 0.19) — and the tree, not operator precedence, sets the order of working.

In a table row, index is the row's position from 0, so index + 1 numbers the rows 1, 2, 3. If the table has a Binding name such as line, use lineIndex.

Add a total over a list

+ Total replaces the page-number buttons in two places: a table column's Footer / total, and a grouping level's Subtotal row (see Group table rows).

  1. Select the table, open the column's card with ▸, and open Footer / total.
  2. Press + Total. A chip marked Σ appears: a sum of the field this column shows, formatted as currency.
  3. Open the chip's ⋯ to change the operation (sum, avg, count, min or max), Format, Collection (the list to go through) or Value path (the field taken from each item, hidden for count).

A sum, average, minimum or maximum uses only items whose value is a number (numbers stored as text count; blanks and other text are skipped), and prints empty when none has one. A count is the number of items in the list, whatever their values; it keeps the currency format it started with, so switch it to Number.

A total adds up one field per item, and a formula cannot use a total as a part. To total quantity × unit price, that amount needs to be a field in your data; Buddy can build a total over a calculation when it generates or edits a template, but the panel cannot.

Use a calculation in a condition

Conditions use the same tree. To show something only when quantity × unit price is over 100 (inside a repeat, or in a table column's condition, where item is the current row):

  1. Select the element and, in its Conditional section, tick Show only when a condition is met. For a table column, open the column's Conditional formatting instead and tick Show cell only when a condition is met. A Comparison appears with = and two parts.
  2. Change = to >.
  3. Switch the first part to Calculation, set its Field to item.Quantity, keep ×, and switch its second part to a Field with item.UnitPrice. When the template reads from a data source, a condition's Field parts can be a searchable list of fields instead: pick the two fields from it.
  4. Change the comparison's second part, a Fixed value, to type Number and enter 100.

The line above the tree reads Shown when Quantity times Unit Price is greater than 100. The same tree is in a styling rule's When this holds…, a column's Show cell only when a condition is met, and a Conditional element's Condition. A calculation that produces nothing is never greater or less than anything, so a > or < condition on it is not met.

Format the result

A formula has the same format menu as a field. Its Default prints a number in the document's locale, such as 1,234.5. Currency uses the document's currency, unless a template Buddy made sets one per table row (the Currency boxes are on field runs only), and Percent prints 0.19 as 19%. See Format values.

What prints when values are missing

A missing value, a blank, or a value that is not a number, such as "N/A", makes the whole formula print nothing, and so does dividing by zero. Numbers stored as text, such as "12.5", are used as numbers. The panel has no control for a placeholder in place of an empty result.

Good to know

  • A formula in a footer, group header or subtotal is worked out once, not per row, so row fields there come out empty. In a footer or subtotal, use a total instead.
  • A formula with a field that leads nowhere is flagged This formula won't resolve on the canvas and in the check before export.

What next