# Calculate given time difference

**URL:** <https://community.grafana.com/t/calculate-given-time-difference/55506>\
**Category:** InfluxDB\
**Created:** [October 28, 2021, 9:34am UTC](https://community.grafana.com/t/calculate-given-time-difference/55506 "2021-10-28T09:34:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ogoku](https://avatars.discourse-cdn.com/v4/letter/o/58956e/32.png) [@ogoku](https://community.grafana.com/u/ogoku)\
**Post date:** [October 28, 2021, 9:34am UTC](https://community.grafana.com/t/calculate-given-time-difference/55506/1 "2021-10-28T09:34:37Z")

</div>

Hi there,

I would like to divide the measurement series into 8h segments if the selected time range is greater than or equal to 8h. With increase() I “reset” the value at a given Time so I am also using the offset parameter in the window() function.

|\> window(every: 8h, offset: 4h)  
|\> increase()

When the selected time range is less than 8h, I do not need to window() and increase() the series.

So my question is, how can I calculate the difference from v.timeRangeStart and v.timeRangeStop to make an if-else statement in the query?

```auto
dataset = from(bucket: "statData")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> filter(fn: (r) => r["_measurement"] == "counterData")
    |> filter(fn: (r) => r["_field"] == "counter")
   
if (d >= 8h) then
  dataset
    |> window(every: 8h, createEmpty: false, offset: 4h)
    |> increase()
else
  dataset
   |> yield()

```

---

<div class="post-metadata">

**Author:** ![ogoku](https://avatars.discourse-cdn.com/v4/letter/o/58956e/32.png) [@ogoku](https://community.grafana.com/u/ogoku)\
**Post date:** [October 28, 2021, 4:32pm UTC](https://community.grafana.com/t/calculate-given-time-difference/55506/2 "2021-10-28T16:32:34Z")

</div>

Solution was:

```auto
windowPeriod = int(v: v.windowPeriod)
if (windowPeriod >= int(v: 8h)) then

```
