Math operations with fields

Hi all,

I would like to perform a math operation between 2 fields.
field 1 and field 2.

how would be the grafana query builder at the end for giving me some values.

Thanks in advance

Hello,

Transformations, and especially Add field from calculation should help you solve your issue.
If you want more help on this, can you give a bit more detail on which datasource you’re using and which use case you want to solve?

The datasource in influxdb and info comes from telegraf.
Can you give me a screenshoot of how it would be in grafana?

Thanks

Sure, so you can have two different queries or one query returning 2 series. And then, you go to the Transform tab, choose Add field from calculation and you can do the following:

You can display only the Sum by enabling Replace all fields.

Or you could use Flux for pulling the data from InfluxDB. It allows calculations between measurements:

Or very simply, if you have your two fields in a single measurement:

  1. Filter your data on that measurement
  2. Filter your data on the two field names
  3. Map the data to get rid of whatever columns and other stuff you don’t want.
  4. CRUCIAL: do a pivot to create separate columns based on your field names
  5. Map the data again, this time to do the calculation you want. Use the dot notation to refer to the columns you want

Note: this can probably work cross-measurements too, you just use an ‘or’ in the measurement filter and make sure you filter tight enough on other tags or field names.

from(bucket: “myBucket”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “myMeasurement”)
|> filter(fn: (r) => r[“_field”] == “myField1” or r[“_field”] == “myField2”)
|> map(fn: (r) => ({_time: r._time, _value: r._value, _field: r._field}))
|> pivot(rowKey:[“_time”], columnKey: [“_field”], valueColumn: “_value”)
|> map(fn: (r) => ({_time: r._time, myField1: r.myField1, myField2: r.myField2, myCalc: r.myField1 + r.myField2}))

Hi,
I think I have an equal problem.
I would like to add to values. But the problem is, that they are not at the exact same time.
So the calculation ist zero + value, which is only one chart.

How cann I fix this problem.

Here are tow pictures:


Hi @ck76

If you are using Flux, you can use this function to normalize the timestamps:

Hi @grant2
could you explain every step I need. Iḿ not experienced in influxDB.
Which screenshots must I send?

thanks for your help, I found the solution in Grafana.

For the benefit of future readers in this forum, can you share your solution?

1 Like