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


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?


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["_measurement"] == “myMeasurement”)
|> filter(fn: ® => r["_field"] == “myField1” or r["_field"] == “myField2”)
|> map(fn: ® => ({_time: r._time, _value: r._value, _field: r._field}))
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: “_value”)
|> map(fn: ® => ({_time: r._time, myField1: r.myField1, myField2: r.myField2, myCalc: r.myField1 + r.myField2}))