Flux query reformatted in Task

I have a rather complex flux query that I am using in Grafana and it has this particular section:

join(tables: {billed: billed, current: current}, on: ["type"])
  |> map(fn: (r) => ({ r with balance: -(r.peak_used_current-r.peak_used_billed)+(r.peak_generated_current-r.peak_generated_billed) }))
  |> map(fn: (r) => ({ r with join: 1 }))

I wanted to reuse this query in a Task (to output the result to MQTT). But I am not getting the same value in MQTT what I see in Grafana. After checking the query in the Task I noticed that the above line is getting re-formatted in the task to this:

join(tables: {billed: billed, current: current}, on: ["type"])
    |> map(fn: (r) => ({r with balance: -r.peak_used_current - r.peak_used_billed + (r.peak_generated_current - r.peak_generated_billed)}))
    |> map(fn: (r) => ({r with join: 1}))

This may look the same, but the first query returns balance = 1995 and the second returns balance = -44801.
I don’t know how to fix this. It is getting replaced all the time in the Task. If I run it in the Explorer everything is OK (not getting changes and I get the result I expect).

I am using the following:
InfluxDB 2.1.1
Server: 657e183
Frontend: cc65325

Now I replaced the calculation this this:

    |> map(fn: (r) => ({r with balance: -r.peak_used_current + r.peak_used_billed + r.peak_generated_current - r.peak_generated_billed}))

And mathematically it is the same as the first calculation (I also removed some of the brackets which were irrelevant for the calculation but made it easier for me to understand the two parts of the formula.

After all I was able to solve this now, just wanted to highlight is case there is a bug in some auto formatting logic.

Hello @nygma2004,
Thank you for sharing.
That is very strange. I’ll share with the Flux team.