Getting the next/previous value based on calculation

I have created a query that joins the derivative to my system uptime, so I can now find every restart my system does. So far, so good! However, I would like to be able to see the “previous” value to get the uptime before the reboot as well, can anyone give me a pointer as to how I would go about this? Ideally, I would like to have a result table with rows (n, n-1) as a finished result for further manipulation, but I can see myself looking to have (n, n+1) at some point as well.

This is my current query, any general tips on how to optimize or improve on it is of course appreciated! Cheers!

uptime = from(bucket: "system")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "system")
  |> filter(fn: (r) => r["_field"] == "uptime")
  |> filter(fn: (r) => r["host"] == "myhost")
  
deriv = from(bucket: "system")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "system")
  |> filter(fn: (r) => r["_field"] == "uptime")
  |> filter(fn: (r) => r["host"] == "myhost")
  |> derivative()

join(tables: {d1: uptime, d2: deriv}, on: ["_time"])
  |> keep(columns: ["host", "_time", "_value_d1", "_value_d2"])
  |> rename(columns: {"_value_d1": "_value", "_value_d2": "derivative"})