GitHub user yappiverse created a discussion: Superset/Jinja Template filtering

# Multi-Level Filtering in Superset using Jinja Templates

I want to implement **multi-level filtering** on Superset using Jinja 
templates. Currently, I have two queries:

## Query 1: `debug_tenant_value`

```sql
SELECT DISTINCT
    id AS value,
    name AS label
FROM x.y.z
WHERE is_active = TRUE
ORDER BY label;
```

## Query 2: `debug_value_selected`

```sql
SELECT
    {% if filter_values('tenant') | length > 0 %}
        {% for t in filter_values('tenant') %}
            '{{ t }}' AS selected_value{% if not loop.last %},{% endif %}
        {% endfor %}
    {% else %}
        'No label selected' AS selected_value
    {% endif %}
```

### Dashboard Implementation

Here’s how I bring it onto the dashboard:

![Dashboard Screenshot 
1](https://github.com/user-attachments/assets/51157d0c-8d88-41c7-a622-75f41048612f)
![Dashboard Screenshot 
2](https://github.com/user-attachments/assets/be0d2aba-15c0-4ccb-8259-9733c438b48c)

---

## Problem

I want to **display the label to the user**, but still **use the value (id) in 
the actual query** later.

My current workaround is concatenating the `id` and `label`:

```sql
SELECT DISTINCT
    id || ' - ' || name AS label_tenant
FROM x.y.z
WHERE is_active = TRUE
ORDER BY label_tenant;
```

Then splitting it in the query like this:

```sql
SELECT DISTINCT
    service
FROM x.y.z
WHERE is_active = true
{% if filter_values('label_tenant') %}
    AND id_tenant IN (
        {% for label in filter_values('label_tenant') %}
            '{{ label.split(' - ')[0] }}'{% if not loop.last %}, {% endif %}
        {% endfor %}
    )
{% endif %}
ORDER BY service;
```

---

## Question

Is there a **better way** to achieve this? Or is concatenating the only option? 
I already tried to pass the value but it doesnt seems to actually passing the 
value, instead it pass the label

---

GitHub link: https://github.com/apache/superset/discussions/35979

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: 
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to