Flux duplicate one of column when field exists

Hello.
I wish to duplicate one of column when field exists.
But couldn’t get it.
like a …

from(bucket: “myremote”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “mydata”)
|> last()
|> drop(columns: [“_start”, “_stop”])
|> pivot(rowKey:[“_time”], columnKey: [“_field”], valueColumn: “_value”)
|> group(columns: [“_measurement”], mode:“by”)
|> duplicate(fn: (r) => ({r with
shipdate: if exists r._value then
column: “shipdate”, as: “shipdate_dup”
else
})
)

Should i use with map()?

Hello @Yutaka,
Yes if you want to duplicate conditionally, I’d use map with conditional filtering.

Otherwise you’d have to do special filtering with the filter() function and then apply the duplicate function.

Please note that the duplicate function doesn’t take a fn parameter:

   |> duplicate(column: "tag", as: "tag_dup")

Thanks!

1 Like

Hello @Anaisdg
Thank you for your informaion. :smile:

I understand about it.
I’m still not enough for flux.
I will find out with your information :pray:

In my case map() function will work.

from(bucket: “myremote”)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r[“_measurement”] == “mydata”)
|> last()
|> drop(columns: [“_start”, “_stop”])
|> pivot(rowKey:[“_time”], columnKey: [“_field”], valueColumn: “_value”)
|> group(columns: [“_measurement”], mode:“by”)
|> map(fn: (r) => ({r with shipdate_dup: r.shipdate }))