Dashboard variable query get distinct labels with influxdb

  • What Grafana version and what operating system are you using?
    v9.3.6 (978237e7cb)

  • What are you trying to achieve?
    I record temp/light/moist from several locations using influxdb and influxv2 language
    Each row is tagged with the location
    I want to get all the unique values of location tag, and make a dashboard variable for multi-selection

  • How are you trying to achieve it?
    I did create a dasnboard variable query, using this query :

from(bucket: "my-bucket")
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> filter(fn: (r) =>
    r._measurement == "my-measurement"
  |> distinct(column: "location")
  • What happened?
    I expected to get the 2 locations that I have records for now
    When using the “explore” tab, the very same query does return the expected rows, ie something like that :

But both in dashboard view, or in dashboard config variable view, only 1 value (ie 1 row) shows up

  • What did you expect to happen?
    I expected both locations to be available as variable multi-choice, but only one does

  • Can you copy/paste the configuration(s) that you are having problems with?
    No sure if this question is relevant in my case

  • Did you receive any errors in the Grafana UI or in related logs? If so, please tell us exactly what they were.
    No error message, quite the opposite, the very same query gives me expected result when using it in explore

  • Did you follow any online instructions? If so, what is the URL?
    No instruction but doc on flux language distinct() function | Flux 0.x Documentation

Hi @bastien,
Can you try with listing unique tags with schema.tagValues function?

E.g. for listing cpu tags:

import "influxdata/influxdb/v1"
    bucket: v.bucket,
    tag: "cpu",
    predicate: (r) => r._measurement == "cpu" and r._field == "usage_system" and r.cpu =~ /cpu[0-2]/,
    start: -1d

Note: Predictate is basically what you usually write in filter() function.

This returned:


Best regards,

1 Like


I tried it and it worked, thanks :slight_smile:

Meanwhile, I also had found a way to do it :

from(bucket: "bucket")
  |> range(start: v.timeRangeStart, stop:v.timeRangeStop)
  |> filter(fn: (r) =>
    r._measurement == "my-measurement"
    and r._field == "temp"
  |> distinct(column: "location")
  |> group()
  |> keep(columns: ["_time", "location"])

What I added from before :

  • group() I guess before that I had a stream of tables. With group() I get a table, and rows are taken as variable entries
  • keep(columns: ["_time", "location"]) which I understood is setting the values of location as default values. I’m not sure about the analogy here, because I understood that _value should be an int/float/bool but not a string

@ldrascic Am I write to think that tagValues() is a more efficient solution coz it’s a single call ?

Haaaa !
Turn out I RTFM thanks to @ldrascic link

It also works fine and is more clear like that :

import "influxdata/influxdb/schema"

    bucket: "bucket",
    tag: "location",
    measurement: "my-measurement",
    start: -1d

This may help you or others in the future.

1 Like