Comparing different years in one graph from one data source

For me the following solution works well using Grafana v10.1.5 and InfluxDB v2.7.3.

I have added the InfluxDB with Flux as Query Language and then added 2 Queries in Grafana, one for the current year, one for the previous year (see dynamic range) using the timeShift() function:

Query A:

from(bucket: "openhab-rpi01")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "WS_HouseRoof_Temp")
  |> filter(fn: (r) => r["_field"] == "value")
  |> set(key: "_field", value: "Außentemperatur")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> yield(name: "mean")

Query B:

import "date"
from(bucket: "openhab-rpi01")
  |> range(start: date.add(d: -1y, to: v.timeRangeStart), stop: date.add(d: -1y, to: v.timeRangeStop))
  |> filter(fn: (r) => r["_measurement"] == "WS_HouseRoof_Temp")
  |> filter(fn: (r) => r["_field"] == "value")
  |> set(key: "_field", value: "Außentemperatur Vorjahr")
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> timeShift(duration: 1y, columns: ["_start", "_stop", "_time"])
  |> yield(name: "mean")

(Optional) Visual Tweaks:

The result looks like this:

3 Likes