How to order the fields of (wide format) time series data

  • What Grafana version and what operating system are you using?
    Grafana: v9.3.2 (21c1d14e91)
    Operating system: Windows 10

  • What are you trying to achieve?
    Display timeseries fields in a certain order.

  • How are you trying to achieve it?
    In previous version (7.4.2) of Grafana I accomplished this by adding an Order by clauses to my SQL query.

Select
    time,
    metric,
    value
From
    MyTable
Order By
    Metric

But this is no longer possible since in the new version of Grafana Timeseries data have to be sorted ascending by their Time field.

  • What happened?
    The fields are always ordered alphabetically by their (metric) name

  • What did you expect to happen?
    To have a way of defining the order of the timeseries fields and it being displayed as desired, like it worked in Grafana 7.4.2

  • Additional info
    I tried a couple of workarounds:

  1. I tried to order by Time, and addtionally by Metric secondly, but this has no affect.
  2. I also tried to format my query as a table query, and then apply a “Partition by values” transformation which does initially accomplish the data to be in a Timeseries format AND with the desired ordering. But after the first refresh, the colors assigned to each field (by the default color pallet) suddenly changes in the Graph and Legend.

My main question therefor is, how can I order the fields of my timeseries data?

Please note that the fields are not fixed, and are dynamically loaded by the query as a group by, so I cannot simply add a “Organize fields” transformation to do the ordering.