Output the max between 2 or more measurements

what is the most efficient way, having to measurements (or even more), two output a third measurement which contains only the maximum between the previous two at every time instant?

@vitttosa You have to pivot the values you want to compare into columns so they exist side by side in a row. For example, if you have two fields, fieldA and fieldB, and you want only use the greater of these two values, you’d do the following:

  1. Query both fields.
  2. Pivot the fields into columns in each row.
  3. Use map() to iterate over each row and math.mMax() to return the greater of two fields.
import "math"

from(bucket: "example-bucket")
    |> range(start: -1h)
    |> filter(fn: (r) => r._measurement == "example-measurement")
    |> filter(fn: (r) => r._field == "fieldA" or r._field == "fieldB")
    |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")
    |> map(fn: (r) => ({r with _value: math.mMax(x: r.fieldA, y: r.fieldB)}))
    |> drop(columns: ["fieldA", "fieldB"])
1 Like