Dividing 2 integers gives back NaN

  • What Grafana version and what operating system are you using?
    Grafana V 9.0.3, Mac os Montery
  • What are you trying to achieve?
    Divide 2 integers values from the same bucket
  • How are you trying to achieve it?
    I use influxdb as my db.
    This is my query

from(bucket: “EVScheduler”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r.AssetID == “${Charger}”)
|> filter(fn: (r) => r.UserID != “test”)
|> filter(fn: (r) => r._measurement == “Website” or r._measurement == “EVChargers”)
|> filter(fn: (r) => r._field == “Distance” or r._field == “Energy”)
|> pivot(rowKey: [“_time”], columnKey: [“_field”], valueColumn: “_value”)
|> map(fn: (r) => ({ r with test1 : int(v: r.Distance) / int(v: r.Energy) }))

  • What happened?
    This gives back no value.
    When I make the calculation trough transform, with binary operator I get NaN (not a number).

I used the following fluxdb explanation Use multiple fields in a calculation | InfluxDB Cloud Documentation

Thank you in advance

i already had this error, if i remember right it was because there is a something divide by 0.
you have to avoid any value =0 in your energy column
may you can use fill() or drop() to work around
maybe join on 0 value timestamp after your division

This is what it looks like in the Transform tab. None of my values are 0, yet it doesn’t sum the “Distance” value.

When is sum the values this way it returns NaN

@raflodewyckx if you click on the bug (debug) icon in the transform tab, you’ll get to see the values and the calculation. Can you share a screenshot of that output?

Screen Shot 2022-07-27 at 4.41.09 PM

1 Like

I have read that in order to do calculations with 2 values, those values need to have the same time stamp. Is that right? The 2 values do not have the same time stamp, therefore I use the function truncateTimeColumn , but this gives less accurate calculations since I need the most recent values for one of the two integers. The value by which I divide is a constant and gets only updated every hour or day and so it does not have the same time stamp.

I simply want do divide these 2 values, is there no easy way around the different timestamp problem? Thanks in advance

in any case you will need a id field to group by it,
for example if i have file moved between two state at different timestamp,
i used part of file name to build unique id, and then i could group by this tag (to pivot and calculation)
Is your timestamp truncated is a unique id for your 2 states ?

Getting the same issue, but on Grafana v7.5.5

I have an identical panel on v9.0.4 that works totally fine and as expected. Both reading from the same InfluxDB. Wondering what the problem might be, even the panel JSON is identical and yet I receive different outputs for my binary operations

@raflodewyckx Perhaps you could try to make the upgrade to v9.0.4 instead of v9.0.3? Maybe there was a patch that fixed this issue in that release?

I truncate both values by 1 day atm, this is to much for now but it works. I will have to find a way to match the time stamps before I send them to grafana

1 Like