Skip to content

Commit 16101d7

Browse files
Add custom date granularities documentation
Generated-By: mintlify-agent
1 parent 5df1d5f commit 16101d7

2 files changed

Lines changed: 130 additions & 3 deletions

File tree

references/dimensions.mdx

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ The table below shows all the dimension properties you can customize:
157157
| [type](#type) | No | Dimension type | The dimension type is automatically pulled from your table schemas in Lightdash but you can override the type using this property. |
158158
| [description](#description) | No | string | Description of the dimension in Lightdash. You can use this to override the description you have for the dimension in dbt. |
159159
| sql | No | string | Custom SQL applied to the column used to define the dimension. |
160-
| [time_intervals](#time-intervals) | No | 'default' or OFF or an array[] containing elements of [date](#date-options), [numeric](#numeric-options) or [string](#string-options) options | 'default' (or not setting the time_intervals property) will be converted into ['DAY', 'WEEK', 'MONTH', 'QUARTER', 'YEAR'] for dates and ['RAW', 'DAY', 'WEEK', 'MONTH', 'QUARTER', 'YEAR'] for timestamps; if you want no time intervals set 'OFF'. |
160+
| [time_intervals](#time-intervals) | No | 'default' or OFF or an array[] containing elements of [date](#date-options), [numeric](#numeric-options), [string](#string-options) options, or [custom granularity](#using-custom-granularities) names | 'default' (or not setting the time_intervals property) will be converted into ['DAY', 'WEEK', 'MONTH', 'QUARTER', 'YEAR'] for dates and ['RAW', 'DAY', 'WEEK', 'MONTH', 'QUARTER', 'YEAR'] for timestamps; if you want no time intervals set 'OFF'. You can also include custom granularity names defined in `lightdash.config.yml`. |
161161
| hidden | No | boolean | If set to true, the dimension is hidden from Lightdash. By default, this is set to false if you don't include this property. Hidden dimensions are also excluded from drilldowns (View underlying data). |
162162
| [compact](#compact) | No | string | This option will compact the number value (e.g. 1,500 to 1.50K). Currently supports one of the following: ['thousands', 'millions', 'billions', 'trillions', 'kilobytes', 'megabytes', 'gigabytes', 'terabytes', 'petabytes', 'kibibytes', 'mebibytes', 'gibibytes', 'tebibytes', 'pebibytes'] |
163163
| [format](#format) | No | string | This option will format the output value on the results table and CSV export. Supports spreadsheet-style formatting (e.g. #,##0.00). Use [this website](https://customformats.com) to help build your custom format. |
@@ -677,9 +677,67 @@ You can see all of the standard interval options for date and timestamp fields b
677677
| QUARTER_NAME | Quarter name | String | Q3 |
678678

679679

680-
### Custom time intervals
680+
### Using custom granularities
681681

682-
You can set custom versions of time intervals by using additional dimensions and groups. Here's an example of how that might look if you wanted to add `year_of_week_iso` to your drop down list for the `delivery_date` dimension. Note that by definiting the `groups:` option, we ensure that the new "Year of week" options is displayed as expected.
682+
You can define reusable custom time granularities in your `lightdash.config.yml` file and reference them in the `time_intervals` array. Custom granularities appear in the date zoom dropdown alongside standard options.
683+
684+
**Step 1**: Define custom granularities in `lightdash.config.yml`:
685+
686+
```yaml
687+
# lightdash.config.yml
688+
custom_granularities:
689+
fiscal_quarter:
690+
label: "Fiscal Quarter"
691+
sql: "DATE_TRUNC('quarter', ${COLUMN} + INTERVAL '1 month')"
692+
week_monday:
693+
label: "Week (Mon-Sun)"
694+
sql: "DATE_TRUNC('week', ${COLUMN})"
695+
```
696+
697+
**Step 2**: Reference them in your dimension's `time_intervals`:
698+
699+
<Tabs>
700+
<Tab title="dbt v1.9 and earlier">
701+
```yaml
702+
- name: order_date
703+
description: 'Date the order was placed'
704+
meta:
705+
dimension:
706+
type: date
707+
time_intervals: ['DAY', 'WEEK', 'fiscal_quarter', 'YEAR']
708+
```
709+
</Tab>
710+
<Tab title="dbt v1.10+ and Fusion">
711+
```yaml
712+
- name: order_date
713+
description: 'Date the order was placed'
714+
config:
715+
meta:
716+
dimension:
717+
type: date
718+
time_intervals: ['DAY', 'WEEK', 'fiscal_quarter', 'YEAR']
719+
```
720+
</Tab>
721+
<Tab title="Lightdash YAML">
722+
```yaml
723+
dimensions:
724+
- name: order_date
725+
description: 'Date the order was placed'
726+
type: date
727+
time_intervals: ['DAY', 'WEEK', 'fiscal_quarter', 'YEAR']
728+
```
729+
</Tab>
730+
</Tabs>
731+
732+
The `${COLUMN}` placeholder in the SQL expression is automatically replaced with the dimension's column SQL at runtime. Custom granularities also inherit `requiredAttributes` and `anyAttributes` from the parent dimension.
733+
734+
<Info>
735+
See [lightdash.config.yml reference](/references/lightdash-config-yml#custom-granularities-configuration) for the full configuration options for custom granularities.
736+
</Info>
737+
738+
### Custom time intervals with additional dimensions
739+
740+
You can also set custom versions of time intervals by using additional dimensions and groups. Here's an example of how that might look if you wanted to add `year_of_week_iso` to your drop down list for the `delivery_date` dimension. Note that by definiting the `groups:` option, we ensure that the new "Year of week" options is displayed as expected.
683741

684742

685743

references/lightdash-config-yml.mdx

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ parameters:
9393
# Configuration for project-wide defaults
9494
defaults:
9595
# ...
96+
97+
# Configuration for custom date granularities
98+
custom_granularities:
99+
# ...
96100
```
97101

98102

@@ -248,4 +252,69 @@ models:
248252

249253
<Info>
250254
See [Tables reference](/references/tables#case-sensitive) for explore-level configuration and [Dimensions reference](/references/dimensions#case-sensitive) for dimension-level configuration.
255+
</Info>
256+
257+
258+
## Custom granularities configuration
259+
260+
The `custom_granularities` section allows you to define reusable custom time granularities that appear in the date zoom dropdown alongside standard options (Day, Week, Month, Quarter, Year). This is useful for business-specific time periods like fiscal quarters, Monday-to-Sunday weeks, or other custom date groupings.
261+
262+
```yaml
263+
custom_granularities:
264+
fiscal_quarter:
265+
label: "Fiscal Quarter"
266+
sql: "DATE_TRUNC('quarter', ${COLUMN} + INTERVAL '1 month')"
267+
week_monday:
268+
label: "Week (Mon-Sun)"
269+
sql: "DATE_TRUNC('week', ${COLUMN})"
270+
fiscal_year:
271+
label: "Fiscal Year"
272+
sql: "EXTRACT(YEAR FROM ${COLUMN} + INTERVAL '1 month')"
273+
```
274+
275+
Each custom granularity is defined as a key-value pair where the key is the granularity name (must be alphanumeric with underscores) and the value is an object with the following properties:
276+
277+
| Property | Required | Value | Description |
278+
| :------- | :------- | :----- | :---------- |
279+
| `label` | Yes | string | A user-friendly label for the granularity as it will be displayed in the date zoom dropdown. |
280+
| `sql` | Yes | string | SQL expression that defines how to truncate/transform the date. Use `${COLUMN}` as a placeholder for the actual date column - it will be replaced with the column's SQL at runtime. |
281+
282+
### How custom granularities work
283+
284+
Custom granularities are defined at the project level in `lightdash.config.yml` and become available on date dimensions through the `time_intervals` property. The `${COLUMN}` template in the SQL expression is automatically replaced with the actual column SQL when generating queries.
285+
286+
**Example**: Define a fiscal quarter granularity and use it on a dimension:
287+
288+
```yaml
289+
# lightdash.config.yml
290+
custom_granularities:
291+
fiscal_quarter:
292+
label: "Fiscal Quarter"
293+
sql: "DATE_TRUNC('quarter', ${COLUMN} + INTERVAL '1 month')"
294+
```
295+
296+
```yaml
297+
# In your model yml file
298+
columns:
299+
- name: order_date
300+
meta:
301+
dimension:
302+
type: date
303+
time_intervals: ['DAY', 'WEEK', 'MONTH', 'fiscal_quarter', 'YEAR']
304+
```
305+
306+
The custom granularity `fiscal_quarter` will now appear in the date zoom dropdown for `order_date`, alongside the standard intervals.
307+
308+
<Info>
309+
Custom granularities inherit `requiredAttributes` and `anyAttributes` from the parent dimension, so access controls are automatically applied.
310+
</Info>
311+
312+
### Use cases
313+
314+
- **Fiscal calendars**: Define fiscal quarters or years that don't align with calendar quarters
315+
- **Week start customization**: Create weeks that start on Monday or any other day
316+
- **Custom periods**: Define business-specific time periods like "retail weeks" or "academic terms"
317+
318+
<Info>
319+
See [Dimensions reference](/references/dimensions#time-intervals) for more information on configuring time intervals on dimensions, including how to reference custom granularities.
251320
</Info>

0 commit comments

Comments
 (0)