# Create a "running integral" of values from another measurement

**URL:** <https://community.influxdata.com/t/create-a-running-integral-of-values-from-another-measurement/26135>\
**Category:** Fluxlang\
**Created:** [August 16, 2022, 7:33pm UTC](https://community.influxdata.com/t/create-a-running-integral-of-values-from-another-measurement/26135 "2022-08-16T19:33:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jens](https://avatars.discourse-cdn.com/v4/letter/j/d2c977/32.png) [@Jens](https://community.influxdata.com/u/Jens)\
**Post date:** [August 16, 2022, 7:33pm UTC](https://community.influxdata.com/t/create-a-running-integral-of-values-from-another-measurement/26135/1 "2022-08-16T19:33:30Z")

</div>

In InfluxDB 2, I have my battery charge / discharge power (in Watt) as follows:

 ![image](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/e/ed1b4a1a30516381601d6d10e860f475a1f0520e.png)

As you can see, the battery supplied power from 20:00 yesterday to ~7:15 today, then a few short bursts during the morning, and was charged again (drain \<0) between 13:00 and 15:00.

I would like to add the battery power (in Wh) to this graph. To do this I would have to calculate a “running integral” from the start of the graph to each point in time, summing up the area below the graph. I know that the drain or charge can be off by a constant (as all integrals can) but I can live with that.

I have found the `integral()` function but this always returns only a single value.

How do I create a “running integral” which returns just as many datapoints as the input function?

Thank you!  
Jens

---

<div class="post-metadata">

**Author:** ![Patrick808](https://avatars.discourse-cdn.com/v4/letter/p/b487fb/32.png) [@Patrick808](https://community.influxdata.com/u/Patrick808)\
**Post date:** [March 3, 2023, 3:40pm UTC](https://community.influxdata.com/t/create-a-running-integral-of-values-from-another-measurement/26135/2 "2023-03-03T15:40:54Z")

</div>

Came to ask the same question. The only solution I can think of is to map a field with a period integral (per record), and then run a cumulativeSum() on that field.

This is my sloppy solution:

```auto
data = from(bucket: "Iotabucket2")
  |> range(start: -2h, stop: -115m)
  |> filter(fn: (r) => r["ct"] == "AC3")
wattS = data
  |> aggregateWindow(fn: mean, every: 1m)
  |> yield(name:"Watts")
kWh = data
  |> aggregateWindow(fn: integral, every: 1m)
  |> cumulativeSum()
  |> map(fn: (r) => ({r with _value: r._value / 60000.0, _field: "kWh", units: "kWh"}))
  |> yield(name: "kWh")

```
