Weekly Flux Query shows full 121 kg for current week (should be ~12 kg)

Hello,

I have this code and get this result:

import "timezone"
import "date"
option location = timezone.location(name: "Europe/Vienna")

startExt = date.sub(d: 6mo, from: v.timeRangeStop)

from(bucket: "bucketA")
  |> range(start: startExt, stop: v.timeRangeStop)
  |> filter(fn: (r) => r._measurement == "Haus")
  |> filter(fn: (r) => r.Bereich == "Pellets")
  |> filter(fn: (r) => r.Sensor == "Status")
  |> filter(fn: (r) => r._field == "Resetierbarer_kg-Z_hler___kg_")
  |> aggregateWindow(every: duration(v: "${per}"), fn: last, createEmpty: true)
  |> timeShift(duration: -1s, columns: ["_time"])
  |> difference(nonNegative: true, columns: ["_value"])
  |> yield(name: "last")

The issue:

When ${per} = 1w, the query shows 121 kg for the current week (KW 43).

But today is Monday, the first day of the week — the real value should be only around 12 kg.

If I switch ${per} to 1d, the daily value looks correct (12 kg).

In Grafana, my week start is already set to Monday.

So my questions are:

  • Does 1w in Flux mean “7 days from now” rather than a calendar week (Mon–Sun)?

  • Is the current, unfinished week being ignored or merged with the previous one?

  • How can I make the aggregation follow real calendar weeks (Mon–Sun)?

Thanks for your help!

guess I found now the solution. I need to set the offset manually for week start Monday:

|> aggregateWindow(every: duration(v: “${per}”), offset: -3d, fn: last, createEmpty: true)

Hi @hanhoe

Looks like you got it. I discovered this years ago and built this example in the Grafana demo site to illustrate: