Hi Team,
I am trying to compare network traffic for this week vs last week in Grafana.
I already have two queries returning the traffic values correctly:
Query A → This week traffic
Query B → Last week traffic
The issue comes when I use the Transformation → Add field from calculation to calculate the sum/difference between the two datasets.
Grafana fails to compare the values correctly, and the calculation either returns:
null values,
incorrect totals,
or no matching rows.
It looks like the timestamps between the two queries are not aligning properly during the transformation.
My goal is to:
-
What Grafana version and what operating system are you using?
-
What are you trying to achieve?
-
How are you trying to achieve it?
-
What happened?
-
What did you expect to happen?
-
Can you copy/paste the configuration(s) that you are having problems with?
-
Did you receive any errors in the Grafana UI or in related logs? If so, please tell us exactly what they were.
-
Did you follow any online instructions? If so, what is the URL?
Comparison of traffic on a single panel
Step 1 → Add Query A (Current Week Traffic)
In the query box type
sum(rate(network_bytes_total[5m]))
Then click
Run queries
You will see a graph
I have used 10m because of my new setup and for your previous week comparison you should use offset 1w
Step 2 → Add Query B (Last Week Traffic)
Click on
- Add query
paste the
sum(rate(network_bytes_total[5m] offset 1w))
IMPORTANT
offset 1w
This shifts the query to last week while keeping timestamps aligned.
click on
Run queries
You will now see
one line for current week
one line for last week
Step 3 → Go to Transformations
At the top tabs click
Transform
Step 4 → Add “Join by field”
Click on
Add transformation
Choose
Join by field
Configure
Join field = Time
This combines both query results into one table.
Step 5 ->Add Calculation
Now again click
Add transformation
Choose
Add field from calculation
Do the Settings
Option Value
Mode → Binary operation
Operation → Subtract
Left field → Query A
Right field ->Query B
This calculates
This week - Last week
Step 6 ->Verify Output
Switch visualisation to
Table
OR
Time series
You should see
Time
Current
Last Week
Difference
No null values
If you are getting “No Data”
Most common reason
Your metric name may not be
network_bytes_total
To find your metric
In Prometheus query box
Type → network
Grafana will show available metrics.
Use your actual metric name.
I am using influx datasource. will this work?
Yes it will work for influx datasource also
The main issue with comparing two time periods in InfluxDB is that the timestamps between the two queries do not align
Query A covers the current period and Query B covers the previous period so when Join by field transformation tries to match them by time it finds no matching timestamps and the difference line will not appear
so use timeshift in queryB
Query A->from(bucket: “your_bucket”)
|> range(start: -1w)
|> filter(fn: (r) => r._measurement == “net”)
|> filter(fn: (r) => r._field == “bytes_recv”)
|> aggregateWindow(every: 1h, fn: mean)
|> yield(name: “A”)
QueryB->from(bucket: “your_bucket”)
|> range(start: -2w, stop: -1w)
|> filter(fn: (r) => r._measurement == “net”)
|> filter(fn: (r) => r._field == “bytes_recv”)
|> aggregateWindow(every: 1h, fn: mean)
|> timeShift(duration: 1w)
|> map(fn: (r) => ({r with _field: “bytes_recv_prev”}))
|> yield(name: “B”)
Now do transformation just like before and you will get the result
Using the time settings in grafana 13 i am able to compare non transforned data. but wehn i do the same on transformed data it is not working. see below. any ideas?