window() function
The window()
function groups records based on a time value.
The function calculates time windows and stores window bounds in the _start
and _stop
columns.
_start
and _stop
values are assigned to rows based on the _time
value.
A single input row may be placed into zero or more output tables depending on
the parameters passed into the window()
function.
window(
every: 5m,
period: 5m,
offset: 12h,
timeColumn: "_time",
startColumn: "_start",
stopColumn: "_stop",
location: "UTC",
createEmpty: false,
)
Parameters
Calendar months and years
every
, period
, and offset
support all valid duration units,
including calendar months (1mo
) and years (1y
).
Window by week
When windowing by week (1w
), weeks are determined using the
Unix epoch (1970-01-01T00:00:00Z UTC). The Unix epoch was on a Thursday, so
all calculated weeks begin on Thursday.
every
Duration of time between windows.
Defaults to period
value.
period
Duration of the window.
Period is the length of each interval.
It can be negative, indicating the start and stop boundaries are reversed.
Defaults to every
value.
offset
Offset is the duration by which to shift the window boundaries.
It can be negative, indicating that the offset goes backwards in time.
Defaults to 0, which will align window end boundaries with the every
duration.
timeColumn
The column containing time.
Defaults to "_time"
.
startColumn
The column containing the window start time.
Defaults to "_start"
.
stopColumn
The column containing the window stop time.
Defaults to "_stop"
.
location
Location used to determine timezone.
Default is the location
option.
Flux uses the timezone database (commonly referred to as “tz” or “zoneinfo”) provided by the operating system.
createEmpty
Specifies whether empty tables should be created.
Defaults to false
.
tables
Input data.
Default is piped-forward data (<-
).
Examples
The following examples use data provided by the sampledata
package
to show how window()
transforms data.
- Window data into 30 second intervals
- Window every 20 seconds covering 40 second periods
- Window by calendar month
Window data into 30 second intervals
import "sampledata"
data = sampledata.int()
|> range(start: sampledata.start, stop: sampledata.stop)
data
|> window(every: 30s)
Window every 20 seconds covering 40 second periods
import "sampledata"
data = sampledata.int()
|> range(start: sampledata.start, stop: sampledata.stop)
data
|> window(every: 20s, period: 40s)
Window by calendar month
The following example uses generate.from
to illustrate windowing by calendar month.
import "generate"
timeRange = {start: 2021-01-01T00:00:00Z, stop: 2021-04-01T00:00:00Z}
data = generate.from(count: 6, fn: (n) => n + n, start: timeRange.start, stop: timeRange.stop)
|> range(start: timeRange.start, stop: timeRange.stop)
data
|> window(every: 1mo)
Was this page helpful?
Thank you for your feedback!
Support and feedback
Thank you for being part of our community! We welcome and encourage your feedback and bug reports for Flux and this documentation. To find support, use the following resources:
InfluxDB Cloud customers can contact InfluxData Support.