Search code examples
sqlpostgresqlgrafanagrafana-templatinggrafana-variable

Grafana: How to check in a query if a single variable is selected or more?


I have a dashboard where I have a variable X for which one or more values can be selected. The data source is postgresql. I have a panel where I make a query to show some trends using X. Up till this point everything is working.

Now, I need to check if the the user has select only one value of the variable X. if the user has selected only one value, then we need to change the column value slightly. How do I check if the user has selected only 1 value? Is there a grafana construct for that? or should I do it via SQL purely?


Solution

  • So, I found a way to check that:

    coalesce(array_length(ARRAY[$X],1),0) = 1
    

    This works for postgreSQL. If you know the above functions, it is pretty self-explanatory.