You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: references/metrics.mdx
+31-13Lines changed: 31 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -7,47 +7,65 @@ A metric is a value that describes or summarizes features from a collection of d
7
7
8
8
In Lightdash, metrics are used to summarize dimensions or, sometimes, other metrics.
9
9
10
+
10
11
## Adding metrics to your project using the `meta` tag.
11
12
12
-
### 1. Using the column `meta` tag (Suggested)
13
+
14
+
### 1. Using the column `meta` tag
13
15
14
16
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.
15
17
16
18
```yaml
17
19
models:
18
-
- name: my_model
20
+
- name: orders_model
19
21
columns:
20
-
- name: user_id # dimension name of your metric
22
+
- name: user_id # dimension your metric is aggregating
21
23
meta:
22
24
metrics:
23
-
num_unique_user_ids: # name of your metric
25
+
distinct_user_ids: # name of your metric
24
26
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
27
32
```
28
33
34
+
35
+
29
36
Once you've got the hang of what these metrics look like, read more about the [metric types you can use below.](#metric-types)
30
37
38
+
31
39
### 2. Using the model `meta` tag
32
40
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:
34
42
35
43
```yaml
36
-
version: 2
37
-
38
44
models:
39
-
- name: my_model
45
+
- name: orders_model
40
46
meta:
41
47
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}
45
54
```
46
55
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
+
47
64
## Metric Categories
48
65
49
66
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:
50
67
68
+
51
69
### Aggregate metrics
52
70
53
71
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