You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by "bhargavmehta01 (via GitHub)" <gi...@apache.org> on 2026/10/12 03:54:38 UTC
Re: [D] The bar chart for different period values [superset]
GitHub user bhargavmehta01 added a comment to the discussion: The bar chart for different period values
You don't need two filters for this: put each period inside its own metric, and the bar chart puts two bars next to each other for every store.
**Fixed periods (no setup)**
In a **Bar Chart**, set **X-axis** = `store` and add two **Custom SQL** metrics:
```sql
-- label: June 2026
SUM(CASE WHEN sale_date >= '2026-06-01' AND sale_date < '2026-07-01' THEN amount END)
-- label: July 2026
SUM(CASE WHEN sale_date >= '2026-07-01' AND sale_date < '2026-08-01' THEN amount END)
```
Set the chart's time range to **No filter**, or to a range that covers both periods, so the outer WHERE doesn't cut one of them off.
**Periods you pick with two filters**
Turn on `ENABLE_TEMPLATE_PROCESSING`. Then build a virtual dataset that exposes the date column twice, once for each period, and reads each period's filter with `get_time_filter(..., remove_filter=True)`:
```sql
{% set a = get_time_filter('period_a', default='2026-06-01 : 2026-07-01', remove_filter=True) %}
{% set b = get_time_filter('period_b', default='2026-07-01 : 2026-08-01', remove_filter=True) %}
SELECT
store,
sale_date AS period_a,
sale_date AS period_b,
CASE WHEN sale_date >= {{ a.from_expr }} AND sale_date < {{ a.to_expr }} THEN amount END AS amount_a,
CASE WHEN sale_date >= {{ b.from_expr }} AND sale_date < {{ b.to_expr }} THEN amount END AS amount_b
FROM sales
```
In the chart:
- X-axis: `store`
- Metrics: `SUM(amount_a)` labeled "Period A", and `SUM(amount_b)` labeled "Period B"
- Filters: one time-range filter on `period_a` and one on `period_b`
`remove_filter=True` keeps those two filters out of the outer WHERE, so they don't cancel each other out. `from_expr` and `to_expr` are already formatted for your database, so don't quote them. Make sure `period_a` and `period_b` are marked **Is temporal** in the dataset's columns.
**If the comparison is always relative** (this month against the same month last year), you don't need any of this. Use a time-series Bar Chart with **Advanced analytics → Time comparison → Time shift**, for example `1 year ago`. It takes free text such as `28 days`.
How I checked: I ran the CASE WHEN SQL on a small sample table (2 stores, 3 months), and it returned one June and one July total per store, with the May rows left out. I read the `get_time_filter` signature and `remove_filter` handling in `superset/jinja_context.py` at the 6.1.0 tag. I haven't run the Jinja dataset in a live Superset instance yet.
GitHub link: https://github.com/apache/superset/discussions/43332#discussioncomment-18873361
----
This is an automatically sent email for notifications@superset.apache.org.
To unsubscribe, please send an email to: notifications-unsubscribe@superset.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org