In Grafana 12.0.0 (Linux), I want to create a pie chart that will give a snapshot of daily stats. The “stats_zone” table looks like this:
+---------------+--------+------------+
| snapshot_date | zone | num_assets |
+---------------+--------+------------+
| 2025-05-25 | AMER | 86858 |
| 2025-05-25 | EMEAR | 140324 |
| 2025-05-25 | APJCI | 319442 |
| 2025-05-26 | AMER | 87385 |
| 2025-05-26 | EMEAR | 141425 |
| 2025-05-26 | APJCI | 320962 |
| 2025-05-27 | AMER | 88741 |
| 2025-05-27 | EMEAR | 137056 |
| 2025-05-27 | APJCI | 320196 |
+---------------+--------+------------+
I can create a variable in my dashboard called “stats_date” that looks like this:
SELECT DISTINCT DATE_FORMAT(snapshot_date, '%Y-%m-%d') FROM stats_zone AS stats_date ORDER BY snapshot_date DESC;
and then use it in my query to build the pie chart:
SELECT zone AS metric, num_assets AS value FROM stats_zone WHERE snapshot_date = '$stats_date'
So far so good. The problem is that when I save this database, the value of “stats_date” that was active when I built the dashboard becomes the value you see whenever you visit the dashboard. If I save the dashboard today, then tomorrow the first value in the dropdown list on the dashboard will be 2025-05-28, but the selected default value will still be 2025-05-27. The user must manually pick the latest date from the stats_date dropdown.
How can I configure this dashboard to always default to the first entry in the stats_date dropdown (which will always be the latest date from the database), and not a hardcoded value?
Thanks to anyone who can help.