# Query with time range calculated on basis of time returned from previous query

**URL:** <https://community.influxdata.com/t/query-with-time-range-calculated-on-basis-of-time-returned-from-previous-query/19757>\
**Category:** Welcome & Getting Started\
**Tags:** influxdb\
**Created:** [May 6, 2021, 10:43am UTC](https://community.influxdata.com/t/query-with-time-range-calculated-on-basis-of-time-returned-from-previous-query/19757 "2021-05-06T10:43:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Smith1](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/eric_smith1/32/7387_2.png) [@Eric\_Smith1](https://community.influxdata.com/u/Eric_Smith1)\
**Post date:** [May 6, 2021, 10:43am UTC](https://community.influxdata.com/t/query-with-time-range-calculated-on-basis-of-time-returned-from-previous-query/19757/1 "2021-05-06T10:43:47Z")

</div>

👋 Folks,

We are using version 1.7 and migrating to 2.0. (Ideally looking for a solution that also works on 1.7, but I am sure that Flux has many options).

I want to define just using influxdb and Grafana a time range for a query where the time range is based on the time field of an influxdb (sub) query.

So in the simplest implementation, I want to find say the time of the last(value) FROM   
Then use that time to calculate n seconds before and after it for `from` and `to` respectively.

In the final implementation, I want to define a Grafana variable with all the times retuned by the query and then generate repeating panels for each time (range).

I hope that that makes sense.

Looking forward to any enlightenment 😊

Best wishes  
Eric

---

<div class="post-metadata">

**Author:** ![tim.hall](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/tim.hall/32/39_2.png) [@tim.hall](https://community.influxdata.com/u/tim.hall)\
**Post date:** [May 19, 2021, 7:51am UTC](https://community.influxdata.com/t/query-with-time-range-calculated-on-basis-of-time-returned-from-previous-query/19757/2 "2021-05-19T07:51:25Z")

</div>

Certainly not possible with InfluxQL. You likely can do this with Flux. I think a simple example of a couple of data points might make this clearer…

Given data like:  
…

I want to do:  
XYZ

And the expected result should be:  
ABC

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [May 19, 2021, 8:23pm UTC](https://community.influxdata.com/t/query-with-time-range-calculated-on-basis-of-time-returned-from-previous-query/19757/3 "2021-05-19T20:23:14Z")

</div>

@Eric_Smith1 This is totally doable in Flux, and I think 1.7 has a new enough version of Flux to make it work.

The following query uses some [timestamp manipulation](https://docs.influxdata.com/influxdb/v2.0/query-data/flux/manipulate-timestamps/) and [string interpolation](https://docs.influxdata.com/influxdb/v2.0/reference/flux/language/string-interpolation/) to generate `from` and `to` timestamps for each row base on `n` seconds added/subtracted to the points `_time` value. I don’t the exact format you’d need to populate a grafana variable, but the `keep()` function at the end keeps only the `from` and `to` columns in the output.

```javascript
n = 2

from(bucket: "example-bucket")
  |> range(start: -1w)
  |> filter(fn: (r) => r._measurement == "example-m" and r._field == "value" )
  |> last()
  |> map(fn: (r) => ({ r with
    from: time(v: int(v: r._time) - int(v: duration(v: "${string(v: n)}s"))),
    to: time(v: int(v: r._time) + int(v: duration(v: "${string(v: n)}s")))  
  }))
|> keep(columns: ["from", "to"])

```
