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: guides/period-over-period.mdx
+81-1Lines changed: 81 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -340,4 +340,84 @@ To do this, you:
340
340
341
341
You can then add this chart to a dashboard. Users can select an option from the `Date Range` parameter and the charts will automatically update to display the selected period.
342
342
343
-
Take a look at our example dashboard in our demo site [here](https://demo.lightdash.com/projects/2014e038-ff4b-4761-ae6f-fbf551e7b468/dashboards/8813b2bf-3983-48b0-aff7-088b59490cfa/view).
343
+
Take a look at our example dashboard in our demo site [here](https://demo.lightdash.com/projects/2014e038-ff4b-4761-ae6f-fbf551e7b468/dashboards/8813b2bf-3983-48b0-aff7-088b59490cfa/view).
344
+
345
+
346
+
### Custom date range example
347
+
348
+
Similar to above, you can also set up a custom date range parameter that allows users to select specific start and end dates for their analysis.
349
+
350
+
Below is the code needed for the parameter and the additional dimension to categorize the date periods for a custom date range:
351
+
352
+
<Accordion title="Custom date range parameter configuration">
353
+
``` yaml
354
+
parameters:
355
+
custom_range_start:
356
+
label: "Start of custom date range"
357
+
description: "The start of the custom date range"
358
+
type: date
359
+
custom_range_end:
360
+
label: "End of custom date range"
361
+
description: "The end of the custom date range"
362
+
type: date
363
+
```
364
+
</Accordion>
365
+
366
+
367
+
<Accordion title="Custom date range additional dimension code">
368
+
``` yaml
369
+
- name: order_date
370
+
description: 'Date of order placement by user.'
371
+
meta:
372
+
dimension:
373
+
type: date
374
+
additional_dimensions:
375
+
order_date_custom_period:
376
+
description: Date period bucket based on custom Start date and End date parameters
377
+
type: string
378
+
sql: |
379
+
case
380
+
--invalid date range
381
+
when ${lightdash.parameters.dbt_orders.custom_range_start} is null
382
+
or ${lightdash.parameters.dbt_orders.custom_range_end} is null
383
+
or ${lightdash.parameters.dbt_orders.custom_range_start} > ${lightdash.parameters.dbt_orders.custom_range_end}
384
+
then 'invalid date range'
385
+
386
+
--current period
387
+
when ${order_date_day} >= ${lightdash.parameters.dbt_orders.custom_range_start}
388
+
and ${order_date_day} <= ${lightdash.parameters.dbt_orders.custom_range_end}
when date(${order_date_day}) between date_sub(${lightdash.parameters.dbt_orders.custom_range_start}, interval 1 year)
411
+
and date_sub(${lightdash.parameters.dbt_orders.custom_range_end}, interval 1 year)
412
+
then 'previous year'
413
+
414
+
else 'out of range'
415
+
end
416
+
```
417
+
</Accordion>
418
+
419
+
The code examples above will give you the following parameter options in the Lightdash UI that will output the custom period labels shown in the chart:
Copy file name to clipboardExpand all lines: guides/using-parameters.mdx
+37-32Lines changed: 37 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ sidebarTitle: Parameters
5
5
6
6
**Parameters** are variables that allow you to create **dynamic, reusable queries** in Lightdash. They enable users to interact with and customize queries without needing to write SQL.
7
7
8
-
Parameters are defined in your `lightdash.config.yml` file and can be referenced in various parts of your Lightdash project.
8
+
Parameters are defined in your `lightdash.config.yml` file or in a `schema.yml` file for a specific model, and can be referenced in various parts of your Lightdash project.
9
9
10
10
<Info>
11
11
If you're new to lightdash.config.yml, check out our [getting started guide](/references/lightdash-config-yml#getting-started-with-lightdashconfigyml) to learn how to create and set up this file.
@@ -28,7 +28,7 @@ Parameters are variables that you can define once and reference in multiple plac
28
28
For example, you might define a `region` parameter that users can set to filter data by different geographic regions, a `date_range` parameter that allows users to select different time periods for analysis, or a `min_revenue` parameter with numeric values that allows users to set revenue thresholds for analysis.
29
29
30
30
<Info>
31
-
**Parameter Types**: Parameters support both stringand number values. You can use strings (like `"EMEA"` or `"2023-01-01"`) or numbers (like `1000` or `5000`) as parameter options.
31
+
**Parameter Types**: Parameters support string, date, and number values. You can use strings (like `"EMEA"`) or numbers (like `1000`) or dates (date parameters will show a calendar picker) as parameter options.
32
32
</Info>
33
33
34
34
@@ -45,6 +45,7 @@ Parameters can be referenced in many places throughout your Lightdash project:
45
45
7.**Additional Dimensions**: Use parameters in the SQL definition of an [additional dimension](/references/dimensions#additional-dimensions)
46
46
8.**Custom Dimensions**: Use parameters in [custom dimension](/references/custom-fields#custom-sql) definitions
47
47
48
+
48
49
## Parameter types
49
50
50
51
Parameters in Lightdash support different data types to help you work with various kinds of data. By default, all parameters are treated as strings, but you can convert them to other types as needed.
@@ -55,6 +56,8 @@ Lightdash officially supports the following parameter types:
55
56
56
57
-**String** (default): Text values
57
58
-**Number**: Numeric values (integers and decimals)
59
+
-**Date**: Date values (date selector is shown in the UI)
The type conversion happens at the SQL level, so the available types depend on your database system (PostgreSQL, BigQuery, Snowflake, etc.). Common types like `integer`, `numeric`, `date`, `timestamp`, and `boolean` are supported across most databases.
70
73
</Note>
71
74
72
-
#### Date conversion
73
-
74
-
As a workaround, you can use `::date` for dates or `::timestamp` for datetime values:
75
-
76
-
```sql
77
-
-- Convert to date (workaround)
78
-
WHERE ${TABLE}.order_date >= ${lightdash.parameters.start_date}::date
79
-
80
-
-- Convert to timestamp (workaround)
81
-
WHERE ${TABLE}.created_at >= ${lightdash.parameters.start_datetime}::timestamp
82
-
```
83
75
84
76
#### Boolean conversion
85
77
@@ -100,6 +92,7 @@ WHERE ${TABLE}.category_id = ${lightdash.parameters.category}::uuid
100
92
WHERE ${TABLE}.amount = ${lightdash.parameters.amount}::decimal(10,2)
101
93
```
102
94
95
+
103
96
## How to reference parameters in SQL
104
97
105
98
### Project-level parameters
@@ -116,6 +109,7 @@ For example, to reference a parameter named `region`:
116
109
${lightdash.parameters.region}
117
110
```
118
111
112
+
119
113
### Model-level parameters
120
114
121
115
To reference model-level parameters in SQL, you need to include the model name:
@@ -130,6 +124,7 @@ For example, to reference a parameter named `region` from the `orders` model:
130
124
${lightdash.parameters.orders.region}
131
125
```
132
126
127
+
133
128
### Using the shorter alias
134
129
135
130
You can also use the shorter alias `ld` instead of `lightdash`:
@@ -152,25 +147,27 @@ ${ld.parameters.region}
152
147
${ld.parameters.orders.region}
153
148
```
154
149
150
+
155
151
## How to define parameters
156
152
157
153
Parameters can be defined at two different levels in your Lightdash project:
158
154
155
+
159
156
### Project-level parameters
160
157
161
158
Project-level parameters are defined in your `lightdash.config.yml` file and are available across your entire project. Here's an example:
162
159
163
160
```yaml
164
161
parameters:
165
162
# Parameter with simple options list
166
-
date_range:
167
-
label: "Date Range"
168
-
description: "Filter data by date range"
163
+
team:
164
+
label: "Team"
165
+
description: "Filter data by team"
169
166
options:
170
-
- "2023-01-01"
171
-
- "2022-01-01"
172
-
- "2021-01-01"
173
-
default: "2023-01-01"
167
+
- "Sales"
168
+
- "Marketing"
169
+
- "Finance"
170
+
default: "Sales"
174
171
175
172
# Parameter with multiple selection enabled
176
173
region:
@@ -193,7 +190,13 @@ parameters:
193
190
- 5000
194
191
- 10000
195
192
default: 5000
196
-
193
+
194
+
# Parameter with date type
195
+
min_date:
196
+
label: "Minimum date"
197
+
description: "Filter to only show data after this date"
198
+
type: "date"
199
+
197
200
# Parameter with options from a dimension
198
201
department:
199
202
label: "Department"
@@ -205,6 +208,7 @@ parameters:
205
208
206
209
For a complete reference of project-level parameter properties and options, see the [lightdash.config.yml reference](/references/lightdash-config-yml#parameters-configuration).
207
210
211
+
208
212
### Model-level parameters
209
213
210
214
Model-level parameters are defined within individual model YAML files in your dbt project and are scoped to the model where they are defined. These parameters are defined in the `meta.parameters` section of your model configuration:
@@ -214,14 +218,14 @@ models:
214
218
- name: orders
215
219
meta:
216
220
parameters:
217
-
date_range:
218
-
label: "Date Range"
219
-
description: "Filter data by date range"
220
-
options:
221
-
- "2023-01-01"
222
-
- "2022-01-01"
223
-
- "2021-01-01"
224
-
default: "2023-01-01"
221
+
date_range_start:
222
+
label: "Date Range Start"
223
+
description: "Start date for filtering orders in custom time period metrics"
224
+
type: "date"
225
+
date_range_end:
226
+
label: "Date Range End"
227
+
description: "End date for filtering orders in custom time period metrics"
228
+
type: "date"
225
229
```
226
230
227
231
Model-level parameters offer the same configuration options as project-level parameters but provide better encapsulation and organization by keeping parameters close to where they're used.
@@ -337,7 +341,7 @@ You can reference parameters in table calculations:
337
341
```sql
338
342
-- Table calculation example
339
343
CASE
340
-
WHEN ${orders.order_date} >= ${lightdash.parameters.date_range}
344
+
WHEN ${orders.order_date} >= ${lightdash.parameters.date_range_start}
This additional dimension will indicate whether an order was placed on or after the date selected in the `date_range` parameter.
@@ -378,12 +382,13 @@ SELECT
378
382
revenue
379
383
FROM orders
380
384
WHERE region IN (${lightdash.parameters.region})
381
-
AND order_date >= ${lightdash.parameters.date_range}
385
+
AND order_date >= ${lightdash.parameters.date_range_start}
382
386
AND revenue >= ${lightdash.parameters.min_revenue}
383
387
```
384
388
385
389
This query will filter orders by the regions selected in the `region` parameter, by the date selected in the `date_range` parameter, and by orders with revenue greater than or equal to the numeric `min_revenue` parameter.
386
390
391
+
387
392
### Model parameters from joined tables in dimensions
388
393
389
394
When working with joined tables, you can reference model-level parameters from the joined table in your dimension definitions:
0 commit comments