Shana
Shana

Reputation: 71

Why is my Flux query in Grafana not sorting by default?

Grafana 7.5.1 with InfluxDB 1.8.2. I'm trying to create a table that displays a sum of the "units" values for each distinct tag value. I am getting the data, but I need the sums to be sorted in descending order by default. This is my Flux query:

    from(bucket: "consumption") 
    |> range(start: -1y) 
    |> filter(fn: (r)  => r._measurement == "stuff" and r._field == "units" and r._value > 0) 
    |> group(columns: ["dc","tnt"]) 
    |> sum(column: "_value")
    |> sort(columns: ["_value"], desc: true)
    |> map(fn: (r) => ({r with _value: r._value / 4.0}))
    |> yield()

I also have a Reduce transformation (Calculations --> Total) and Organize Fields transformation.

But no matter what I do in the sort function, it doesn't change anything in the table. The table is just always sorted alphabetically by the tag values ("dc", "tnt"). I need it to be sorted by _value descending. What am I doing wrong?

Thanks!

Upvotes: 1

Views: 3996

Answers (1)

Peter Donker
Peter Donker

Reputation: 348

This video explains what you're seeing:

https://www.youtube.com/watch?v=9B4ioIlNGMk

Basically you'll need to add an empty |> group() before your sort to collapse all those tables into one.

Upvotes: 7

Related Questions