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

> ## Agent Instructions
> To set up Hatchmark for a user, follow https://hatchmark.dev/docs/get-started/agent-setup.md. Inside a Hatchmark project, prefer `hatchmark docs`: it prints the docs of the installed version.

# Define metrics

> Define an amount, a count and a rate as metrics, check their values with hatchmark query, and pin them with a test.

You will end with three metrics on a refunds source: an amount, a count and a rate, each checked
against the numbers it should give and pinned by a test. The steps assume the `refunds` source from
[Connect your data](/docs/guides/connect-your-data.md), with `refunded_at` and `amount` columns.

## 1. Sum a column

Every file under `metrics/` is one metric, named after the file. Create
`metrics/refunded_amount.yaml`:

```yaml theme={null}
label: Refunded amount
description: Amounts refunded to customers, in USD.
type: aggregate
table: refunds
aggregate: sum
column: amount
time: refunded_at
format:
  type: currency
  compact: true
better: down
```

* `table` names the source and `column` what to add up.
* `time` is the date that places a row in a period. Set it on almost every metric.
* `better: down` colours a rise red on every KPI.

## 2. Count rows

`metrics/refunds.yaml` counts rows, so it needs no `column`:

```yaml theme={null}
label: Refunds
description: Number of refunds.
type: aggregate
table: refunds
aggregate: count
time: refunded_at
format:
  type: number
better: down
```

`filters` keeps only some rows, such as `status: paid` in the sample `orders` metric. See
[filters](/docs/reference/metrics.md#filters).

## 3. Divide two metrics

A rate is a `ratio` of two metrics, here the refunds over the sample project's `orders`. Create
`metrics/refund_rate.yaml`:

```yaml theme={null}
label: Refund rate
description: Refunds as a share of paid orders.
type: ratio
numerator: refunds
denominator: orders
format:
  type: percent
  decimals: 1
better: down
```

A ratio divides the totals for each period and group, so a quarter's rate is that quarter's refunds
over its orders, never an average of monthly rates.

## 4. Check the numbers

```sh theme={null}
hatchmark check
hatchmark query refund_rate refunded_amount --range last_complete_quarter --compare previous_period
```

```text theme={null}
Refund rate: 14.0% for Q3 2026, +1.71 pp vs 12.3% for Q2 2026.
Refunded amount: $12.6K for Q3 2026, +28.5% vs $9.77K for Q2 2026.
```

`query` computes values exactly as a dashboard card does. Compare one against a number you trust,
such as a total from `hatchmark data query`. `hatchmark explain refund_rate` prints the SQL behind it.

## 5. Pin a value

Add a test with the value you checked, so `hatchmark test` fails when a change to the data or the
definition moves it:

```yaml theme={null}
tests:
  - name: Q3 2026
    window: [2026-07-01, 2026-09-30]
    expect: 12554.25
```

```sh theme={null}
hatchmark test refunded_amount
```

[Tests](/docs/reference/metrics.md#tests) also pin values per group and against fixture files.

## Next

* [Build a dashboard](/docs/guides/build-a-dashboard.md) with the new metrics.
* [Metrics](/docs/reference/metrics.md) lists every metric type: `sql`, `formula`, `per_entity` and
  point-in-time metrics over snapshots.


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