Substract two datasets

Hello everyone.

I have a request, that returns two data streams:

import "sampledata"
import "experimental"
import "experimental/aggregate"
        
        yesterday = from(bucket: "jmeter_night")
        |> range(start: experimental.subDuration(from: now(), d: 48h), stop: experimental.subDuration(from: now(), d: 24h))
        |> filter(fn: (r) => r["_measurement"] == "jmeter")
        |> filter(fn: (r) => r["application"] == "my application")
        |> filter(fn: (r) => r["_field"] == "pct95.0")
        |> filter(fn: (r) => r["transaction"] == "all")
        |> group(columns: ["transaction"])
        |> aggregateWindow(every: 10m, fn: mean)
        |> timeShift(duration: 24h, columns: ["_start", "_stop", "_time"])   

        today = from(bucket: "jmeter_night")
        |> range(start: experimental.subDuration(from: now(), d: 24h), stop: now())
        |> filter(fn: (r) => r["_measurement"] == "jmeter")
        |> filter(fn: (r) => r["application"] == "my application")
        |> filter(fn: (r) => r["_field"] == "pct95.0")
        |> filter(fn: (r) => r["transaction"] == "all") 
        |> group(columns: ["transaction"])     
        |> aggregateWindow(every: 10m, fn: mean)  

How can i get the difference between “today” and “yesterday”?
I need to see values = today - yesterday.

@romanandreevich7 This should do it for you:

yesterday =
    from(bucket: "jmeter_night")
        |> range(start: -48h, stop: -24h)
        |> filter(fn: (r) => r["_measurement"] == "jmeter")
        |> filter(fn: (r) => r["application"] == "my application")
        |> filter(fn: (r) => r["_field"] == "pct95.0")
        |> filter(fn: (r) => r["transaction"] == "all")
        |> aggregateWindow(every: 10m, fn: mean)
        |> timeShift(duration: 24h)
        |> set(key: "day", value: "yesterday")
        |> group(columns: ["_time", "_value"], mode: "except")

today =
    from(bucket: "jmeter_night")
        |> range(start: -24h)
        |> filter(fn: (r) => r["_measurement"] == "jmeter")
        |> filter(fn: (r) => r["application"] == "my application")
        |> filter(fn: (r) => r["_field"] == "pct95.0")
        |> filter(fn: (r) => r["transaction"] == "all")
        |> aggregateWindow(every: 10m, fn: mean)
        |> set(key: "day", value: "today")
        |> group(columns: ["_time", "_value"], mode: "except")

union(tables: [yesterday, today])
    |> pivot(rowKey: ["_time"], columnKey: ["day"], valueColumn: "_value")
    |> map(fn: (r) => ({r with _value: r.today - r.yesterday}))
2 Likes

Yes, it works pretty well.
Thanks you a lot!