# How can I plot the difference value(now) - value(24h ago) in Flux?

**URL:** https://community.influxdata.com/t/how-can-i-plot-the-difference-value-now-value-24h-ago-in-flux/22304
**Category:** InfluxDB 2
**Tags:** time, query, flux
**Created:** [October 28, 2021, 2:19pm UTC](https://community.influxdata.com/t/how-can-i-plot-the-difference-value-now-value-24h-ago-in-flux/22304 "2021-10-28T14:19:53Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![mihnea17](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@mihnea17](https://community.influxdata.com/u/mihnea17)
#### Post date: [October 28, 2021, 2:19pm UTC](https://community.influxdata.com/t/how-can-i-plot-the-difference-value-now-value-24h-ago-in-flux/22304/1 "2021-10-28T14:19:53Z")

</div>

Hello,

I have the following Flux query that plots the value read from a sensor over time:

```auto
from(bucket: "adeunis")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "mqtt_consumer")
  |> filter(fn: (r) => r["topic"] == "adeunis")
  |> filter(fn: (r) => r["_field"] == "temperatures_0_value")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> yield(name: "mean")

```

What I would like to do is to write a query that returns the difference between the last read value and a value from yesterday (let’s say yesterday @ 8.00 PM).

Could this be possible using Flux, or should I write a script?

---

<div class="post-metadata">

### Author: ![FixTestRepeat](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/fixtestrepeat/32/5208_2.png) [@FixTestRepeat](https://community.influxdata.com/u/FixTestRepeat)
#### Post date: [October 28, 2021, 7:24pm UTC](https://community.influxdata.com/t/how-can-i-plot-the-difference-value-now-value-24h-ago-in-flux/22304/2 "2021-10-28T19:24:42Z")

</div>

Pretty sure this recipe will work for you.

> **[Compare the last measurement to a mean stored in another bucket | InfluxDB...](https://docs.influxdata.com/influxdb/v2.0/query-data/common-queries/compare-values/)**
>
> Compare the value from the latest point to an average value stored in another bucket. This is useful when using the average value to calculate a threshold check.

(Untested , but this should help get you started)

```auto
CurrentRange = from(bucket: "adeunis")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "mqtt_consumer")
  |> filter(fn: (r) => r["topic"] == "adeunis")
  |> filter(fn: (r) => r["_field"] == "temperatures_0_value")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> yield(name: "mean")

PriorRange = from(bucket: "adeunis")
  |> range(start: v.timeRangeStart - 24h, stop: v.timeRangeStop - 24h)
  |> filter(fn: (r) => r["_measurement"] == "mqtt_consumer")
  |> filter(fn: (r) => r["topic"] == "adeunis")
  |> filter(fn: (r) => r["_field"] == "temperatures_0_value")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> yield(name: "mean")

# then apply the join per the example. Something similar to this but will need tweaking probably 

join(tables: {curr : CurrentRange, prior: PriorRange}, on: ["topic"])
  |> map(fn: (r) => ({r with deviation: r._value_curr - r._value_prior}))

```

You will probably need to play with the join (eg on topic, and the tabling naming, and/or the map that calculates the difference

---

<div class="post-metadata">

### Author: ![mihnea17](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@mihnea17](https://community.influxdata.com/u/mihnea17)
#### Post date: [November 1, 2021, 9:42am UTC](https://community.influxdata.com/t/how-can-i-plot-the-difference-value-now-value-24h-ago-in-flux/22304/3 "2021-11-01T09:42:50Z")

</div>

I will try this. Thanks!

---

<div class="post-metadata">

### Author: ![mihnea17](https://avatars.discourse-cdn.com/v4/letter/m/e8c25b/32.png) [@mihnea17](https://community.influxdata.com/u/mihnea17)
#### Post date: [November 2, 2021, 3:03pm UTC](https://community.influxdata.com/t/how-can-i-plot-the-difference-value-now-value-24h-ago-in-flux/22304/4 "2021-11-02T15:03:35Z")

</div>

Apparently, Flux has the difference() function. This, used with an agreggateWindow(every 24h), works pretty well for me.

> **[Query InfluxDB with Flux | InfluxDB Cloud Documentation](https://docs.influxdata.com/influxdb/cloud/query-data/get-started/query-influxdb/)**
>
> Learn the basics of using Flux to query data from InfluxDB.

> **[difference() function | Flux 0.x Documentation](https://docs.influxdata.com/flux/v0.x/stdlib/universe/difference/)**
>
> difference() returns the difference between subsequent values.

---

<div class="post-metadata">

### Author: ![FixTestRepeat](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/fixtestrepeat/32/5208_2.png) [@FixTestRepeat](https://community.influxdata.com/u/FixTestRepeat)
#### Post date: [November 2, 2021, 11:04pm UTC](https://community.influxdata.com/t/how-can-i-plot-the-difference-value-now-value-24h-ago-in-flux/22304/5 "2021-11-02T23:04:24Z")

</div>

Thanks for posting the final solution you ended up with 👍

---

<div class="post-metadata">

### Author: ![worogko.roman](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/worogko.roman/32/14990_2.png) [@worogko.roman](https://community.influxdata.com/u/worogko.roman)
#### Post date: [January 26, 2024, 6:27am UTC](https://community.influxdata.com/t/how-can-i-plot-the-difference-value-now-value-24h-ago-in-flux/22304/6 "2024-01-26T06:27:24Z")

</div>

What would be the final solution that uses difference() anyway?
