timedMovingAverage() function

Warning! This page documents an earlier version of Flux, which is no longer actively developed. Flux v0.50 is the most recent stable version of Flux.

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

Function type: Aggregate

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

Parameters

every

The frequency of time windows.

Data type: Duration

period

The length of each averaged time window. A negative duration indicates start and stop boundaries are reversed.

Data type: Duration

column

The column used to compute the moving average. Defaults to "_value".

Data type: String

Examples

Calculate a seven day moving average every day
from(bucket: "telegraf/autogen"):
  |> range(start: -7y)
  |> filter(fn: (r) =>
    r._measurement == "financial" and
    r._field == "closing_price"
  )
  |> timedMovingAverage(every: 1y, period: 5y)
Calculate a five year moving average every year
from(bucket: "telegraf/autogen"):
  |> range(start: -50d)
  |> filter(fn: (r) =>
    r._measurement == "financial" and
    r._field == "closing_price"
  )
  |> timedMovingAverage(every: 1d, period: 7d)

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)