Documentation

timedMovingAverage() function

Flux 0.36.0+

The timedMovingAverage() function calculates the mean of values in a defined time range at a specified frequency.

timedMovingAverage(
  every: 1d,
  period: 5d,
  column: "_value"
)

For each row in a table, timedMovingAverage() returns the average of the current value and all row values in the previous period (duration). It returns moving averages at a frequency defined by the every parameter.

Each color in the diagram below represents a period of time used to calculate an average and the time a point representing the average is returned. If every = 30m and period = 1h:

0:000:301:001:302:002:303:003:002:302:001:301:000:30

Parameters

every

(Required) Frequency of time windows.

Calendar months and years

every supports all valid duration units, including calendar months (1mo) and years (1y).

Aggregate by week

When aggregating 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.

period

(Required) Length of each averaged time window. A negative duration indicates start and stop boundaries are reversed.

column

Column used to compute the moving average. Defaults to "_value".

tables

Input data. Default is piped-forward data (<-).

Examples

The following examples use generate.from() to generate sample data and illustrate how timedMovingAverage() transforms data.

Calculate a five year moving average every year

import "generate"

timeRange = {start: 2015-01-01T00:00:00Z, stop: 2021-01-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
  |> timedMovingAverage(every: 1y, period: 5y)

View input and output

Calculate a seven day moving average every day

import "generate"

timeRange = {start: 2021-01-01T00:00:00Z, stop: 2021-01-08T00:00:00Z}

data = generate.from(
    count: 7,
    fn: (n) => n + n,
    start: timeRange.start,
    stop: timeRange.stop
  )
  |> range(start: timeRange.start, stop: timeRange.stop)

data
  |> timedMovingAverage(every: 1d, period: 7d)

View input and output

Function definition

timedMovingAverage = (every, period, column="_value", tables=<-) =>
  tables
    |> window(every: every, period: period)
    |> mean(column:column)
    |> duplicate(column: "_stop", as: "_time")
    |> window(every: inf)

Upgrade to InfluxDB Cloud or InfluxDB 2.0!

InfluxDB Cloud and InfluxDB OSS 2.0 ready for production.