Grafana w/ InfluxDB: How to sort based on field key?

Grafana: v8.1.2
InfluxDB: v2.0.8

I’m using the flux query language. I have a data structure like so:

_time
_measurement
|- Battery
_tag
|- Unit ID
_field
|- b1c1 (Battery bank 1, cell 1)
|- b1c2
|- b1c3

|- b1c10
|- b1c11
|- b1c12

When I do a query on “Battery”, renaming the keys by regex to just 1, 2 … 12 in Grafana, and display the fields in a “Stat” panel, Grafana will display it like so: 1, 10, 11, 12, 2, 3, 4 … instead of the numerical ascending order.

I currently workaround this by doing 12 queries in the order I want but that is quite painful, is there any option in InfluxDB / Grafana that I can use to do this?

Hi @ycsin

It might be helpful for us here if you shared your panel json. Paste it into a code block using triple back-ticks (```) in your response?

Hi @mattabrams,

thanks for the reply.

{
  "id": 23763571993,
  "gridPos": {
    "h": 26,
    "w": 6,
    "x": 12,
    "y": 21
  },
  "type": "stat",
  "title": "🔋 Bank 1",
  "transformations": [
    {
      "id": "renameByRegex",
      "options": {
        "regex": "b1c(\\d+) .*",
        "renamePattern": "$1"
      }
    }
  ],
  "datasource": "${DB}",
  "pluginVersion": "8.1.2",
  "maxDataPoints": 50,
  "interval": "60s",
  "fieldConfig": {
    "defaults": {
      "thresholds": {
        "mode": "absolute",
        "steps": [
          {
            "color": "red",
            "value": null
          },
          {
            "color": "light-orange",
            "value": 3.5
          },
          {
            "color": "green",
            "value": 3.8
          },
          {
            "color": "red",
            "value": 4.2
          }
        ]
      },
      "mappings": [],
      "color": {
        "mode": "thresholds"
      },
      "decimals": 2,
      "max": 4.3,
      "min": 3.3,
      "unit": "volt"
    },
    "overrides": []
  },
  "options": {
    "reduceOptions": {
      "values": false,
      "calcs": [],
      "fields": ""
    },
    "orientation": "horizontal",
    "text": {},
    "textMode": "name",
    "colorMode": "value",
    "graphMode": "area",
    "justifyMode": "auto"
  },
  "targets": [
    {
      "query": "from(bucket: \"BMS\")\r\n  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)\r\n  |> filter(fn: (r) => r[\"uid\"] == \"${UID}\")\r\n  |> filter(fn: (r) => r[\"_measurement\"] == \"BMS\")\r\n  |> filter(fn: (r) => r[\"_field\"] =~ /b1c.*/)\r\n  |> sort(columns: [\"_time\"], desc: true)\r\n  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)\r\n  |> yield(name: \"mean\")",
      "rawQuery": true,
      "refId": "1"
    }
  ],
  "description": "",
  "timeFrom": null,
  "timeShift": null
}

Currently it looks like this:

I’d like them to be in this order instead:

InfluxDB: