Variable Examples
Variable Examples
1 Dataset Date Filtering
To improve dataset query efficiency, it is often necessary to use date filter conditions in datasets, as shown below:
SELECT
-- ... Omitted SQL
FROM foodmart.sales_fact_sample a
JOIN foodmart.time_by_day b ON a.time_id = b.time_id
WHERE b.the_date >= '${arr.get(dt, 0, cdt.addMonth(-1, "yyyy-MM-dd") )}'
AND b.the_date <= '${arr.get(dt, 1, cdt.now("yyyy-MM-dd") )}'Built-in variable function arr.get(array variable, index, default value): Retrieves the value at the specified index of an array variable. Array variables are typically generated by date range query components, but for error tolerance, if the first passed value is a single value, it will still work correctly.
Date Index and Value Type
If your database is Oracle or has the issue "Implicit conversion between date fields (DATE type) and strings causes index failure, similar to Oracle", please use a string-to-date function to convert the date variable to a date. For example:
b.the_date <=
to_date('${arr.get(dt, 1, cdt.now("yyyy-MM-dd") )}', 'yyyy-MM-dd')to_date function depends on your database. Query scripts in dataset definitions do not currently support unified functions.1.1 Assigning Date Variables in Self-Service Analysis
Scenario 1: Dataset Variables Not Declared in Advance
We know that variables can be used even if they are not declared in advance. The downside is that if they are not declared in advance, during the self-service analysis (chart design) stage, it is unknown which variables are defined in the dataset, their names, data types, and whether they are arrays or single values.
In this case, we need to manually input variable names (usually variable names need to be uniformly agreed upon or inquired from the dataset maintainer), select variable types, and set variable values. The following operation assigns values to a date range variable:
Scenario 2: Dataset Variables Declared in Advance
If variables and their default values are declared during the dataset maintenance phase, the variable list will automatically pop up when opening variable settings during the chart design stage.
1.2 Assigning Date Variables in Dashboards
Variable assignments in self-service analysis/chart design are temporary and primarily serve self-service analysis, as temporary charts may not be added to dashboards. Additionally, variables in dashboards take precedence over those in self-service analysis. So, how do we assign values to variables during the dashboard design stage?
Steps:
- Add a date range component.
- Edit the date range component and associate it with an environment variable (this date control can also be associated with multiple other objects).
- Set the environment variable key to the variable
dtused in the dataset.
2 Binding Variables to Date Dimensions
Through the above method, we have fully implemented dynamic date queries for datasets. However, one inconvenience is that from the self-service analysis stage onward, users must know that date range changes take effect only through variable assignments, which may be a barrier for non-technical users.
If users are unaware of the existence of date variables and only drag date dimensions to the filter bar, the query will combine dataset dynamic date queries and self-service analysis dimension filters, ultimately forming an intersection.
Issue
Because variables do not change, dataset filter conditions remain fixed. Regardless of how dimension filter conditions are set, only data within the dataset's date range can be viewed.
WITH S0 AS (
SELECT
-- ... Omitted SQL
FROM foodmart.sales_fact_sample a
JOIN foodmart.time_by_day b ON a.time_id = b.time_id
-- 1 Dataset filter condition: last 10 days
WHERE b.the_date >= '2025-12-15'
AND b.the_date <= '2025-12-25'
)
SELECT DATE_FORMAT(`r_date`, '%Y-%m-%d') AS c_0,
SUM(`store_sales`) AS v_0
FROM S0
-- 2 Dimension filter condition: last year
WHERE `r_date`>=STR_TO_DATE('2024-12-25', '%Y-%m-%d')
GROUP BY DATE_FORMAT(`r_date`, '%Y-%m-%d')Is there a way to use variables for dataset queries while making users unaware of date variables during self-service analysis?
The answer is: Binding dimensions to variables
- During dataset design, edit the date dimension.
- In the dimension advanced configuration bar, bind the date dimension field to the variable
dtand restrict the date filter method to range filtering. - During self-service analysis, do not set filter conditions for the date dimension; use the default filter range.
- Add date conditions through regular dimension filtering (rows/columns dimensions, filter bar allowed), and the set date values automatically bind to the predefined variable
dt.
Dashboard Filter Configuration
Similarly, when configuring associations for dashboard filters, users do not need to worry about variables. Simply associate the date filter directly with the date field, allowing dataset users to remain unaware of variables from self-service analysis to dashboard design stages.
Note
Variables bound through dimension filters only affect the chart itself and will not be applied to other chart components.