Use processed strings to refer as column names in Flux

How to use refer column names using r.sourceProcessed and r.targetProcessed ? The processed variables are exact same as the column name.

sourceProcessed = processStatusName(status: source)
targetProcessed = processStatusName(status: target)

from(bucket: bucket)
  |> range(start: startRange, stop: now())
  |> filter(fn: (r) =>  r._field == sourceProcessed or r._field == targetProcessed)
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
  |> filter(fn: (r) => r.sourceProcessed != "" and r.targetProcessed != "")
  |> map(fn: (r) => {
      diffSeconds = float(v: int(v: r.targetProcessed) - int(v: r.sourceProcessed))
      time_diff = diffSeconds / 86400.0 
      return {_time: r._time, time_diff: time_diff}
    })
  |> group(columns: ["_start", "_stop"])
  |> mean(column: "time_diff")
  |> yield(name: "mean_time_diff")

This issue occurs only in bottom part. Following query works without throwing any errors.

from(bucket: bucket)
  |> range(start: startRange, stop: now())
  |> filter(fn: (r) =>  r._field == sourceProcessed or r._field == targetProcessed)
  |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")

Any ideas to fix this? TIA!

I dont think you can filter like that, the notation is

r.columnName

or

r[“columName”]

Iin this case the attribute or object being referenced cannot be variables, however I do something like this all the time using grafana and grafana variables which in the influx script are interpolated using the following notation ${variablename}.

but, I may be wrong.

@fercasjr thanks for the reply. This is indeed for a Grafana Dashboard and the sourceProcessed and targetProcessed are being manipulated from the Grafana dropdown and the dropdown elements have more readable attribute names with spaces and parentheses. What I have done is processed them to match with db fields accordingly. I even tried mapping data in Grafana but that did not work as well.

For an example my columns like this

status_1_timestamp,status_2_timestamp,status_3_timestamp

and my dropdown attributes are like

Status 1, Status 2, Status 3