Daily aggregation (by date not 24h) and range (first to last record)

Hello,
Please could someone assist me with how to specify the range so that it would start from the oldest to the last record in the bucket instead of the v.timeRangeStart and v.timeRangeStop

|> range(start: v.timeRangeStart, stop: v.timeRangeStop)

Also,
I would like to aggregate by the maximum value for each date not 24hours i.e. The highest value on 21st, 22nd and 23rd …etc of each day in the bucket.

Would this allow me to achieve that?
|> aggregateWindow(every: 1d, fn: max, createEmpty: false)

You can truncate your range start/stop to days; this way every 1d window will fill an actual day:

import "date"

timeRangeStart = date.truncate(t: v.timeRangeStart, unit: 1d)
// now use timeRangeStart as argument to range()

See date.truncate() function | Flux 0.x Documentation

If you do not specify a stop, the range will go to now() and so naturally the last window (ie, result row) returned will fit your last record.

Starting the range at first record is not as easy. You cannot query without specifying a range. You could, for example, specify -1y if you know that it will not be older than one year.