Derivative on downsampled bucket gives wrong result

Hi, I’m new in this community and I’ve been using InfluxQL for very few time, and I have a question for you.
I’ve downsampled one of my buckets’ data (which is collected every 10s) for monotonically increasing (with resets) fields like SNMP ifHCInOctets with a query like the following one:

from(bucket: "my-bucket")
  |> range(start: -task.every)
  |> filter(fn: (r) => r["_measurement"] == "snmp")
  |> filter(fn: (r) => r["_field"] == "ifHCInOctets")
  |> aggregateWindow(every: 1h, fn: max, createEmpty: false)
  |> to(bucket:"my-downsampled-bucket", org: "myorg")

I’ve used the max function since it is monotonically increasing and in order to ignore if the counter is being reset within the aggregate window of 1h. If I plot the values of ifHCInOctets in the original bucket and in the downsampled one, they are almost the same, so everything is fine. This is the query:

from(bucket: "my-bucket") // or "my-downsampled-bucket"
  |> range(start: -1d)
  |> filter(fn: (r) =>
    r._measurement == "snmp" and
    r.agent_host == "router" and 
    r._field == "ifHCInOctets" and
    r.ifDescr == "wan0" 
  )
  |> aggregateWindow(every: 1h, fn: max)

If I introduce a derivative with 1s time, in order to have the Bytes/s input data rate, the graph for the downsampled bucket is completely wrong… What am I missing?

This is the query that gives different results with the original and downsampled bucket:

from(bucket: "my-downsampled-bucket")
      |> range(start: -1d)
      |> filter(fn: (r) =>
        r._measurement == "snmp" and
        r.agent_host == "router" and 
        r._field == "ifHCInOctets" and
        r.ifDescr == "wan0" 
      )
      |> derivative(
          unit: 1s,
          nonNegative: true,
          columns: ["_value"],
          timeColumn: "_time"
      )
      |> aggregateWindow(every: 1h, fn: max)

Given the fact that the downsampled values seem to be correct (plots are almost the same between original and downsampled bucket) there must me something really stupid that I’m missing, since I don’t understand why the derivative gives such different results.

Thank you in advance for any help.