# Integral Function not returning expected

**URL:** <https://community.influxdata.com/t/integral-function-not-returning-expected/25061>\
**Category:** Fluxlang\
**Tags:** influxdb, time-series, time, query, flux\
**Created:** [May 16, 2022, 8:58pm UTC](https://community.influxdata.com/t/integral-function-not-returning-expected/25061 "2022-05-16T20:58:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ticchioned](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/ticchioned/32/9856_2.png) [@ticchioned](https://community.influxdata.com/u/ticchioned)\
**Post date:** [May 16, 2022, 8:58pm UTC](https://community.influxdata.com/t/integral-function-not-returning-expected/25061/1 "2022-05-16T20:58:35Z")

</div>

I have a flux query that gets the integral of some data and I noticed an inconsistency with the function that I would like explained if at all possible. For example, lets say I want to get the integral over a 15 min interval:

```auto
    _value _start _stop _time
23706.40067 2022-03-08T19:15:00.000Z	2022-03-08T19:30:00.000Z	2022-03-08T19:15:00.000Z
23706.26172 2022-03-08T19:15:00.000Z	2022-03-08T19:30:00.000Z	2022-03-08T19:15:56.614Z
23707.85742 2022-03-08T19:15:00.000Z	2022-03-08T19:30:00.000Z	2022-03-08T19:27:56.617Z

```

Influx will return what I expect, 21,336,427,687,765,830 (when integral unit = ns). But lets say I attempt to get the integral of this data:

```auto
  _value _start _stop _time
23707.33292 2022-03-08T21:00:00.000Z	2022-03-08T21:15:00.000Z	2022-03-08T21:00:00.000Z
23707.20898 2022-03-08T21:00:00.000Z	2022-03-08T21:15:00.000Z	2022-03-08T21:03:59.521Z
23707.80664 2022-03-08T21:00:00.000Z	2022-03-08T21:15:00.000Z	2022-03-08T21:08:54.723Z
0 2022-03-08T21:00:00.000Z	2022-03-08T21:15:00.000Z	2022-03-08T21:08:55.683Z
0 2022-03-08T21:00:00.000Z	2022-03-08T21:15:00.000Z	2022-03-08T21:09:04.323Z
23707.80664 2022-03-08T21:00:00.000Z	2022-03-08T21:15:00.000Z	2022-03-08T21:09:05.283Z
23707.47379 2022-03-08T21:15:00.000Z	2022-03-08T21:30:00.000Z	2022-03-08T21:15:00.000Z

```

This interval will return the value 1,574,762,662,963,110,700 which is a bigger number than I was expecting… I was expecting a number similar to the data set above. It seems as if the 0’s are causing this. When I increase those 0’s, I begin to see the number I would like to achieve. Why does Influx do this and is there anyway to prevent this from happening? What is going on in the integral function???

---

<div class="post-metadata">

**Author:** ![ticchioned](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/ticchioned/32/9856_2.png) [@ticchioned](https://community.influxdata.com/u/ticchioned)\
**Post date:** [May 17, 2022, 3:44pm UTC](https://community.influxdata.com/t/integral-function-not-returning-expected/25061/2 "2022-05-17T15:44:13Z")

</div>

I would like to emphasize what’s going on here by sharing a little bit more about the issue I am experiencing regarding Influx’s built in Integral function. In the picture attached, I have quite a bit going on. I am trying to compare how another historian is doing their integrals vs how Influx is doing it. While I’m not expecting Influx to work exactly the same, the differences in my opinion are quite shocking.

In the attached image, I pulled raw data down from historian number 1 (the historian I am using to compare to Influx) and I also pulled raw data down from Influx. I chose a 15 min interval and the data is relatively the same between historians (the data I have in Influx is slightly compressed). I then manually calculated the integral in both days and nanoseconds. As shown in Step 5, the results are just about the same, only a 0.00033% difference was shown between the two manual calculations.

I then preformed a server side integral on both data historians. The first historian, returned a value similar to both manually calculated integrals whereas influx returns a value that is not even close… The value Influx returns has a percent difference of 194.71% when compared with the manual calculations.

 ![Integral Compare Pic](https://us1.discourse-cdn.com/flex023/uploads/influxdata/original/2X/0/03e6e588e6eefe6ee0e21abcf8a8f58e585210c7.png)

Here is the flux I am using to return the Integral in nanoseconds:

```auto
offset=duration(v: string(v: uint(v: 2022-03-08T20:00:00.0000000Z)-uint(v:date.truncate(t: 2022-03-08T20:00:00.0000000Z, unit: 900s))) + "ns")

from(bucket: "MyBucket")
    |> range(start: 2022-03-08T20:00:00.0000000Z, stop: 2022-03-08T22:00:00.0000000Z)
    |> filter(fn: (r) => r._measurement == "Air_Temp" and r._field == "Value")
    |> window(every: 15m, offset: offset, createEmpty: true)
    |> integral(interpolate: "linear", unit: 1ns, timeColumn: "_time")

```

What is going on here and why is there such a vast difference between my manual calculation and what Influx is doing to calculate these values? Is there a way to stay consistent with my manual calculations while still using the server side integral flux function?

---

<div class="post-metadata">

**Author:** ![Anaisdg](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/anaisdg/32/6401_2.png) [@Anaisdg](https://community.influxdata.com/u/Anaisdg)\
**Post date:** [May 23, 2022, 6:32pm UTC](https://community.influxdata.com/t/integral-function-not-returning-expected/25061/3 "2022-05-23T18:32:41Z")

</div>

Hello @ticchioned,  
Thanks for sharing the detailed explanation.  
I don’t know why this behavior is happening.  
I created this issue:

> <https://github.com/influxdata/flux/issues/4782>
>
> https://community.influxdata.com/t/integral-function-not-returning-expected/2506…1
> Thank you
