Using timefilter to calculate delta between first value and last value

I’m using Grafana 8.4.1V

  • What are you trying to achieve?

I’m trying to have a pie chart with loading and unloading values from a machine. Like this:

However I would like to have the values on the chart based in the time filter of grafana. My postgresql table contains the total hours for loading and unloading of the machine as you can see here:

  • How are you trying to achieve it?
    I’m doing a query in order to get the last non null value from this measures.
    My table:

  • What happened?
    For now I just have the chart with the last not null values from both.

  • What did you expect to happen?

I would like to have a delta from the first value and last value of loading hours and also the same for unloading hours. This way the chart will be dynamic and will depend of the timefilter of grafana.

Can someone help me?