How to use Jinja template for "Inverse Selection" filter type #34372
-
I need help with Jinja templating for handling "Inverse Selection" filter type in SQL Lab/Dataset. Setup: Normal filter (Filter Type: "Filter") - works fine What Works - Normal Filter:
Problem:
Questions:
Expected Behavior:
Additional Context:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @natcharam! I'm Dosu and I’m helping the superset team. To handle both normal and "Inverse Selection" filters for the same column in a Jinja template, use the get_filters() macro instead of filter_values(). The get_filters() macro exposes the filter operator (IN or NOT IN), so you can generate the correct SQL logic for each filter type. Here’s how you can do it: {% for filter in get_filters('CAMPAIGN_GROUP') %}
{% if filter.op == 'IN' %}
AND CAMPAIGN_GROUP IN ({{ "'" + "', '".join(filter.val) + "'" }})
{% elif filter.op == 'NOT IN' %}
AND CAMPAIGN_GROUP NOT IN ({{ "'" + "', '".join(filter.val) + "'" }})
{% endif %}
{% endfor %} This approach works because get_filters() returns a list of filters for the column, each with an op field (like 'IN' or 'NOT IN') and a val field (the values). You can loop through them and generate the appropriate SQL for each filter type. filter_values() does not distinguish between filter types and will not handle NOT IN logic automatically—you need to use get_filters() for this use case. The NOT IN logic is not generated automatically; you must handle it in your template as shown above. See the code and docstring for get_filters() for more details and examples on this pattern To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
Hi @natcharam! I'm Dosu and I’m helping the superset team.
To handle both normal and "Inverse Selection" filters for the same column in a Jinja template, use the get_filters() macro instead of filter_values(). The get_filters() macro exposes the filter operator (IN or NOT IN), so you can generate the correct SQL logic for each filter type. Here’s how you can do it:
This approach works because get_filters() r…