> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bludia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Computed fields

> Computed fields calculate values from other fields and related records using arithmetic, string, logic, and aggregation operations (sum, avg, count, min, max). Values can be persisted or computed on read.

A **computed field** gets its value from a formula instead of user input. Use it for totals, prices with tax, full names, or counts of related records.

| Example | Formula |
| :- | :- |
| `total` on order items | `quantity × unit_price` |
| `price_with_tax` | `price × 1.08` |
| `full_name` | `concat(first_name, " ", last_name)` |
| `order_total` on orders | `sum(order_items.line_total)` |
| `open_tickets` on customers | `count(tickets)` |
| `is_overdue` | `if(due_date < today, true, false)` |

## Create a computed field

**1. Add a field**

Add the field that will hold the result, for example `order_total`.

**2. Make it computed**

Choose **Computed** and build the formula in the expression editor.

**3. Pick the result type**

`number`, `string`, `boolean`, `date`, or `json`.

**4. Choose null handling and persistence**

See the options below.

## Operations

> [tab] Arithmetic
>
> `add`, `subtract`, `multiply`, `divide`, `modulo`, `pow`, `abs`, `round`, `round_even`, `floor`, `ceil`
>
> Arithmetic uses exact decimals, so money stays accurate.

> [tab] Text
>
> `concat`, `upper`, `lower`, `trim`, `length`

> [tab] Logic
>
> `if`, `and`, `or`, `not`, `eq`, `neq`, `gt`, `lt`, `gte`, `lte`

> [tab] Dates
>
> `today`, `now`, `add_days`, `add_months`, `days_between`, `weeks_between`, `months_between`, `years_between`, `hours_between`, `minutes_between`
>
> The `*_between` operations take two dates and return whole units from the first to the second, negative when the second is earlier. For example, `years_between(birth_date, today)` is an age. Days, weeks, months and years ignore the time of day.

> [tab] Aggregation
>
> `sum`, `avg`, `count`, `min`, `max` over a related collection, for example the sum of `line_total` across an order's `order_items`.

## Expression format

Formulas are stored as an expression tree. The dashboard builds it for you; this is what it looks like:

```json theme={null}
{
  "op": "multiply",
  "args": [
    { "field_id": "12" },
    { "value": "1.08" }
  ]
}
```

| Node | Shape |
| :- | :- |
| Field reference | `{ "field_id": "12" }` |
| Literal | `{ "value": "1.08" }` |
| Operation | `{ "op": "add", "args": [ ... ] }` |
| Aggregation | `{ "op": "sum", "agg_collection": "order_items", "agg_field": "line_total" }` |

## Totals from related records

An invoice's `paid_amount` can be the sum of its payments' `amount`. Each payment links to its invoice with a relation field.

1. On **Invoices**, add a number field `paid_amount` and make it computed.
2. Choose the aggregation **Sum**, the collection **Payments**, and the field **amount**.
3. Turn **Persisted** on, so the total is stored and can be filtered and sorted.

```json theme={null}
{ "op": "sum", "agg_collection": "<payments id>", "agg_field": "<amount field id>", "agg_relation_id": <relation id> }
```

The total is updated in the same transaction as the payment change: when a payment is created, edited, moved to another invoice, trashed or restored. With the null strategy `zero`, an invoice without payments shows `0`.

## Options

| Option | Values | Meaning |
| :- | :- | :- |
| **Null strategy** | `propagate` | Any empty input makes the result empty (default) |
| | `zero` | Treat empty inputs as `0` |
| | `empty` | Treat empty inputs as an empty string |
| | `skip` | Ignore empty inputs |
| | `error` | Fail when an input is empty |
| **Persisted** | on / off | On: the value is stored and kept up to date when inputs change, so you can filter and sort on it. Off: the value is calculated when read. |

Bludia tracks dependencies between fields, so changing an input (including a related record) recalculates the values that depend on it.

> [!NOTE]
>
> Computed fields are read-only. Values sent by clients for them are rejected.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.