Skip to content

Commit 2b83c38

Browse files
authored
add date params to docs (#264)
1 parent 14c6678 commit 2b83c38

File tree

4 files changed

+128
-35
lines changed

4 files changed

+128
-35
lines changed

guides/period-over-period.mdx

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,4 +340,84 @@ To do this, you:
340340

341341
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.
342342

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}
389+
then 'current period'
390+
391+
--previous period
392+
when ${order_date_day} between date_sub(
393+
${lightdash.parameters.dbt_orders.custom_range_start},
394+
interval (date_diff(
395+
${lightdash.parameters.dbt_orders.custom_range_end},
396+
${lightdash.parameters.dbt_orders.custom_range_start},
397+
day
398+
) + 1) day
399+
) and date_sub(
400+
${lightdash.parameters.dbt_orders.custom_range_end},
401+
interval (date_diff(
402+
${lightdash.parameters.dbt_orders.custom_range_end},
403+
${lightdash.parameters.dbt_orders.custom_range_start},
404+
day
405+
) + 1) day
406+
)
407+
then 'previous period'
408+
409+
--previous year
410+
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:
420+
421+
<Frame>
422+
<img src="/images/guides/custom_range_pop.png" alt=""/>
423+
</Frame>

guides/using-parameters.mdx

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ sidebarTitle: Parameters
55

66
**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.
77

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.
99

1010
<Info>
1111
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
2828
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.
2929

3030
<Info>
31-
**Parameter Types**: Parameters support both string and 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.
3232
</Info>
3333

3434

@@ -45,6 +45,7 @@ Parameters can be referenced in many places throughout your Lightdash project:
4545
7. **Additional Dimensions**: Use parameters in the SQL definition of an [additional dimension](/references/dimensions#additional-dimensions)
4646
8. **Custom Dimensions**: Use parameters in [custom dimension](/references/custom-fields#custom-sql) definitions
4747

48+
4849
## Parameter types
4950

5051
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:
5556

5657
- **String** (default): Text values
5758
- **Number**: Numeric values (integers and decimals)
59+
- **Date**: Date values (date selector is shown in the UI)
60+
5861

5962
### Type conversion workarounds
6063

@@ -69,17 +72,6 @@ ${lightdash.parameters.parameter_name}::type
6972
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.
7073
</Note>
7174

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-
```
8375

8476
#### Boolean conversion
8577

@@ -100,6 +92,7 @@ WHERE ${TABLE}.category_id = ${lightdash.parameters.category}::uuid
10092
WHERE ${TABLE}.amount = ${lightdash.parameters.amount}::decimal(10,2)
10193
```
10294

95+
10396
## How to reference parameters in SQL
10497

10598
### Project-level parameters
@@ -116,6 +109,7 @@ For example, to reference a parameter named `region`:
116109
${lightdash.parameters.region}
117110
```
118111

112+
119113
### Model-level parameters
120114

121115
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:
130124
${lightdash.parameters.orders.region}
131125
```
132126

127+
133128
### Using the shorter alias
134129

135130
You can also use the shorter alias `ld` instead of `lightdash`:
@@ -152,25 +147,27 @@ ${ld.parameters.region}
152147
${ld.parameters.orders.region}
153148
```
154149

150+
155151
## How to define parameters
156152

157153
Parameters can be defined at two different levels in your Lightdash project:
158154

155+
159156
### Project-level parameters
160157

161158
Project-level parameters are defined in your `lightdash.config.yml` file and are available across your entire project. Here's an example:
162159

163160
```yaml
164161
parameters:
165162
# 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"
169166
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"
174171

175172
# Parameter with multiple selection enabled
176173
region:
@@ -193,7 +190,13 @@ parameters:
193190
- 5000
194191
- 10000
195192
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+
197200
# Parameter with options from a dimension
198201
department:
199202
label: "Department"
@@ -205,6 +208,7 @@ parameters:
205208
206209
For a complete reference of project-level parameter properties and options, see the [lightdash.config.yml reference](/references/lightdash-config-yml#parameters-configuration).
207210
211+
208212
### Model-level parameters
209213
210214
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:
214218
- name: orders
215219
meta:
216220
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"
225229
```
226230

227231
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:
337341
```sql
338342
-- Table calculation example
339343
CASE
340-
WHEN ${orders.order_date} >= ${lightdash.parameters.date_range}
344+
WHEN ${orders.order_date} >= ${lightdash.parameters.date_range_start}
341345
THEN ${orders.revenue}
342346
ELSE 0
343347
END
@@ -361,7 +365,7 @@ models:
361365
additional_dimensions:
362366
is_after_cutoff_date:
363367
type: boolean
364-
sql: ${order_date} >= ${lightdash.parameters.date_range}
368+
sql: ${order_date} >= ${lightdash.parameters.date_range_start}
365369
```
366370

367371
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
378382
revenue
379383
FROM orders
380384
WHERE region IN (${lightdash.parameters.region})
381-
AND order_date >= ${lightdash.parameters.date_range}
385+
AND order_date >= ${lightdash.parameters.date_range_start}
382386
AND revenue >= ${lightdash.parameters.min_revenue}
383387
```
384388

385389
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.
386390

391+
387392
### Model parameters from joined tables in dimensions
388393

389394
When working with joined tables, you can reference model-level parameters from the joined table in your dimension definitions:

images/guides/custom_range_pop.png

252 KB
Loading

references/lightdash-config-yml.mdx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,15 @@ parameters:
145145
- 5000
146146
- 10000
147147
default: 5000
148-
department:
148+
custom_date_range_start:
149+
label: "Custom date range - start"
150+
description: "The start date for a custom date range filter"
151+
type: "date"
152+
custom_date_range_end:
153+
label: "Custom date range - end"
154+
description: "The end date for a custom date range filter"
155+
type: "date"
156+
department:
149157
label: "Department"
150158
description: "Filter data by department"
151159
options_from_dimension:
@@ -159,7 +167,7 @@ Each parameter is defined as a key-value pair where the key is the parameter nam
159167
| :----------------------- | :------- | :------------------------- | :---------- |
160168
| `label` | Yes | string | A user-friendly label for the parameter as it will be displayed in the UI. |
161169
| `description` | No | string | A description of the parameter. |
162-
| `type` | No | "string" or "number" | The type of the parameter. Defaults to "string" if not specified. |
170+
| `type` | No | "string", "number", or "date" | The type of the parameter. Defaults to "string" if not specified. |
163171
| `options` | No | Array of strings or numbers | A list of possible values for the parameter. |
164172
| `default` | No | string, number, or Array of strings/numbers | The default value(s) for the parameter. |
165173
| `multiple` | No | boolean | Whether the parameter input will be a multi-select. |

0 commit comments

Comments
 (0)