[Flux] Calculate data by time frame

I have a set of data with a little calculation, now I want to calculate the result through different way with different time frame.
I original query

binance = from(bucket: "mainbucket")
  |> range(start: timeRange)
  |> filter(fn: (r) => r["_measurement"] == "Balance")
  |> filter(fn: (r) => r["_field"] == "bitGet1")
  |> filter(fn: (r) => r["type"] == "bitGet")
  |> aggregateWindow(every: 5m, fn: last, createEmpty: false)

join(tables: {ftx:ftx, ba:binance}, on: ["_time"])
  |> map(fn: (r) => ({
      _time: r._time,
      _value: (r._value_ftx + r._value_ba)
    })
  )
  |> movingAverage(n: 5)
  |> yield(name: "last")

Now I want to make result:
(r._value_ftx + r._value_ba) if date <= 2022-09-01
(r._value_ftx + r._value_ba)/2 if date is between 2022-09-01 and 2022-10-01
(r._value_ftx + r._value_ba)/3 if date is > 2022-10-01

Please help to write the logic with flux, thanks.

can anyone help on this?

Hello @Yole_Yu,
I’m sorry for the delay.
You’re going to want to use conditional mapping:

  |> map(
        fn: (r) => ({r with
            _value: if r._time <= 2022-09-01 then 
                (r._value_ftx + r._value_ba) 
            else if r._time >=  2022-09-01 and r._time <= 2022-10-01 then
               (r._value_ftx + r._value_ba)/2 i
            else if r._time >= 2022-10-01 then
                (r._value_ftx + r._value_ba)/3
        }),
    )

I hope that helps! Please let me know if you’re still running into problems. :slight_smile:

thanks, I have also find the solution