How to handle two fields in flux

Hi,

I was hoping if someone could give me more insights on how fields are handled in flux lang.
Let’s say I have a measurement “m1” with two fields “f1” and “f2” how should I use flux if we need both the fields in the same graph.

I tried:
from(bucket: “SomeDB/autogen”)
|> range($range)
|>filter(fn: ® => r._measurement == “m1” and r._field =~ /^f1|f2/ )
|>keep(columns: ["_time",“f1”,“f2”])
|>group(columns: [“time”], mode:“by”)
|>window(every: 1m)
|>sum(column: “f1”)
|>avg(column: “f2”)
|>duplicate(column: “_stop”, as: “_time”)
|>window(every:inf)

This gives me an error saying “f2” does not exist.
I believe this is because for each time I have two records one with “f1” and other with “f2”.

Also, If I want to show two fields in a graph each from a different measurement should I use a join or union which is a better way.

As far as I understand the data is visualized based on the number of tables returned by the flux query, each table will be a line.
Please correct me if I am wrong.

@Sameer_Raj You’re correct in saying that each table is its own line in the graph. So the goal is to ensure that f1 and f2 are in separate tables, which they should be by default since _field is part of the group key.

In your results, it’s important to understand that f1 and f2 are not column names. They are values in the _field column. To get them to be their own columns, you need to use pivot() right after your filter.

from(bucket: "SomeDB/autogen")
  |> range($range)
  |> filter(fn: (r) => r._measurement == "m1" and r._field =~ /^f1|f2/ )
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")

I notice that you do two aggregates later in the query. The 2nd one won’t actually work since aggregate functions drop columns that 1) aren’t in the group key or 2) aren’t the specified aggregate column. So after you sum f1, f2 won’t exist anymore. You can use reduce() to aggregate them and preserve them. Looking at your query as a whole, this should give you what you’re trying to do:

from(bucket: "SomeDB/autogen")
  |> range($range)
  |> filter(fn: (r) => r._measurement == "m1" and r._field =~ /^f1|f2/ )
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> window(every: 1m)
  |> reduce(
      identity: {f1_sum: 0.0, f2_sum: 0.0, f2_avg: 0.0, count: 0},
      fn: (r, accumulator) => ({
        f1_sum: r.f1 + accumulator.f1_sum,
        f2_sum: r.f2 + accumulator.f2_sum,
        f2_avg: (r.f2 + accumulator.f2_sum) / (accumulator.count + 1),
        count: accumulator.count + 1
      })
    )
  |> drop(columns: ["f2_sum", "count"])
  |> rename(columns: {f1_sum: "f1", f2_avg: "f2"})
  |> duplicate(column: "_stop", as: "_time")
  |> window(every:inf)
1 Like

There’s a typo in your code, ‘accumualtor’ should read ‘accumulator’

Hi Scott,
Thanks for the explanation , and I appreciate the level of details. As of now I was using joins but I will now replace most of my queries with the reducer which seems more logical and let you know my feed back.

Sameer

Thanks for that. I fixed it in the example.