Custom reduce-like equivalent of cumulativeSum

I have a query:

from(bucket: "journal_test")
  |> range(
    start: -70d
  |> filter(fn: (r) => r._field == "amount")  
  |> window(every: autoInterval)
  |> keep(columns: ["_value", "_time", "_field"])
  |> cumulativeSum()

it provides a cumulative sum for each data point which can be then charted. I’d like to define custom operation (e.g. sum of multiplicated two fields but probably bit more complex operation over 3-4 fields). Ideally something like reduce() just need to output partial result for each point.

@ciekawy reduce() wouldn’t work in this case because it’s “destructive” – it aggregates all rows in a table into a single output row. You’d want to use pivot() to pivot all the fields used in your calculation into columns, then map() to run some kind of mathematic operation using those field values and store the value in a new column, then run cumulativeSum() on the new column.

customFunc = (tables=<-) =>
    |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
    |> map(fn: (r) => ({ r with _value: r.field1 * r.field2 * r.field3)
    |> cumulativeSum(column: "_value")