# Calculated columns in the Data explorer

A calculated column is a column in a [Data explorer report](https://breww.com/docs/aggregate-and-raw-data-explorer-reports/) that Breww works out with a formula, much like a formula in a spreadsheet. Use one for a figure the data doesn't hold directly, such as the price per unit, a margin percentage or the days between two dates. A calculated column gives the same figure in the table, pivots, charts, dashboard widgets and exports.

## Adding a calculated column

1. In **Columns and filters**, select **Add a calculated column** beneath the columns.
2. Under **Start from**, keep **A new formula**, or choose a built-in column or a calculated column from another report on the same data to start from a copy of it. Your copy doesn't change if the original does.
3. Enter a **Name**.
4. Write the **Formula**. Use **Insert into the formula** on the right to find fields, other calculated columns and functions, and click one to add it where your cursor is. Type a field's name and the editor suggests matches.
5. Choose how to **Show as**: **Number**, **Money**, **Percentage**, **Volume** or **Weight**.
6. Select **Save column**.

A report can have up to 10 calculated columns. To change one later, select the pen button on its row.

![The Add a calculated column window, with a formula, a note that it's worked out on each row and a warning about totals](https://storage.googleapis.com/breww-user-uploads/markdown-embedded/ed7e230da63a5573dc81a53e41b5bc8a8c720eb30bedadfc61219aeebc7672b8.png)

## Worked out on each row, or from totals

As you type, the editor tells you how the column will be worked out:

- **Worked out on each row.** Every row gets its own figure, for example `(value_in_base_currency - duty_cost) / quantity`. You can filter, sort and group by these columns.
- **Worked out from totals.** Breww adds up the rows first, then works out the formula once for each group and for the total. For example, `SUM(total_amount_in_base_currency) / SUM(quantity)` gives the right average price for each product and for the grand total. These columns can't be filtered or grouped by.

Some row formulas give the right figure on each row but the wrong one when the rows are added up or averaged, as with a price or a percentage. For these, Breww doesn't offer Sum or Average, and the editor explains why. Select **Work it out from totals instead** and Breww rewrites the formula so its totals are right.

## Writing a formula

Use field names as they're shown in **Insert into the formula**, such as `quantity` or `value_in_base_currency`. To use a field on a related record, go through the relation, such as `product.name`. You can only use fields the report's data offers you.

Combine them with `+`, `-`, `*`, `/` and brackets, and functions such as these:

| Function                          | What it does                                                   |
| --------------------------------- | -------------------------------------------------------------- |
| `SUM(value)`                      | Adds up a value across all the rows                            |
| `AVG(value)`                      | The average of a value across all the rows, leaving out blanks |
| `IF(condition, value, otherwise)` | One value when the condition is met, and another when it isn't |
| `IFBLANK(value, instead)`         | Uses something else in place of a blank                        |
| `ROUND(number, places)`           | Rounds a number to this many decimal places                    |
| `DAYS_BETWEEN(from, to)`          | How many days there are from one date to another               |

The **Functions** tab under **Insert into the formula** lists every function you can use, with what each one does. A formula that uses a function which adds up rows, such as `SUM`, `AVG`, `COUNT`, `MIN` or `MAX`, makes a column that's worked out from totals.

### Blanks

A blank stays blank. If any part of a row's formula is blank, such as a product with no cost, that row's result is blank too, and totals leave it out. To treat a blank as zero, wrap it in `IFBLANK`, for example `IFBLANK(duty_cost, 0)`.

## Built-in calculated columns

Many datasets come with built-in calculated columns, listed with a **Built-in** badge at the end of **Add a column**, each with a note on how it's worked out. Examples include **Margin**, **Margin %**, **Total cost**, **Value less duty**, **Discount given**, **Discount %** and **Average price per unit** on products sold, plus **Average order value**, **Days overdue**, **Days in production**, **Loss %** and **Age band** on other datasets.

Built-in columns can't be changed. To adjust one, choose it under **Start from** in **Add a calculated column** and change your copy.

## Where calculated columns go

A calculated column is saved as part of its report, so it goes wherever the report goes: share codes, copies, scheduled emails, dashboard widgets and the [Breww public API](https://breww.com/docs/breww-public-api/). The [AI report builder](https://breww.com/docs/ai-report-builder/) can add and change calculated columns for you too.

If a column a formula relies on is removed from the table, the report tells you which formula still uses it. If a formula can no longer be worked out, for example because a custom field it uses was deleted, the report won't run until you edit or remove it.
