Grafana & Prometheus: Weekly delta

I’m using Grafana and Prometheus for monitoring, and I often want to break a counter metric (continuously increasing) into weekly deltas. Sometimes I find it confusing how Prometheus range vectors and functions work in Grafana, and I tend to forget how I did it the last time. Hence I want to write down one way of doing weekly deltas that I recently used and checked for correctness.

  1. Choose a counter metric you want to split into weekly deltas, and add it to a new panel in Grafana. Do any filtering and adjustments as needed. You may also already set a title, units and other formatting options in Grafana.

    In my case, I used our household gas consumption as counted by a tasmota sensor. The metric is called tasmota_sensors_c1_, gets filtered by instance and scaled down by 100, resulting in the following query: tasmota_sensors_c1_{instance=~"$instance"} / 100

    Plot of the counter metric used

  2. Add the “Operations > Range functions > Delta” function to your query in the query editor. Set the Range to the internal $__interval variable of Grafana (should be the default). This will result in the following query: delta(tasmota_sensors_c1_{instance=~"$instance"}[$__interval]) / 100

    Adding the “Delta” operation in the query editor

  3. Click “Run queries” or “Refresh” to execute the new query. The plot will now show differences per time point and look something like this:

    Delta time series plot

  4. Set the visualisation type to “bar chart” in the Grafana panel settings.

    Setting the visualisation type in Grafana

  5. Open the “Query options” above the query editor and set the “Min interval” to 1d for daily, 1w for weekly or 1M for monthly deltas. Other ranges are possible as well.

    Setting the minimum interval in the query options

  6. If the query returns multiple time series, you may want to set “Stacking” to Normal in the query options panel. Consider enabling X-axis labels minimum spacing if labels overlap. You can also set a custom time unit to format the X-axis labels to your desire.

The resulting query will show the weekly deltas for your selected time series and may look like thise:

Prometheus panel showing weekly deltas in a bar chart

Analysis

You may notice that the time range displayed in bars does not neccessary match the time range specified in your query. For example, my query from 2024-01-20 to 2026-01-20 only includes bars from 2024-03-07 to 2026-01-15.

Apparently, Grafana will show bars for each full week contained in the query time range that has data. That means:

  • If your query time range exceeds the range your timeseries hold data for, bars will only be shown for the time range your timeseries holds data in (my timeseries do not hold data before 2024-03-03).
  • The bars are shown on dates where a week starts in Grafana. The dates of the bars always represent the start of a week (although weeks appear to start on Thursdays in my case).
  • If your query time range starts in the middle of a week, the first bar may show a date before the start of your query time range, because it holds the data for the full week containing your query start date.
  • If your query time range ends in the middle of a week, the last bar of your plot may show a date before the end of your query time range, because it holds the data for the full week containing your query end date.

Verification

We can verify these findings by comparing the value of a bar with the corresponding range of the time series. For example, the bar for 2026-01-08 shows a value of 51.82:

Bar chart, hovering the bar for 2026-01-08 with a value of 51.82

Querying the time range from 2026-01-01 00:00:00 to 2026-01-07 23:59:59 shows a start value of 1299.49 and an end value of 1351.24, resulting in a delta of 51.75:

Time series showing start and end values for the time span represented by the bar

This matches closely the value reported by the bar chart. I couldn’t find out why the value doesn’t match exactly, maybe Prometheus uses a slightly different time range or there are rounding issues. However, this result is close enough for me to believe I understood what Grafana and Prometheus are doing here. I hope it helps you as well, if you have an even better understanding of this, feel free to reach out to me on Mastodon ;)