timeWeightedAvg() function

Flux 0.83.0+

The timeWeightedAvg() function outputs the time-weighted average of non-null records in a table as a float. Time is weighted using the linearly interpolated integral of values in the table. timeWeightedAvg() is an aggregate function.

Output data type: Float

timeWeightedAvg(unit: 1m)



(Required) Time duration used when computing the time-weighted average.


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


The following example uses data provided by the sampledata package to show how timeWeightedAvg() transforms data.

import "sampledata"

data = true)
  |> range(start: sampledata.start, stop: sampledata.stop)
  |> fill(usePrevious: true)
  |> unique()

  |> timeWeightedAvg(unit: 1s)

View input and output

Function definition

timeWeightedAvg = (tables=<-, unit) => tables
  |> integral(
    unit: unit,
    interpolate: "linear"
  |> map(fn: (r) => ({
    r with
    _value: (r._value * float(v: uint(v: unit))) / float(v: int(v: r._stop) - int(v: r._start))

Upgrade to InfluxDB Cloud or InfluxDB 2.0!

InfluxDB Cloud and InfluxDB OSS 2.0 ready for production.