How to replace negative values in a table

A table can contain both positive and negative values. Now I don’t want to hide the negative values but replace them with 0 values. I tried that with the following code. The negative values are also recognized and replaced with 0. However, the positive values should be retained. I haven’t managed to do that yet. Where is the error in my code?

from(bucket: "telegraf")
  |> range(start: today())
  |> filter(fn: (r) => r["_field"] == "Total DC power (sum of all PV inputs)")
  |> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)
  |> map(
        fn: (r) => ({r with
            level: if r._value <= 0.0 then
                "0.0"
            else
                "r._value",
        }),
    )

example

Also, the value in the new column should be a number.

Hi, you are creating a new column where both negative and positive values are strings (one of them is 0.0 but it is a string)
You should remove the double quotes from r._value and from 0.0 as well

The column level now expects strings so you’ll better create a new column

1 Like

Many Thanks @MzazM
That was the solution:

The code now looks like this and works:

from(bucket: "telegraf")
  |> range(start: today())
  |> filter(fn: (r) => r["_field"] == "Total DC power (sum of all PV inputs)")
  |> aggregateWindow(every: v.windowPeriod, fn: last, createEmpty: false)
  |> map(
    fn: (r) => ({r with
      level: if r._value <= 0.0 then
        0.0
      else
        r._value,
      }),
    )
  |> drop(columns: ["_start", "_stop", "_measurement", "host", "name", "slave_id", "type", "_value"])  
  |> yield(name: "last")