# Derived fields

> Columns worked out from other columns — same-row arithmetic, totals from a child table, and roll-ups through a hierarchy.

Section: Recipes · Type: concept · Last updated: 2026-09-13

Three keys compute a column instead of drawing it. derive is arithmetic over the same row — with comparisons, if/else and and / or / not — (functions: abs, add_months, ceil, date, day, floor, max, min, mod, month, noise, round, weekday, year) or, for identifiers that encode the row, a string grammar (alnum, concat, consonants, digits, lower, pad, part, slice, upper, vowels). On a target typed date or a number, a date is a whole number of days, so a due date or a length of stay is one formula. aggregate totals a child table (avg, count, max, mean, min, sum) and rollup totals up a tree or graph. The receipt re-checks every formula, so a derived column that disagrees with its inputs is reported.

## The picture

Inputs (other columns) → derive · aggregate · rollup (computed) → Receipt (formulas re-checked)

Computed after the inputs exist; chains settle deepest-first.

## The three keys

- derive (e.g. premium * 0.1) — Worked out from other columns on the same row. Arithmetic over this row’s own columns. Order does not matter — the engine settles the formulas until they stop changing.
- aggregate (e.g. sum(LineItem.amount)) — Totalled from a child table — sum, count, average, min, max. Filled in after every table exists. Chains settle deepest-first, so a total of totals works.
- rollup (e.g. sum(cost)) — Totalled up a hierarchy — itself plus everything beneath it. Needs the table to declare a tree or a graph.

## Numeric functions

- abs, add_months, ceil, date, day, floor, max, min, mod, month, noise, round, weekday, year

## Operators

- + - * / == != < <= > >= ? : and or not && || ! . — and binds tighter than or; a comparison or a logical operator gives 1 or 0.

## Dates

- Inside a formula a date is a whole number of days — only when the target declares a type.
- type date: the answer is written as a date — issued_on + 30, add_months(opened_on, 12).
- a number type: the answer is a number — discharged_on - admitted_on is the nights between them.
- year, month, day and weekday (ISO, Monday 1) read a date; date(y, m, d) builds one and refuses an impossible date.

## String functions

- alnum, concat, consonants, digits, lower, pad, part, slice, upper, vowels

## Aggregate functions

- avg, count, max, mean, min, sum

## One of each

```json
{
  "fields": [
    {
      "name": "premium",
      "type": "finance.amount",
      "min": 100,
      "max": 5000
    },
    {
      "name": "gst",
      "type": "float",
      "derive": "round(premium * 0.1, 2)"
    },
    {
      "name": "issued_on",
      "type": "date",
      "min": 2026,
      "max": 2026
    },
    {
      "name": "due_on",
      "type": "date",
      "derive": "issued_on + 30"
    },
    {
      "name": "high_value",
      "type": "int",
      "derive": "premium > 4000 and weekday(issued_on) <= 5 ? 1 : 0"
    },
    {
      "name": "total_claims",
      "type": "float",
      "aggregate": "sum(Claim.amount)"
    },
    {
      "name": "dept_cost",
      "type": "float",
      "rollup": "sum(cost)"
    }
  ]
}
```

## Common questions

**Does the order of derived fields matter?**

No. The engine settles same-row formulas until they stop changing, and fills aggregates after every table exists, deepest first — so a total of totals works.

**Can a derive work out a date or a duration?**

Yes, when the target declares its type. Inside a formula a date is a whole number of days: a field typed date with derive "issued_on + 30" is a due date, and a field typed int with "discharged_on - admitted_on" is a length of stay. year, month, day and weekday read a date (weekday is ISO: Monday 1); add_months(d, n) moves by calendar months and clamps to the end of a shorter month; date(y, m, d) builds one. An untyped target never reads a date as a number, so a bare derive: "start_date" still copies the text.

**Can derive build a text identifier?**

Yes. When the expression is not arithmetic the engine reads it as the string grammar: bare words are fields, single quotes are literals, and . concatenates — e.g. upper(slice(surname, 0, 3)) . '-' . digits(dob). Functions: alnum, concat, consonants, digits, lower, pad, part, slice, upper, vowels.

## Limitations

- Date arithmetic needs a typed target: an untyped or datetime field never reads a date as a number, so declare type date for a date answer or a number type for a count of days.
- The string grammar has no arithmetic and no + — the dot concatenates, and an expression is read as arithmetic first, so the two never compete.
- A string derive that references a field the row does not carry yields nothing, never an empty string — a silently wrong identifier is worse than a blank one.

## Related

- [Fields & types](https://theworklens.com/docs/recipes/fields): Every field key a recipe accepts, the 20 value types the engine generates, and how a field declares a checksum-valid identifier.
- [Relationships](https://theworklens.com/docs/recipes/relationships): How tables point at each other — ref, scope and fanout on a field, and the relationship kinds a recipe can declare.
- [Receipts](https://theworklens.com/docs/proof/receipts): What a run’s proof report contains, field by field, with a real one — and the line between what it proves and what it does not.

_← Previous: [Rules & constraints](https://theworklens.com/docs/recipes/rules) · Next: [Scenarios](https://theworklens.com/docs/recipes/scenarios) →_

_Keys from the studio’s reference tab; function names from the engine vocabulary generated out of generate.rs. Source: recipe-rules.ts + recipe-vocab.json._

_Canonical: https://theworklens.com/docs/recipes/derived-fields · This copy: https://theworklens.com/docs/recipes/derived-fields.md_
