Window by specific irregular times and periods

is there a way to window irregular times and periods.

currently I have:

from(bucket: “test”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: ® => r["_measurement"] == “meas”)
|> filter(fn: ® => r["_field"] == “height”)
|> group(columns: [“batch”])

which returns a tub with may measurements for each batch

by design/built-in assumptions for the data:
(1) each batch of measurements starts and stops randomly
(2) only one batch can be actively collecting measurements at a time
(3) after one batch stops measuring and another batch starts measuring you cannot go back to the first batch. therefore batches cannot have overlapping windows

the question:
How can I get the average measurement for each batch? I’d like to essentially “window” the data where each window is a single batch. each batch’s start and end time is each table’s _start and _stop

Hello @Addison_Williams,
First it’s good to know that you can pass in your own function into the aggregateWindow() function. There might be something there for you to write a custom aggregation.
This is explained in this blog as well:

Depending on your data this might be the best solution.

To achieve this special windowing with real time data I can think of the following approach:

  • Use a task with a map() and conditional query logic to write a unique tag to your data for the duration of the batch.
    For example you could generate a unique tag by simply making the tag the start time of each batch i.e. converting now() to a string
  • Then you could group by unique tag for each batch and calculate the mean.

I hope this helps. Let me know what all I can clarify.

Hello @Addison_Williams,
Something along the lines of what Scott wrote in this thread could also help: