# Difference between two values of different measurements

**URL:** <https://community.influxdata.com/t/difference-between-two-values-of-different-measurements/38691>\
**Category:** InfluxDB 2\
**Created:** [October 8, 2024, 2:38pm UTC](https://community.influxdata.com/t/difference-between-two-values-of-different-measurements/38691 "2024-10-08T14:38:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![s118](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@s118](https://community.influxdata.com/u/s118)\
**Post date:** [October 8, 2024, 2:38pm UTC](https://community.influxdata.com/t/difference-between-two-values-of-different-measurements/38691/1 "2024-10-08T14:38:37Z")

</div>

Hello. I have the voltage measurements of 16 cells that make up a battery. I would like to ask a question to represent the difference between the cell with the highest voltage and the cell with the lowest voltage. Do you know how this can be done? Thank you.

---

<div class="post-metadata">

**Author:** ![grant1](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/grant1/32/15107_2.png) [@grant1](https://community.influxdata.com/u/grant1)\
**Post date:** [October 9, 2024, 12:50am UTC](https://community.influxdata.com/t/difference-between-two-values-of-different-measurements/38691/2 "2024-10-09T00:50:46Z")

</div>

Hi @s118 Are you using Flux or InfluxQL? Have you attempted any queries thus far and if so, can you share here (even if not working or giving incorrect results)?

---

<div class="post-metadata">

**Author:** ![s118](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@s118](https://community.influxdata.com/u/s118)\
**Post date:** [October 9, 2024, 3:24am UTC](https://community.influxdata.com/t/difference-between-two-values-of-different-measurements/38691/3 "2024-10-09T03:24:11Z")

</div>

Hi. Obviously I’m referring to Flux (we’re on an influxdb 2 forum).  
If we were talking about a single table I would solve it this way: [How to calculate difference between min and max with Flux?](https://community.influxdata.com/t/how-to-calculate-difference-between-min-and-max-with-flux/17115)  
The problem is that I want the difference between the maximum and minimum of 16 measurements.

---

<div class="post-metadata">

**Author:** ![grant1](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/grant1/32/15107_2.png) [@grant1](https://community.influxdata.com/u/grant1)\
**Post date:** [October 9, 2024, 10:01am UTC](https://community.influxdata.com/t/difference-between-two-values-of-different-measurements/38691/4 "2024-10-09T10:01:43Z")

</div>

> [@s118](#):
>
> Obviously I’m referring to Flux (we’re on an influxdb 2 forum).

That was my guess, I have been wrong to assume that before as [InfluxQL is still being used](https://docs.influxdata.com/influxdb/v2/query-data/influxql/) by people who are on v2.

Since you have only 16 cells, I would think creating a tag called `cell number` would be logical (rather than separate measurements for the 16 cells). Assuming the `measurement` would be called `voltage`, then I think something like this should work (did not have an opportunity to create fake data and test).

```auto
import "math"

data = from(bucket: "s118-bucket")
  |> range(start: -1h) // Adjust the time range as needed
  |> filter(fn: (r) => r["_measurement"] == "voltage")

maxVoltage = data
  |> max(column: "_value")
  |> findRecord(fn: (key) => true)

minVoltage = data
  |> min(column: "_value")
  |> findRecord(fn: (key) => true)

diff = math.abs(x: maxVoltage._value - minVoltage._value)

```

---

<div class="post-metadata">

**Author:** ![s118](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@s118](https://community.influxdata.com/u/s118)\
**Post date:** [October 9, 2024, 2:26pm UTC](https://community.influxdata.com/t/difference-between-two-values-of-different-measurements/38691/5 "2024-10-09T14:26:12Z")

</div>

Tanks. This is the code:

```auto
import "math"
data = from(bucket: "energy")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "VOLTAGE_CELL")
maxVoltage = data
  |> max(column: "_value")
  |> findRecord(
    fn: (key) => true,
    idx: 0,
    )
minVoltage = data
  |> min(column: "_value")
  |> findRecord(
    fn: (key) => true,
    idx: 0,
    )
delta = math.abs(x: maxVoltage._value - minVoltage._value)

```

and this is the error obtained:  
“error in query specification while starting program: this Flux script returns no streaming data. Consider adding a “yield” or invoking streaming functions directly, without performing an assignment”

---

<div class="post-metadata">

**Author:** ![grant1](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/grant1/32/15107_2.png) [@grant1](https://community.influxdata.com/u/grant1)\
**Post date:** [October 9, 2024, 4:50pm UTC](https://community.influxdata.com/t/difference-between-two-values-of-different-measurements/38691/6 "2024-10-09T16:50:46Z")

</div>

> [@s118](#):
>
> Consider adding a “yield” or invoking streaming functions directly, without performing an assignment”

Can you add this to the very end and see what happens?

```auto
|> yield(name: "voltage_difference")

```

---

<div class="post-metadata">

**Author:** ![s118](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@s118](https://community.influxdata.com/u/s118)\
**Post date:** [October 9, 2024, 5:11pm UTC](https://community.influxdata.com/t/difference-between-two-values-of-different-measurements/38691/7 "2024-10-09T17:11:54Z")

</div>

Error:

```auto
expected stream[A] but found float (argument tables)

```

---

<div class="post-metadata">

**Author:** ![grant1](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/grant1/32/15107_2.png) [@grant1](https://community.influxdata.com/u/grant1)\
**Post date:** [October 9, 2024, 5:23pm UTC](https://community.influxdata.com/t/difference-between-two-values-of-different-measurements/38691/8 "2024-10-09T17:23:46Z")

</div>

Hi @s118 Let’s break this down into chunks and try to hone in on the error.

Does this give you a single value?

```auto
data = from(bucket: "energy")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "VOLTAGE_CELL")
maxVoltage = data
  |> max(column: "_value")
  |> findRecord(
    fn: (key) => true,
    idx: 0,
    )
  |> yield(name: "max_value")

```

---

<div class="post-metadata">

**Author:** ![s118](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@s118](https://community.influxdata.com/u/s118)\
**Post date:** [October 9, 2024, 5:37pm UTC](https://community.influxdata.com/t/difference-between-two-values-of-different-measurements/38691/9 "2024-10-09T17:37:47Z")

</div>

Other error:

```auto
error @9:6-9:13: expected stream[A] but found { B with _value: A, _time: time, _stop: time, _start: time, _measurement: string, _field: string, } (record) (argument tables)

```

---

<div class="post-metadata">

**Author:** ![s118](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@s118](https://community.influxdata.com/u/s118)\
**Post date:** [October 16, 2024, 3:53pm UTC](https://community.influxdata.com/t/difference-between-two-values-of-different-measurements/38691/10 "2024-10-16T15:53:09Z")

</div>

any help to solve my query?
