timedMovingAverage() function

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

*Function type: Transformation*

  1. timedMovingAverage(
  2. every: 1d,
  3. period: 5d,
  4. column="_value"
  5. )

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
  1. from(bucket: "example-bucket"):
  2. |> range(start: -7y)
  3. |> filter(fn: (r) =>
  4. r._measurement == "financial" and
  5. r._field == "closing_price"
  6. )
  7. |> timedMovingAverage(every: 1y, period: 5y)
Calculate a five year moving average every year
  1. from(bucket: "example-bucket"):
  2. |> range(start: -50d)
  3. |> filter(fn: (r) =>
  4. r._measurement == "financial" and
  5. r._field == "closing_price"
  6. )
  7. |> timedMovingAverage(every: 1d, period: 7d)

Function definition

  1. timedMovingAverage = (every, period, column="_value", tables=<-) =>
  2. tables
  3. |> window(every: every, period: period)
  4. |> mean(column:column)
  5. |> duplicate(column: "_stop", as: "_time")
  6. |> window(every: inf)

Related articles