Problem to get exactly 2 weeks of data for my Flux query

Hello All,

I’m using grafana 10.1

I would to compute kwh per day from last 2 weeks. My computation of kwh working well but the time selection is not correct:

I used:
|> range(start: -2w, stop: -1d)

When I use this range it took my current time to compute the 2 weeks. So, if the time is 14:21 (07/21) so the data will start from (07/07: 14:21 to 07/20-14:21). which it is not good.

I would like data from midnight (00:00) like this:
-07/07 - 00:00 to 07/20 - 23:59

Ideally I should use:
|> range(start: -2w/d, stop: -1d/d)

But it do not work in my flux:
“Status: 500. Message: invalid: error @5:22-5:23: undefined identifier d error @5:18-5:23: duration is not Divisible error @5:35-5:36: undefined identifier d error @5:31-5:36: duration is not Divisible”

So how to proceed ?

Thanks for your help

Try this:

import "date"

...

  |> range(start: date.truncate(t: -14d, unit: 1d), stop: date.truncate(t: -1d, unit: 1d))

Thanks ebabeshko,

The exact line is
|> range(start: date.truncate(t: -14d, unit: 1d), stop: date.truncate(t: now(), unit: 1d))

If you use stop: date.truncate(t: -1, unit: 1d)) it is in fact, 2 days before (so 07/19 with my example).

ok. could be slightly simplified:

import "date"
...
|> range(start: date.truncate(t: -2w, unit: 1d), stop: today())