# Dynamically adjust values to currently selected range

**URL:** <https://community.grafana.com/t/dynamically-adjust-values-to-currently-selected-range/75041>\
**Category:** InfluxDB\
**Tags:** time-selection, influxdb, bar-chart\
**Created:** [October 28, 2022, 6:23pm UTC](https://community.grafana.com/t/dynamically-adjust-values-to-currently-selected-range/75041 "2022-10-28T18:23:19Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![vo5tr0](https://avatars.discourse-cdn.com/v4/letter/v/8491ac/32.png) [@vo5tr0](https://community.grafana.com/u/vo5tr0)\
**Post date:** [October 28, 2022, 6:23pm UTC](https://community.grafana.com/t/dynamically-adjust-values-to-currently-selected-range/75041/1 "2022-10-28T18:23:19Z")

</div>

Hey 🙂

I have this query right now:

```auto
from(bucket: "iobroker")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "sourceanalytix.0.shelly __1__ shellyplus1pm#7c87ce64d6d0#1 __Relay0__ Energy.cumulativeReading")
  |> filter(fn: (r) => r["_field"] == "value")
  |> aggregateWindow(every: 30m, fn:last)
  |> difference()
  |> yield(name: "mean")

```

It will give me a bar chart with the produced kwh in a 30 minute interval.

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/5/9/594a34bd8e15f128beca2daf831f1a180b6b5de2.png)

This works very well in a max 7 day range. But what if I want to change the dashboard to let’s say 6 months? Is there any way to dynamically adjust the “every: 30m” value to the current range?

I have really no idea yet how the flux language works… But I could think of something like this:

```auto
if range() <= 7d then 
aggregateWindow(every: 30m, fn:last)
else
aggregateWindow(every: 1d, fn:last)

```

Is that possible somehow?

---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [October 28, 2022, 7:19pm UTC](https://community.grafana.com/t/dynamically-adjust-values-to-currently-selected-range/75041/2 "2022-10-28T19:19:03Z")

</div>

> [@vo5tr0](#):
>
> But what if I want to change the dashboard to let’s say 6 months?

hello,

Where would you be changing this 6 months range?

---

<div class="post-metadata">

**Author:** ![vo5tr0](https://avatars.discourse-cdn.com/v4/letter/v/8491ac/32.png) [@vo5tr0](https://community.grafana.com/u/vo5tr0)\
**Post date:** [October 28, 2022, 7:24pm UTC](https://community.grafana.com/t/dynamically-adjust-values-to-currently-selected-range/75041/3 "2022-10-28T19:24:12Z")

</div>

With the time range picker of my dashboard

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/e/f/efc61adcf0d652d23c171bd1668425958383cd00.png)

This is how it will look with a selected range of 7 days (Every: 30m):

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/1/9/19d00680fa1b863ba5d9a928480f5680da61396a.png)

I want the query to automatically switch to something like that (Every: 1d):

 ![image](https://us1.discourse-cdn.com/grafana/original/3X/9/4/946db0720dcd76d87f35e2e7709452d26649bd0d.png)

---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [October 28, 2022, 7:47pm UTC](https://community.grafana.com/t/dynamically-adjust-values-to-currently-selected-range/75041/4 "2022-10-28T19:47:02Z")

</div>

what if they select date ranges from here?

![image](https://us1.discourse-cdn.com/grafana/original/3X/1/f/1fa88b998c7723428a79cf383ba63654e6ad53a4.png)

---

<div class="post-metadata">

**Author:** ![vo5tr0](https://avatars.discourse-cdn.com/v4/letter/v/8491ac/32.png) [@vo5tr0](https://community.grafana.com/u/vo5tr0)\
**Post date:** [October 28, 2022, 8:00pm UTC](https://community.grafana.com/t/dynamically-adjust-values-to-currently-selected-range/75041/5 "2022-10-28T20:00:47Z")

</div>

Doesn’t matter.  
I want to adjust my query as soon as the selected range is \>7 days 😃

---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [October 28, 2022, 8:59pm UTC](https://community.grafana.com/t/dynamically-adjust-values-to-currently-selected-range/75041/6 "2022-10-28T20:59:03Z")

</div>

😆

so let’s start with what the doc says and also those grafana global variables

```auto
color = if code == 0 then "green" else if code == 1 then "yellow" else "red"

```

> **[Expressions | Flux 0.x Documentation](https://docs.influxdata.com/flux/v0.x/spec/expressions/#conditional-expressions)**
>
> An expression specifies the computation of a value by applying the operators and functions to operands.

> **[Variables | Flux 0.x Documentation](https://docs.influxdata.com/flux/v0.x/spec/variables/)**
>
> Flux variables hold values. A variable can only hold values defined by its type.

Now let’s try capturing the time values from grafana in flux

---

<div class="post-metadata">

**Author:** ![yosiasz](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/yosiasz/32/45847_2.png) [@yosiasz](https://community.grafana.com/u/yosiasz)\
**Post date:** [October 28, 2022, 10:10pm UTC](https://community.grafana.com/t/dynamically-adjust-values-to-currently-selected-range/75041/7 "2022-10-28T22:10:31Z")

</div>

Not sure how you can capture global variales such as $\_\_timeFilter ☹

BUt you can capture your own variables

```auto
import "array"
filter = "$boojie"

a = [
    "1",
    "2",
    "3",
    "4",
    filter,
]
b = a |> array.map(fn: (x) => ({_value: x}))
array.from(rows: b)

```

---

<div class="post-metadata">

**Author:** ![vo5tr0](https://avatars.discourse-cdn.com/v4/letter/v/8491ac/32.png) [@vo5tr0](https://community.grafana.com/u/vo5tr0)\
**Post date:** [November 5, 2022, 1:04pm UTC](https://community.grafana.com/t/dynamically-adjust-values-to-currently-selected-range/75041/8 "2022-11-05T13:04:21Z")

</div>

I came up with something like that now:

```auto
timestart = uint(v: v.timeRangeStart)
timestop = uint(v: v.timeRangeStop)
duration = duration(v:timestop - timestart)

from(bucket: "iobroker")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "sourceanalytix.0.shelly __1__ shellyplus1pm#7c87ce64d6d0#1 __Relay0__ Energy.cumulativeReading")
  |> filter(fn: (r) => r["_field"] == "value")
  |> aggregateWindow(every: if int(v:duration) <= int(v:2d) then 30m else if int(v:duration) <= int(v:7d) then 1h else 1d , fn:last)
  |> difference()
  |> yield(name: "mean")

```

Works perfectly 😍

Maybe I am able to help someone else with these problems:

First error was:

```auto
time is not Subtractable

```

Solution:  
You have to convert the timestamp value to unix time with the uint() function

Second error:

```auto
unsupported binary expression duration < duration

```

Solution:  
To compare two durations (e.g. 1d \< 2d) you have to convert them to a numeric value first.  
int(v:1d) \< int(v:2d)

---

<div class="post-metadata">

**Author:** ![matijapolajnar](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@matijapolajnar](https://community.grafana.com/u/matijapolajnar)\
**Post date:** [January 14, 2024, 8:06pm UTC](https://community.grafana.com/t/dynamically-adjust-values-to-currently-selected-range/75041/10 "2024-01-14T20:06:19Z")

</div>

Great, thank you for this!

I “simplified” it further by computing a window size that always gains 800 records:

```auto
  |> aggregateWindow(every: duration(v:(uint(v: v.timeRangeStop) - uint(v: v.timeRangeStart))/uint(v: 800)), fn:last)
  |> yield(name: "mean")

```
