influxdbinfluxql

How to get start of week in Influx QL?


I need to write an flux string in such a way as to get data for a week, but not the |> range(start: -1w), but the calendar week, that is, in my case - 02.10.2023 00:00:00. It would also be interesting to know how to get data from the beginning of a calendar month/quarter/year.

I tried using |> range(start: date.truncate(t: now(), unit: 1w)) |> range(start: -1w) but I get either the start of the current day or the start of the day that was 7 days ago. I also tried to use date.add() in combination with date.weekDay(), but I got an error because the d: parameter expects Duration, not the number of days. I need the calendar start of the week.


Solution

  • Finally, combination of methods date.truncate and timeShift helped me to solve my problem.

    import "date"
       from(bucket: "training")
      |> range(start: date.truncate(t: -1y, unit: 1w))
      |> aggregateWindow(every: 1w, fn: sum, createEmpty: true)
      |> timeShift(duration: -3d)
      |> fill(column: "_value", value: 0.0)