Calculate difference of values from the same metric

  • What Grafana version and what operating system are you using?

Grafana v9.5.21 (2dd6ebef03fbe42927769bf00bcf0bc4e621bb9c)
Ubuntu 18
DataSource - VictoriaMetrics v1.84.0

  • What are you trying to achieve?

calculate difference between last value and last but one value of metric and show the result on dashboard.

for example I have such data:

02-01-25_08-01-26: 0.21: 
01-01-25_16-00-01: 0.18: 
01-01-25_08-03-13: 0.19:

I want to calculate 0.21-0.18 == 0.03 and have this value 0.03 in the new field so I can show this in the Stat panel for example.

  • How are you trying to achieve it?

the last value 0.21 I can get with this query:

last_over_time(snow_depth{srv_ID="4"}[1d])*100

so this is my first question:
how to get value 0.18?

lets assume that data comes to datasource 2 times per day - around 8AM and 4PM.
(but for other metric which I also want to do same calculation - pattern would be - every half of hour)

Also there is second part of my task - calculating the difference of values.
I checked Transform “Add field from calculation” but seems there is some issue with creating new field from binary operation on two different fields - it will create new field with result only if the source fields - are the same!
and it does not create new field with result of calculation if the source fields different!
it is really strange.
Please take a look on the first picture - fields in calculation the same - SRV4_snow_depth and it have created test_field123 - so I can use it to show on the panel:

but when I use different fields - it shows that there no such field test_field123

And here my second question - where Im wrong to use this Transform? Should it be done in some different way?

Hi,
I’m not sure but maybe idelta function would help? (I’m not sure if you have high enough version of VM but since it’s supported by prometheus, I think you should be fine).

1 Like

“idelta” seems works but not sure for its “lookbehind window d”
in one my case, when data changes 2 times per day - 8 AM and 4PM, I have use this query:

idelta(snow_depth{srv_ID="4"}[1d])*100 to receive correct difference between last and last but one values

despite the fact that between two metric values it is only 8h I need to use 1d to receive correct result. I dont know how it works and when it possible to get the wrong result.

So I still would like to know - how to get value 0.18 ? how to get the “last but one” value?