# Calculated fields

> Work out one figure per contact from their events or records, like orders in the last 90 days or total spent, and use it in segments, messages and journeys.

URL: https://docs.lemuai.com/segments/calculated-fields
For: Everyone
Checked against LemuAI app version 1.0.12
Related: [Segments](https://docs.lemuai.com/segments/overview), [Predictions](https://docs.lemuai.com/segments/predictions), [Custom Records](https://docs.lemuai.com/records/overview), [Standard and Conversion Events](https://docs.lemuai.com/website-pixel/standard-events)

A **calculated field** is a number or date LemuAI works out for every contact, for example "orders in the last 60 days", "total spent", "average order value" or "last order date". It's stored on the contact like any other field, so you can use it in:

- segment rules (**Contact property** → the field, marked "(calculated)"),
- message templates (`{{ contact.metrics.total_spent_60d }}`),
- journeys and the contact page.

They're under **Audience → Segments → Calculated fields**.

## Create one

**Step: Name and key**

**Name** (like `Total spent in the last 60 days`) and **Key** (like `total_spent_60d`). The key is fixed once created, because templates use it.

**Step: Choose what to calculate from**

**From events** (a contact's events, like `Purchase`) or **From records** (a record type, like orders or loans).

**Step: Choose the calculation**

| From events | From records |
|---|---|
| Number of times | Number of records |
| Total of a property | Total of a field |
| Average of a property | Average of a field |
| Smallest value of a property | Smallest value of a field |
| Largest value of a property | Largest value of a field |
| Number of different values of a property | Number of different values of a field |
| Date of the first time | |
| Date of the most recent time | |

Pick the event (or record type), the property or field (for events, type a property name like `value`, `order.total` or `items.quantity`), and a time window if you want one.

**Step: Preview and create**

**Preview** shows sample values. **Create field**: values are calculated in a few minutes.

## How values update

- Recalculated every hour, when you save the field, and when you press **Recalculate now**.
- Only contacts with something to show get a value. A count or total of 0 is left empty; segments treat an empty count or total as 0.
- Deleting a field removes its values from every contact; messages that use it show nothing in its place.

## Examples

| Name | From | Calculation |
|---|---|---|
| Orders in the last 90 days | Events: `Purchase` | Number of times, last 90 days |
| Total spent | Events: `Purchase` | Total of a property: `value` |
| Last order date | Events: `Purchase` | Date of the most recent time |
| Loans outstanding | Records: `loans` (filter status is active) | Number of records |
| Different categories bought | Events: `Purchase` | Number of different values of a property: `items.category` |

Then a segment "Total spent is at least 10000 and Orders in the last 90 days is 0" finds big spenders who stopped buying.
