Skip to content

Commit b29f56e

Browse files
authored
clarify metrics ref doc (#51)
* clarify metrics ref doc * clarify aggreate vs non aggregate
1 parent 2b0bf78 commit b29f56e

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

references/metrics.mdx

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,65 @@ A metric is a value that describes or summarizes features from a collection of d
77

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

10+
1011
## Adding metrics to your project using the `meta` tag.
1112

12-
### 1. Using the column `meta` tag (Suggested)
13+
14+
### 1. Using the column `meta` tag
1315

1416
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.
1517

1618
```yaml
1719
models:
18-
- name: my_model
20+
- name: orders_model
1921
columns:
20-
- name: user_id # dimension name of your metric
22+
- name: user_id # dimension your metric is aggregating
2123
meta:
2224
metrics:
23-
num_unique_user_ids: # name of your metric
25+
distinct_user_ids: # name of your metric
2426
type: count_distinct # metric type
25-
num_user_ids:
26-
type: count
27+
- name: revenue # dimension your metric is aggregating
28+
meta:
29+
metrics:
30+
sum_revenue: # name of your metric
31+
type: sum # metric type
2732
```
2833
34+
35+
2936
Once you've got the hang of what these metrics look like, read more about the [metric types you can use below.](#metric-types)
3037
38+
3139
### 2. Using the model `meta` tag
3240

33-
Sometimes a metric references many columns, in these cases you can define the metric at the model level:
41+
Sometimes a metric references multiple columns, in these cases you can define the metric at the model level:
3442

3543
```yaml
36-
version: 2
37-
3844
models:
39-
- name: my_model
45+
- name: orders_model
4046
meta:
4147
metrics:
42-
num_unique_user_ids:
43-
type: count_distinct
44-
sql: ${TABLE}.user_id
48+
revenue_per_user:
49+
type: number
50+
sql: ${sum_revenue} / ${distinct_user_ids}
51+
sum_total_paid:
52+
type: sum
53+
sql: ${revenue} + ${taxes_paid}
4554
```
4655

56+
<Warning>
57+
**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.
58+
59+
**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.
60+
</Warning>
61+
62+
Read on to learn more about aggregate vs non-aggregate metrics!
63+
4764
## Metric Categories
4865

4966
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:
5067

68+
5169
### Aggregate metrics
5270

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

0 commit comments

Comments
 (0)