Dashboard Filter > Raw SQL Query Variable
Learn how to create a Raw SQL Query Variable filter in Draxlr to pass dashboard filter values into variables in your raw SQL queries, with multi-value and date range support.
Raw SQL Query Variable filters pass the value a viewer picks on the dashboard into a variable in your Raw SQL queries, such as {{employeeId}}. Use them when you want full control over how the value is used in the SQL.
Unlike Standard filters, which filter by column values, Raw SQL Query Variable filters only work with Raw SQL items that contain variables.
Here is an interactive demo of creating and using a Raw SQL Query Variable filter. The dashboard has a Raw SQL item, Orders by Ship Country, that uses this query:
SELECT ShipCountry, COUNT(*) AS Orders
FROM Orders
WHERE 1 = 1
[[AND EmployeeID IN ({{employeeId}})]]
GROUP BY ShipCountry
ORDER BY Orders DESC;

Click Add Filter.
How to Create a Raw SQL Query Variable Filter
1. Add a filter
Click Add Filter in the filter bar and choose Raw SQL Query Variable. This option appears when the dashboard has at least one Raw SQL item with variables, or is a global dashboard.
2. Enter a name
Give the filter a name that viewers will understand, such as Employee.
3. Pick a data type
Choose the Data Type: Text, Numeric, Boolean, Date & Time, Date or Time. Pick the same type you set for the variable in the Raw SQL editor, because only variables of that type can be selected.
4. Set the options for the data type
-
Allow multiple values (Text and Numeric): let viewers enter several values. Your query must use the variable with an operator that takes a list, such as
INorNOT IN:SELECT * FROM `Orders` WHERE `Orders`.`EmployeeID` IN ({{employeeId}}); -
Use date range (Date and Date & Time): let viewers pick a start and end date instead of a single value. See Filtering by a date range below.
-
Dropdown List (Text and Numeric, optional): show viewers a list of values to pick from. See Preset values.
5. Choose the variable in each item
Each Raw SQL item with variables shows Select Raw Query Variable. Choose the variable the filter's value should go into. Only variables that match the data type are listed, and for a date range filter only {{dateRange:<field name>}} variables.
- Click Use same for other elements to pick the variable with the same name in every other item that has it.
- Leave an item's variable empty if the filter shouldn't affect it.
- Items without a matching variable show No filter option.
6. Click Create
The filter appears in the filter bar.
7. Apply the filter
Click the filter, enter a value, and click the check mark. With Allow multiple values, separate the values with commas, for example 1, 4. The value is passed into the variable of every linked item, and those items update.
Keep items working without a value:
Wrap the condition that uses the variable in [[ ... ]] to make it optional.
The item then shows all rows until a value is picked in the filter. See
Making a section optional.
Filtering by a Date Range
Turn on Use date range to filter dashboard elements between a start and an end date. The option appears when the filter's data type is Date or Date & Time.
Your queries need a date range variable:
Date range filters only work with queries that use
{{dateRange:<field name>}}, where <field name> is the date column to
filter on. A regular variable such as {{orderDate}} can't receive a range.
For example, this saved query filters orders by their OrderDate:
SELECT * FROM `Orders`
WHERE {{dateRange:OrderDate}};
To keep the element working when the filter isn't applied, wrap the condition in an optional section:
SELECT * FROM `Orders`
WHERE 1 = 1
[[AND {{dateRange:OrderDate}}]];
When creating the filter:
- Select the Date or Date & Time data type.
- Turn on Use date range.
- For each dashboard element, select its
dateRange:<field name>variable. Variables of other types are not listed.
On the dashboard, the filter opens a date range picker. Pick a start and end date, or a preset: Today, This Week, Last Week, This Month, Last Month or This Year. The selected range is applied to the field in every connected element. Learn more about the syntax in Raw SQL.
Key Notes
- Works only with Raw SQL items that contain variables. For other items, use a Standard filter.
- The variable's data type must match the filter's data type, for example Numeric for IDs and Text for names.
- Multiple values need
INorNOT INin the query. - Date range filters need
{{dateRange:<field name>}}in the query.
