Query timerange from a influxdb tag and use it as variable for a second query

Setup: infludb v2.7.0, grafana v8.2.5

I have stored time series data (fitness data) in a bucket “test” and I have two tags “start” and “stop” representing the time within the series was recorded (both in format: “2023-05-21T14:00:55.000Z”). This span I want to use in grafana for the timerange of representing the data in the _field values.

Approach is as follows:

a = from(bucket: "test")
  |> range(start: -1y, stop: now())
  |> filter(fn: (r) => r._measurement == "series" and r.id == "175")
  |> keep(columns: ["start"])
  |> distinct()

b = from(bucket: "test")
  |> range(start: -1y, stop: now())
  |> filter(fn: (r) => r._measurement == "series" and r.id == "175")
  |> keep(columns: ["stop"])
  |> distinct()

from(bucket: "test")
  |> range(start: a, stop: b)
  |> filter(fn: (r) => r._measurement == "series" and r.id == "175" and r._field == "frequency")
  |> aggregateWindow(every: 1m, fn: mean, createEmpty: false)

Problem: I cant “extract” the times as string or time, they still are in a table.
Error:

value is not a time, got stream

I have tried also a._start, b._stop with same result:

...
|> range(start: time(v: a._start), stop: time(v: b._stop))
...

I couldn’t find a way to dismantle the table into single values - any hint appreciated.

1 Like

hint:

Referencing “_time” from previous query as “range(start: , stop:now())” for a following query. I can’t get it to work - InfluxDB 2 - InfluxData Community Forums

If I have some free time I will return with the solution, I have done what you are asking multiple times…

OK many thanks, this works for me.

a = from(bucket: "test")
  |> range(start: -1y, stop: now())
  |> filter(fn: (r) => r._measurement == "activities" and r.id == "175" and r._field == "starttime")
  |> findRecord(
        fn: (key) => key._measurement == "activities",
        idx: 0,
    )

b = from(bucket: "test")
  |> range(start: -1y, stop: now())
  |> filter(fn: (r) => r._measurement == "activities" and r.id == "175" and r._field == "stoptime")
  |> findRecord(
        fn: (key) => key._measurement == "activities",
        idx: 0,
    )

from(bucket: "test")
  |> range(start: a._time, stop: b._time)
  |> filter(fn: (r) => r._measurement == "series" and r.id == "175" and r._field == "frequency")
  |> aggregateWindow(every: 1m, fn: mean, createEmpty: false)
1 Like