# If cause for range duration change

**URL:** <https://community.influxdata.com/t/if-cause-for-range-duration-change/19900>\
**Category:** InfluxDB 2\
**Created:** [May 14, 2021, 7:50pm UTC](https://community.influxdata.com/t/if-cause-for-range-duration-change/19900 "2021-05-14T19:50:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![srbp](https://avatars.discourse-cdn.com/v4/letter/s/7c8e57/32.png) [@srbp](https://community.influxdata.com/u/srbp)\
**Post date:** [May 14, 2021, 7:50pm UTC](https://community.influxdata.com/t/if-cause-for-range-duration-change/19900/1 "2021-05-14T19:50:45Z")

</div>

dear, please

i have debug dash board, where i use variables to see my

- actual buckets
- all measurements for each bucket
- sample data for each measurement

one of my measurement is conversion table between machine\_id to human\_readable. as this i only import once it can be very old. so i am looking for function where i detect such a measurement and then want to shift v.timeRangeStart to -1y

time\_range = (measurement) =\>  
measurement  
//|\> filter(fn: (r) =\> if measurement == “ttn\_2\_machines” then string(v: duration(v: -1y))  
//else string(v: duration(v: v.timeRangeStart)))  
[//v.timeRangeStart](https://v.timeRangeStart)  
|\> filter(fn: (r) =\> if measurement == “ttn\_2\_machines” then -1y  
else v.timeRangeStart)  
v.timeRangeStart

tr = time\_range(measurement: “\_measurement”)

from(bucket: v.bucket)  
|\> range(start: tr)  
|\> filter(fn: (r) =\> r[“\_measurement”] == v.measurement)  
|\> map(fn: (r) =\> ({ r with value: r.\_value}))  
|\> drop(columns:[“\_start”, “\_stop”, “host”]) // “\_measurement”])  
|\> sort(columns: [“\_time”], desc:true)  
|\> limit(n:10)

thank you for pointing to right place, pavel

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [May 21, 2021, 7:09pm UTC](https://community.influxdata.com/t/if-cause-for-range-duration-change/19900/2 "2021-05-21T19:09:06Z")

</div>

Hello @srbp,  
I’m having some trouble understanding what it is that you’re trying to accomplish, but I’ll try to point you in the right direction.

1. You can apply multiple time range functions to your data.  
For example

```auto
from(bucket: "system")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "cpu")
  |> filter(fn: (r) => r["_field"] == "usage_system" and exists r["_value"])
  |> range(start: v.timeRangeStart)

```

You can use `exists` to check whether something exists first.

> **[Use Flux to check if a value exists | InfluxDB Cloud (TSM) Documentation](https://docs.influxdata.com/influxdb/cloud/query-data/flux/exists/)**
>
> Use the Flux exists operator to check if a record contains a key or if that key’s value is null.

You can also filter for data from a point in time:

```auto
cutoff = <some timestamp>
   |> filter(fn: (r) => r._time >= cutoff)

```

You can also return a timestamp from a previous query with `getRecord` and use that in a subsequent query [getRecord() function | Flux Documentation](https://docs.influxdata.com/influxdb/cloud/reference/flux/stdlib/built-in/transformations/stream-table/getrecord/)

Does anything here help? If not, can you describe in a little more detail what you’re trying to do?

Finally, below is a section from [Top 5 Hurdles for Intermediate Flux Users and Resources for Optimizing Flux | InfluxData](https://www.influxdata.com/blog/top-5-hurdles-for-intermediate-flux-users-and-resources-for-optimizing-flux/). It’s a list of flux functions you can use to manipulate time:

> Functions to [manipulate timestamps](https://docs.influxdata.com/influxdb/v2.0/query-data/flux/manipulate-timestamps/): Time series data is difficult to work with. Flux includes several functions to work with time. However, a couple of these functions are spread out between various community package contributions and Flux packages within the documentation. A consolidated list of all of these functions provide you with a deeper understanding of how you can use Flux to work with time. The following functions allow you to manipulate your timestamps and execute a variety of time series transformations. Including:
> 
> - [hourSelection()](https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/hourselection/): select data between specific parts of the day.
> - [duration()](https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/type-conversions/duration/): convert a timestamp to a duration in terms of seconds, minutes, hours, etc.
> - [events.Duration()](https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/contrib/events/duration/): calculate the duration between events
> - [now()](https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/built-in/misc/now/): return the current time
> - [system.time()](https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/system/time/): return the current time of the system
> - [time()](https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/type-conversions/time/): convert a Unix nanosecond timestamp to an RFC3339 timestamp
> - [uint()](https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/type-conversions/uint/): convert RFC3339 timestamp to a Unix nanosecond timestamp
> - [truncateTimeColumn()](https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/built-in/transformations/truncatetimecolumn/): round or truncate an entire column to a specific timestamp unit
> - [date.truncate()](https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/date/truncate/): round or truncate data down to a specific timestamp unit.
> - [experimental.addDuration()](https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/experimental/addduration/)\*: add timestamps to each other
> - [experimental.subDuration()](https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/experimental/subduration/)\*: subtract timestamps from each other
> - [experimental.alignTime()](https://docs.influxdata.com/influxdb/v2.0/reference/flux/stdlib/experimental/aligntime/)\*: compare data across a window; i.e., week over week or month over month.

---

<div class="post-metadata">

**Author:** ![srbp](https://avatars.discourse-cdn.com/v4/letter/s/7c8e57/32.png) [@srbp](https://community.influxdata.com/u/srbp)\
**Post date:** [May 23, 2021, 11:02am UTC](https://community.influxdata.com/t/if-cause-for-range-duration-change/19900/3 "2021-05-23T11:02:17Z")

</div>

hallo @Anaisdg , thank you for your answer. i will try to describe again and better:

what i really like with FLUX is, that we can achieve same goal via multiple ways:

- the goal here is to use condition/function to detect static table / measurement: “ttn\_2\_machines” and if True then overcharge the v.timeRangeStart with -3y

- the reason for this is to limit range for sensor data to work with let’s say only -24h or -4d and not -3y

- i have succeed with two tables and union

//static table which can be 2year old  
ttn\_table =  
from(bucket: v.bucket)  
|\> range(start: -1y)  
|\> filter(fn: (r) =\> r["\_measurement"] == v.measurement)  
|\> filter(fn: (r) =\> v.measurement == “ttn\_2\_machines”)  
|\> map(fn: (r) =\> ({ r with value: r.\_value}))  
|\> drop(columns:["\_start", “\_stop”, “host”, “\_field”, “\_measurement”])  
|\> sort(columns: ["\_time"], desc:true)  
|\> limit(n:10)  
|\> group()

//dynamic table incremented every 1m with lot’s of sensor data  
all\_data\_tables =  
from(bucket: v.bucket)  
|\> range(start: -1y)  
|\> filter(fn: (r) =\> r["\_measurement"] == v.measurement)  
|\> filter(fn: (r) =\> v.measurement != “ttn\_2\_machines”)  
|\> range(start: v.timeRangeStart)  
|\> map(fn: (r) =\> ({ r with value: r.\_value}))  
|\> drop(columns:["\_start", “\_stop”, “host”]) // “\_measurement”])  
|\> sort(columns: ["\_time"], desc:true)  
|\> limit(n:10)

union(tables: [ttn\_table, all\_data\_tables])

- but i believe there will be more efficient way and also interested if i can use IF to shift duration for RANGE like described in initial question

thank you, pavel

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [June 1, 2021, 3:41pm UTC](https://community.influxdata.com/t/if-cause-for-range-duration-change/19900/4 "2021-06-01T15:41:19Z")

</div>

Hello @srbp,  
I agree that’s what I like about Flux too. I think the solution that I gave you is more efficient, but maybe @scott has a better idea.

---

<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:** [June 1, 2021, 4:30pm UTC](https://community.influxdata.com/t/if-cause-for-range-duration-change/19900/5 "2021-06-01T16:30:24Z")

</div>

You could create a custom function that returns a different dataset based on the selected measurement:

```javascript
getData = (bucket, measurement) => {
  ttn_table =
    from(bucket: bucket)
      |> range(start: -2y)
      |> filter(fn: (r) => r["_measurement"] == measurement)
      |> filter(fn: (r) => measurement == "ttn_2_machines")
      |> drop(columns: ["_start", "_stop", "host", "_field", "_measurement"])
      |> map(fn: (r) => ({ r with value: r._value}))
      |> sort(columns: ["_time"], desc:true)
      |> limit(n:10)
      |> group()

  dynamic_tables =
    from(bucket: bucket)
      |> range(start: timeRangeStart)
      |> filter(fn: (r) => r["_measurement"] == measurement)
      |> filter(fn: (r) => measurement != "ttn_2_machines")
      |> map(fn: (r) => ({ r with value: r._value}))
      |> drop(columns:["_start", "_stop", "host"]) // "_measurement"])
      |> sort(columns: ["_time"], desc:true)
      |> limit(n:10)
  
  returnData = if measurement == "ttn_2_machines" then ttn_table else dynamic_tables

  return returnData
}

getData(bucket: v.bucket, measurement: v.measurement)

```

---

<div class="post-metadata">

**Author:** ![srbp](https://avatars.discourse-cdn.com/v4/letter/s/7c8e57/32.png) [@srbp](https://community.influxdata.com/u/srbp)\
**Post date:** [June 1, 2021, 7:17pm UTC](https://community.influxdata.com/t/if-cause-for-range-duration-change/19900/6 "2021-06-01T19:17:44Z")

</div>

this is great, very nice solution. thank you @scott and @anaisdg

pavel
