Amanda
Amanda

Reputation: 1

Add a time range to a virtual dataset on superset with JINJA

I can use the following query with JINJA in Superset's SQL Lab:

SELECT {{ current_username() }}

and it returns the user name. But when I use the following query:

SELECT *
FROM tbl
WHERE dttm_col > '{{ from_dttm }}' and dttm_col < '{{ to_dttm }}'

it returns this error:

Failed to execute query '128'. The following parameters in your query are undefined: 'from_dttm", "to_dttm". Please check your templates parameters for syntax errors and make sure they match across your SQL query and et Parameters.

Upvotes: 0

Views: 174

Answers (1)

Letsios Matthaios
Letsios Matthaios

Reputation: 306

To add those parameters you have to:

  • click the 3 dots on the right side
  • then click on parameters
  • and in the new popup add a valid json file with the proper parameters

In my example I have this query

SELECT order_number, order_date, sales
FROM cleaned_sales_data 
WHERE TRUE
    {% if from_dttm is not none %}
      AND order_date > '{{ from_dttm }}'
    {% endif %}
    {% if to_dttm is not none %}
      AND order_date < '{{ to_dttm }}'
    {% endif %}

and I added the following parameters:

{
"from_dttm": "2003-05-01",
"to_dttm": "2003-06-01"
}

enter image description here

and then you get the proper results based on the given parameters

enter image description here

Upvotes: 0

Related Questions