I think that you can not do that. First, the range() function only accepts one time value at a time and not a list. Also there is no loop in flux, except map(), which could maybe be possible with your data.
And that error happens because your “charging_sessions” is a stream and not a table
I am also very interested in your problem and hope you find a better working solution.
EDIT: These 2 return a single record (like a row in csv) :
|> last()
|> findRecord(fn: (key) => key._field == “fieldName”, idx: 0,)
This 1 returns a table (like a sheet in csv):
|> tableFind(fn: (key) => true)
and findRecord() and tableFind() return only the first “thing” they find with the given filter. So tableFind (key) => true returns first table!
No solution, but a quick and bad work-around
import "array"
testbucket = array.from(rows: // "test bucket"
[
{_time: now(), _field: "session_start_dts", _value: 1646479320},
{_time: now(), _field: "session_start_dts", _value: 1646843541},
{_time: now(), _field: "session_start_dts", _value: 1648457833},
{_time: now(), _field: "session_start_dts", _value: 1651316596},
])
charging_sessions = array.from(rows: // create simulated query result
[
{_time: now(), _field: "session_start_dts", _value: 1646479320},
{_time: now(), _field: "session_start_dts", _value: 1646843541},
{_time: now(), _field: "session_start_dts", _value: 1648457833},
{_time: now(), _field: "session_start_dts", _value: 1651316596},
])
// you only get the !last! entry from the record with last()
stopTime_var = time(v: (charging_sessions |> last(column: "_value") |> findRecord(fn: (key) => true, idx: 0))._value * 1001000000)
// get all data that is older then extracted last() time of from above
testbucket
|> range(start: -90d , stop: stopTime_var)
|> yield(name: "timestamps")
// dirty version of getting your simulated example timestamps out
stopTime_var0 = time(v: (charging_sessions |> tableFind(fn: (key) => true) |> getColumn(column: "_value"))[0] * 1001000000)
stopTime_var1 = time(v: (charging_sessions |> tableFind(fn: (key) => true) |> getColumn(column: "_value"))[1] * 1001000000)
stopTime_var2 = time(v: (charging_sessions |> tableFind(fn: (key) => true) |> getColumn(column: "_value"))[2] * 1001000000)
stopTime_var3 = time(v: (charging_sessions |> tableFind(fn: (key) => true) |> getColumn(column: "_value"))[3] * 1001000000)
testbucket
|> range(start: -90d , stop: stopTime_var0)
|> yield(name: "timestamps0")
testbucket
|> range(start: -90d , stop: stopTime_var1)
|> yield(name: "timestamps1")
testbucket
|> range(start: -90d , stop: stopTime_var2)
|> yield(name: "timestamps2")
testbucket
|> range(start: -90d , stop: stopTime_var3)
|> yield(name: "timestamps3")