Skip to content

Commit f6827a7

Browse files
Add time interval reference examples to dimensions and metrics documentation (#168)
* Update references/dimensions.mdx * Update references/dimensions.mdx * Update references/metrics.mdx * Update references/metrics.mdx * Update references/metrics.mdx * Update references/dimensions.mdx * Update references/metrics.mdx * Update references/metrics.mdx --------- Co-authored-by: mintlify[bot] <109931778+mintlify[bot]@users.noreply.github.com>
1 parent 6e168b6 commit f6827a7

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

references/dimensions.mdx

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,42 @@ You can set custom versions of time intervals by using additional dimensions and
415415
groups: ["Delivery Date"]
416416
```
417417
418+
### Reference time intervals in other dimensions
419+
420+
You can reference specific time intervals of a dimension in other dimensions. When you define time intervals for a dimension (like `session_start`), Lightdash creates separate dimensions for each interval (e.g., `session_start_day`, `session_start_month`). You can reference these in custom SQL for other dimensions.
421+
422+
For example, if you have a `user_created_at` dimension with time intervals defined, you can calculate the duration between two dates using the `DAY` interval:
423+
424+
```yaml
425+
- name: user_created_at
426+
meta:
427+
dimension:
428+
type: timestamp
429+
time_intervals:
430+
- DAY_OF_WEEK_NAME
431+
- WEEK
432+
- MONTH
433+
- RAW
434+
- DAY
435+
- HOUR_OF_DAY_NUM
436+
- QUARTER
437+
- name: first_purchase_at
438+
meta:
439+
dimension:
440+
type: timestamp
441+
time_intervals:
442+
- DAY
443+
- MONTH
444+
- QUARTER
445+
- name: duration
446+
meta:
447+
dimension:
448+
type: number
449+
sql: EXTRACT(DAY FROM ${first_purchase_at_day} - ${user_created_at_day})
450+
```
451+
452+
In this example, `${user_created_at_day}` and `${first_purchase_at_day}` reference the `DAY` time interval versions of the `user_created_at` and `first_purchase_at` dimensions.
453+
418454
## Groups
419455

420456
You can group your dimensions and metrics in the sidebar using the `groups` parameter.
@@ -948,3 +984,34 @@ models:
948984
type: max
949985
sql: ${version}
950986
```
987+
988+
### Referencing time intervals in additional dimensions
989+
990+
You can reference time interval dimensions within additional dimensions. This is useful for creating custom logic based on specific time intervals.
991+
992+
For example, you can create a tier based on whether a specific time interval has a value:
993+
994+
```yaml
995+
- name: session_start
996+
meta:
997+
dimension:
998+
type: timestamp
999+
time_intervals:
1000+
- DAY_OF_WEEK_NAME
1001+
- WEEK
1002+
- MONTH
1003+
- RAW
1004+
- DAY
1005+
- HOUR_OF_DAY_NUM
1006+
- QUARTER
1007+
additional_dimensions:
1008+
extra_session_start_tier:
1009+
type: string
1010+
sql: |
1011+
CASE
1012+
WHEN ${session_start_month} IS NOT NULL THEN 'month'
1013+
ELSE 'else'
1014+
END
1015+
```
1016+
1017+
In this example, the additional dimension `extra_session_start_tier` references `${session_start_month}`, which is the `MONTH` time interval of the `session_start` dimension.

references/metrics.mdx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,18 @@ metrics:
639639
sql: 'IF(${subscriptions.is_active}, ${user_id}, NULL)'
640640
```
641641

642+
### Referencing time intervals in custom SQL
643+
644+
You can reference specific time intervals of dimensions in custom SQL for aggregate metrics. When a dimension has time intervals defined, you can reference the specific interval by appending the interval name to the dimension name.
645+
646+
```yaml
647+
max_month:
648+
type: max
649+
sql: ${session_start_month}
650+
```
651+
652+
In this example, the metric references `${session_start_month}`, which is the `MONTH` time interval of the `session_start` dimension. This allows you to perform aggregations on specific time interval versions of your dimensions.
653+
642654
## Using custom SQL in non-aggregate metrics
643655

644656
In non-aggregate metrics, you can reference any other metric from the given model and any joined models. You **can’t reference other dimensions.**
@@ -693,6 +705,20 @@ The list of fields must be made of dimension names (or metrics if you'd like to
693705

694706
The order that the fields are listed in `show_underlying_values` is the order that they'll appear in on the `view underlying data` table.
695707

708+
### Referencing time intervals in show underlying values
709+
710+
You can reference specific time intervals of dimensions in the `show_underlying_values` list. When a dimension has time intervals defined, you can reference the specific interval by appending the interval name to the dimension name (e.g., `session_start_month`, `session_end_quarter`).
711+
712+
```yaml
713+
count:
714+
type: count
715+
show_underlying_values:
716+
- session_start_month
717+
- session_end_quarter
718+
```
719+
720+
In this example, `session_start_month` and `session_end_quarter` reference the `MONTH` and `QUARTER` time intervals of the `session_start` and `session_end` dimensions respectively.
721+
696722
## Groups
697723

698724
You can group your dimensions and metrics in the sidebar using the `groups` parameter.
@@ -854,6 +880,19 @@ models:
854880
- web_sessions.is_bot_user: false
855881
```
856882

883+
### Referencing time intervals in filters
884+
885+
You can reference specific time intervals of dimensions in metric filters. When a dimension has time intervals defined, you can filter on the specific interval by appending the interval name to the dimension name (e.g., `session_start_day`).
886+
887+
```yaml
888+
count:
889+
type: count
890+
filters:
891+
- session_start_day: "!null"
892+
```
893+
894+
In this example, the filter checks that `session_start_day` (the `DAY` time interval of the `session_start` dimension) is not null.
895+
857896
### Metric filters cannot be used with non-aggregate metrics
858897

859898
You can't use filters with non-aggregate metric types. Instead, if your non-aggregate metrics are referencing aggregate metric types, you need to apply metric filters to the aggregate metrics.

0 commit comments

Comments
 (0)