grafanainfluxdbflux

# InfluxDB: How to add a column that's the result of difference of consecutive rows/timestamps using Flux?


I'm new to InfluxDB & the Flux query language so appreciate the patience! Happy to be redirected to documentation but I haven't found anything genuinely useful. I have a simple time-series data with one _measurement so far.

Screenshot from Grafana

I want to create another column or table (whatever), which has the values that are the difference of consecutive values in the column (G_PAED) as shown in the screenshot above. For example:

What is expected!

I have the following query to get the table shown in the first image.

Flux query to get the data

I couldn't find anything on the flux documentation, neither in the Grafana to perform this operation. Any help would be appreciated. Thank you.

I tried using the map() function to create a new column, but I don't know how to iterate/index the G_PAED in flux or if it's even possible.

|> map(fn: (r) => ({r with _diff: r.G_PAED - r.G_PAED }))

Solution

  • I believe what you are looking for is the difference() function.

    AggregatedConsumption = from(bucket: "Energy")
        |> range(start: v.timeRangeStart, end: v.timeRangeEnd)
        |> filter(fn: (r) => r["_measurement"] == "General")
        |> filter(fn: (r) => r["_field"] == "G_PAED")
        |> aggregateWindow(every: 1m, fn: last, createEmpty: false)
        |> difference()
        |> yield(name: "AggregatedConsumption")
    

    This will only return the aggregated data so if you need both you will need to use the join() function (or create multiple queries on grafana)