timedMovingAverage() function

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)

MOVING_AVERAGE()

This documentation is open source. See a typo? Please, open an issue.


Need help getting up and running? Get Support