How can I get the date of the 'max' value measured by 'aggregateWindow' in InfluxDB?

I’ve currently written an InfluxDB query that:

  • Sets a time range of 1 hour before the current time.
  • Applies filters for a single ‘measurement’, ‘_field’, and multiple ‘tags’.
  • Aggregates data into 15-minute intervals and calculates the ‘max’ value within each interval.

Here’s an example of the query:

from(bucket: "bk")
  |> range(start: -1h)
  |> filter(fn: (r) => r["_measurement"] == "ED")
  |> filter(fn: (r) => r["_field"] == "value")
  |> filter(fn: (r) => r["tag"] =~ /TAG1_Hz|TAG1_Ia|TAG1_Ib/)
  |> aggregateWindow(every: 15m, fn: max, createEmpty: false)
  |> limit(n:4)
  |> yield(name: "max")

However, I need to additionally extract the date when the ‘max’ value occurred. Unfortunately, I’m not very familiar with InfluxDB and am having trouble with this. Could you please help me?

The data I want to obtain (highlighted in red):

Welcome @DevGisoun to the Influx forum.

I do not have a lot of time at the moment to test this, but you should be able to obtain the date/hour when the max value occurred.

The query below is from another example that obtains the time value. Could you try inserting this line just before your yield statement?

|> keep(columns: [“_time”])

Full query:

data=from(bucket: "TEMP_DATA")
  |> range(start: -2m)
  |> filter(fn: (r) => r["_measurement"] == "placeholder_1")
  |> group()
  |> last()
  |> keep(columns: ["_time"])
  |> limit(n:1)
  |> yield(name: "timestamp")

Here is a similar query that I created for Grafana to find the max date & time for each region, which uses the same idea as above.

1 Like