Time Series: how to use column values as serie names

Possible duplicate of 80782.

  • What Grafana version and what operating system are you using?
    v11.6 / Linux

  • What are you trying to achieve?

    Use a column data as serie name while keeping all metadata columns available:

    SELECT ts, val, serie, metadata
    FROM data_table
    

    to be treated by Grafana as

    ts serie 1 serie N metadata
    ts1 val1 NULL metadata1
    ts1 NULL val2 metadata2
    tsM valX NULL metadataX
  • How are you trying to achieve it?

    Currently I’ve come up with only working solution:

    1. GROUP BY into JSON:
    SELECT ts, json_object_agg(serie, val) as obj, metadata
    FROM data_table
    GROUP BY ts, metadata
    
    1. Use Extract fields transformation.
  • What happened?

    The downside of the above approach is complication of SQL query - unnecessary GROUP BY and JSON packing/unpacking.

    I’ve also tried another transformations, but all of them have their own downsides:

    • Grouping to matrix - with Column: serie, Row: ts, Cell value: val - it works and allows to show time series grouped by serie names, but it doesn’t preserve metadata column => if I want to create override to form a link for every point on a graph using metadata column, it is not possible with this transformation.
    • Partition by values - it somehow works and even link could be created, but:
      • data is no longer in single table when Inspect -> Data is used.
      • every serie is prefixed with val column name, so I see val serie 1val serie N in the legend.
  • What did you expect to happen?

    Have an option for Time Series panel to auto-group values and metadata by specified column or have transformation that works like Grouping to matrix, but keeps metadata columns connected with values.

  • Did you follow any online instructions? If so, what is the URL?
    Time series | Grafana documentation

Just profiled 2 variants - with and without json_object_agg, and in my case using this workaround increases query time from 6.5 to 9 seconds. I would say that it is too much, so better to have proper solution in Grafana.

What is your datasource? MSSQL?

PostgreSQL 16