Search code examples
grafanainfluxdb

Is there a way to programmatically change the function performed in a query?


I'm utilizing Grafana with InfluxDB as the database.

Say I have the following query

SELECT MIN("field_1"), MAX("field_1") FROM "measurement" WHERE $timeFilter 

but I would like the user the ability to view the MEAN in the same panel instead

SELECT MEAN("field_1") FROM "measurement" WHERE $timeFilter 

Is there a way to accomplish this? Ideally the solution would be agnostic to field_1 as I'd like to use it across multiple panels with different fields.


Solution

  • Create Grafana dashboard variable e.g. function with values, which match InfluxDB functions: MIN,MAX,MEAN,COUNT,LAST,... and then use that variable in the Grafana InfluxDB query:

    SELECT ${function:raw}("field_1") FROM "measurement" WHERE $timeFilter
    

    so Grafana generates correct InfluxDB query syntax.

    Even field can be dashboard variable, which can be automatically discovered with InfluxDB query SHOW FIELD KEYS.