Query for replacement values in energy counting

I have a simple situation:
I record two time series. The first is a basic energy counter, which just counts up. Unfortunately, sometimes the sensor is wrong or not connected.
For that I have a second time series where I can manually enter the correct metering reading — from time to time.
So the first time series contains many values, just counting up. The second is fewer.

Now, I would like to write a query which merges these to time series in a specific way. The result should show one series which counts up, just like the first series. But every value from the second series corrects the counting from the first series. But it shouldn’t just replace the value at the specific timestamp, all following values are corrected as well. The upcounting should just continue staring from the replacement value, until the next replacement value.

I hope my goal is clear. I have some experience with influx queries, but I have no idea where to start here.

I could use some hints. If I find a solution I’ll share it here.

Hello @phaabe,
Just to verify you’re using 2.x and Flux correct?
This won’t be possible in InfluxQL and 1.x. I’m not even sure if it’s possible with Flux.

@phaabe Can you provide examples of the data from your two series?