timedMovingAverage() function
The timedMovingAverage()
function calculates the mean of values in a defined time
range at a specified frequency.
Function type: Transformation
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: "example-bucket"):
|> 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: "example-bucket"):
|> 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)
Support and feedback
Thank you for being part of our community! We welcome and encourage your feedback and bug reports for InfluxDB and this documentation. To find support, the following resources are available:
InfluxDB Cloud and InfluxDB Enterprise customers can contact InfluxData Support.