A query like this works fine:
from(bucket: “X”)
|> range(start: 2022-01-01T00:00:00Z, stop: 2022-01-02T00:00:00Z)
|> group()
|> map(
fn: (r) => ({r with Errorx: r[“Y”]})
)
But when I replace “Y” with a variable shown below, I get an error (invalid: type error @7:37-7:38: expected int but found string)
Some time has passed without any replies, so I want to begin the dialogue. Looks like you want the value of Errorx to be the value in another column, but that column’s name isn’t hard-coded, it’s stored in a variable. If I’m understanding correctly, then perhaps map’s if-then-else construct would work for you?
@deb In this example from the map documentation (linked above)
|> map(fn: (r) => ({time: r._time, source: r.tag, alert: if r._value > 10 then true else false}))
the _value column of the record is used as the test condition. In your case, what is determining whether to choose Y, Z or I? Let’s say it’s also the _value column. Then something like
|> map(fn: (r) => ({r with Errorx: if r._value == (select Y column) then r["Y"]}))
|> map(fn: (r) => ({r with Errorx: if r._value == (select Z column) then r["Z"] else r["I"]}))
might to the job.
A couple disclaimers: (a) I don’t think else if is an allowable construct in map so I’ve broken it into two maps. (b) I’ve made assumptions about your data, so this approach may not be helpful (goes back to what is determining the column choice).
I think vary inside map is not allowed to prevent a runtime error, that is vary gets set to a non-existent column (e.g., “QQ”, “21.6”).
Thank you for your help. I didn’t want it to depend on the _value column, I wanted vary to determine the column name. I’m actually working on something different with a function, where vary is an input and map is used to do something else in the function. Thanks for your help, I’ll use the if-else to avoid the r[vary] error. The else if actually worked for me:
vary = "Y"
from(bucket: “X”)
|> range(start: 2022-01-01T00:00:00Z, stop: 2022-01-02T00:00:00Z)
|> group()
|> map(
fn: (r) => ({r with Errorx:
if vary == "Y"
then r["Y"]
else if vary == "Z"
then r["Z"]
else r["I"]
})
)
@deb Great news! I figured you gave us a code snippet of something bigger and vary was playing a deeper role in your script. Also, thanks for expanding my knowledge of map.