Grafana : Conditional Update of custom variable

No Grafana does not allow direct if-else logic between variables in the variable settings.
But we can use a workaround using a query variable with PostgreSQL.
You can you this approch mention in steps

Step 1: Create variable y

Step 2: Create variable x

  • Type: Query
  • Name: x
  • Data source: PostgreSQL
  • Query:
SELECT CASE
         WHEN '${y}'::int = 1 THEN '0'
         WHEN '${y}'::int = 0 THEN NULL
       END AS x;

This will make x return:

  • 0 when y = 1

  • null when y = 0

  • Type: Custom

  • Name: y

  • Values: 1,0

  • This will create a dropdown so you can choose between 1 or 0

How to use x

In your panel queries, just use ${x} like this:

SELECT *
FROM my_table
WHERE some_column = ${x}