Return zero 0 when no data from flux query

I looked around, also in documentation but have not found how to overcome this…

Imaging when there no data to return because I am writing to database only when event occurs. Is there a way to make condition or return 0 when there is no data ?

I am presenting data from Grafana and I would rather display zero in some cases for users than no data.

Thanks

from(bucket: "test")
  |> range($range)
  |> filter(fn: (r) =>
    r._measurement == "quantity" and
    r._field == "countfld" and
    r.trigger == "reject")
|> drop(columns: ["msgid", "red", "green", "yellow", "trigger", "deviceid"])
|> sum(column: "_value")

@salvq There isn’t a way to do this, currently. fill() will replace null values with a specified value, but in your case, there aren’t any points with null values. There just aren’t any points so Flux doesn’t know what to fill.

What Flux is missing is time interpolation – the ability to create points at specific time intervals when no points exists. This is a known issue and there’s an issue/proposal on the Flux repo that outlines the problem and potential solutions: Spec fill/Interpolate function · Issue #436 · influxdata/flux · GitHub

This is what I was worry about :frowning: I will need to create some dummy records every x seconds in order to keep 0 as output.

Anyway, before your post I was about to try to utilize conditional approach within filter function, would this work to evaluate condition and if false create new table ? I have not deep dive that much in flux yet.

from(bucket: "test")
  |> range($range)
  |> filter(fn: (r) =>
    if r.trigger != "reject" then ....

Hi.

I know I am late here but if you are displaying in Grafana there is an option “No Value” - What to show when there is no value - it is under Standard Options in Grafana V8 and will allow you to edit what is displayed when there are no records. I use this to change the " No data " to whatever I feel the need to show the user when there is no data but I don’t want to say no data.

At least for single stat display mode, it may be there for Graphs as well.

Good luck.

1 Like

@Duck999, thanks for the follow up. I also have another follow up for this thread. As of Flux 0.87.0, Flux supports linear interpolation: interpolate.linear() function documentation.