Time Range Parameter in Metric #16117
Unanswered
qlpanalytics
asked this question in
Q&A / Help
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a metric with a nested query. In the nested select statement, I want to reference the to/from TIME RANGE specified in the filter/filter box:
![image](https://user-images.githubusercontent.com/88547703/128524307-0d83ee01-7b92-4f00-9bc1-913d85916f38.png)
I've tried using the {{ from_dttm }} and {{ to_dttm }} parameters, but they do not populate anything. We have feature flags enabled.
We have also tried creating a jinja context add on for time filter, but that also has not worked in the nested select statement.
Any ideas what needs to be done to reference the time range dates in my metric? Here is an example of the metric (dates are bolded that we want to reference the time range):
CASE WHEN lead = 'No' THEN 0
ELSE (SELECT SUM(cost) from database.abc WHERE lead = 'No'
AND date >= {{ from_dttm }}
AND date <= {{ to_dttm }})
END
I am using version 1.0.0. The following are enabled:
FEATURE_FLAGS = {
"ENABLE_TEMPLATE_PROCESSING": True,
}
from typing import Optional
from superset.utils.date_parser import get_since_until
from superset.utils.core import merge_extra_filters
def time_filter() -> Optional[str]:
from superset.views.utils import get_form_data
form_data, _ = get_form_data()
merge_extra_filters(form_data)
time_range = form_data.get("time_range")
since, until = get_since_until(time_range)
time_format = '%Y-%m-%d %H:%M:%S'
if not until:
return None
until = until.strftime(time_format)
if not since:
return '<= '{}''.format(until)
since = since.strftime(time_format)
return 'BETWEEN '{}' AND '{}''.format(since, until)
JINJA_CONTEXT_ADDONS = {
'time_filter': time_filter
}
Beta Was this translation helpful? Give feedback.
All reactions