Skip to content

clarify metrics ref doc #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 31 additions & 13 deletions references/metrics.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,65 @@ A metric is a value that describes or summarizes features from a collection of d

In Lightdash, metrics are used to summarize dimensions or, sometimes, other metrics.


## Adding metrics to your project using the `meta` tag.

### 1. Using the column `meta` tag (Suggested)

### 1. Using the column `meta` tag

To add a metric to Lightdash using the `meta` tag, you define it in your dbt project under the dimension name you're trying to describe/summarize.

```yaml
models:
- name: my_model
- name: orders_model
columns:
- name: user_id # dimension name of your metric
- name: user_id # dimension your metric is aggregating
meta:
metrics:
num_unique_user_ids: # name of your metric
distinct_user_ids: # name of your metric
type: count_distinct # metric type
num_user_ids:
type: count
- name: revenue # dimension your metric is aggregating
meta:
metrics:
sum_revenue: # name of your metric
type: sum # metric type
```



Once you've got the hang of what these metrics look like, read more about the [metric types you can use below.](#metric-types)


### 2. Using the model `meta` tag

Sometimes a metric references many columns, in these cases you can define the metric at the model level:
Sometimes a metric references multiple columns, in these cases you can define the metric at the model level:

```yaml
version: 2

models:
- name: my_model
- name: orders_model
meta:
metrics:
num_unique_user_ids:
type: count_distinct
sql: ${TABLE}.user_id
revenue_per_user:
type: number
sql: ${sum_revenue} / ${distinct_user_ids}
sum_total_paid:
type: sum
sql: ${revenue} + ${taxes_paid}
```

<Warning>
**Non-aggregate metrics** (`number`, `boolean`, etc.) can only reference other metrics in `sql:` since they are inserted directly into the generated SQL query without being wrapped in an aggregate function.

**Aggregate metrics** (`sum`, `count_distinct`, etc.) can only reference dimensions since they are wrapped in an aggregate function before being added to the generated SQL query. Wrapping an aggreate function in another aggregate function will cause an error.
</Warning>

Read on to learn more about aggregate vs non-aggregate metrics!

## Metric Categories

Each metric type falls into one of these categories. The metric categories tell you whether the metric type is an aggregation and what type of fields the metric can reference:


### Aggregate metrics

Aggregate metric types perform (surprise, surprise) aggregations. Sums and averages are examples of aggregate metrics: they are measurements summarizing a collection of data points.
Expand Down