Summing values of variables on chart

influx: Influx CLI 2.3.0 (git: 88ba346) build_date: 2022-04-06T19:30:53Z

I have chart with four variables. Each variable have value for the same timestamp. How to draw chart with only sum of four values?

from(bucket: "est")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "balance")
  |> filter(fn: (r) => r["_field"] == "flow1" or r["_field"] == "flow2" or r["_field"] == "flow3" or r["_field"] == "flow4")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> yield(name: "mean")
0	balance		flow1	1.12	2022-11-15T20:51:22.422Z	2022-11-17T20:51:22.422Z	2022-11-15T22:20:00.000Z
0	balance		flow1	0.09	2022-11-15T20:51:22.422Z	2022-11-17T20:51:22.422Z	2022-11-16T22:20:00.000Z
1	balance		flow2	2.03	2022-11-15T20:51:22.422Z	2022-11-17T20:51:22.422Z	2022-11-15T22:20:00.000Z
1	balance		flow2	0.41	2022-11-15T20:51:22.422Z	2022-11-17T20:51:22.422Z	2022-11-16T22:20:00.000Z
2	balance		flow3	2.64	2022-11-15T20:51:22.422Z	2022-11-17T20:51:22.422Z	2022-11-15T22:20:00.000Z
2	balance		flow3	1.15	2022-11-15T20:51:22.422Z	2022-11-17T20:51:22.422Z	2022-11-16T22:20:00.000Z
3	balance		flow4	0.21	2022-11-15T20:51:22.422Z	2022-11-17T20:51:22.422Z	2022-11-15T22:20:00.000Z
3	balance		flow4	0.03	2022-11-15T20:51:22.422Z	2022-11-17T20:51:22.422Z	2022-11-16T22:20:00.000Z

Can you try this?

from(bucket: "est")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "balance")
  |> filter(fn: (r) => r["_field"] == "flow1" or r["_field"] == "flow2" or r["_field"] == "flow3" or r["_field"] == "flow4")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> map(fn: (r) => ({  _value: r.flow1 + r.flow2 + r.flow3 + r.flow4, _time:r._time, _field:"SumOfFlows" }))
  |> yield(name: "sum")

@parom1
Did you get a chance to try the above? Please post back via the forum and if my proposed query worked, please mark as Solution.

1 Like