Flux Query: Calculate average of measurement in current month

Hi,

I’m collecting the energy price on an hourly basis into InfluxDB. I’m trying to setup a query that would give me an updated average price of energy during current month.

Flux Query:

import “strings”

month = time(v: “${strings.substring(v: string(v: now()), start: 0, end: 8)}01T00:00:00Z”)

from(bucket: “bucket”)
|> range(start: month)
|> filter(fn: (r) => r[“_measurement”] == “energyprice”)
|> filter(fn: (r) => r[“_field”] == “value”)
|> aggregateWindow(every: 31d, fn:mean)

Does this look correct? I am not sure how I should setup the aggregateWindow.

Idea is that the query would give me a continuous update on the average price during a calendar month.

So f.ex on the 15th of December it should show me the average of all samples between 1-15th and so on.

@Stian_Braa You were super close! aggregateWindow() aggregates data in intervals over time. In your case, you just need a single aggregation, not an aggregation over time. There’s also a simpler way to get the timestamp for the beginning of the month:

import “date”

month = date.truncate(t: now(), unit: 1mo)

from(bucket: "bucket")
    |> range(start: month)
    |> filter(fn: (r) => r["_measurement"] == "energyprice")
    |> filter(fn: (r) => r["_field"] == "value")
    |> mean()

Thanks alot @scott !!