Flux filtering distinct field values based on multi-select template variable tag only gives values from the first tag

Dear all,

I have a multi-select template variable named unit2 which lets the user select one or more tag values for the ‘unit’ tag in my data. I want to fetch the distinct values of the ‘con_StackTrackingNumber’ field across all the selected ‘unit’ tag values. However, the query seems to always fetch the results based on the first tag value only. How can I fix it? ChatGPT couldn’t help :smiley:

from(bucket: “hsl_5m”)
|> range(start: 2000-01-01T00:00:01Z)
|> filter(fn: (r) => r[“_measurement”] == “internal”)
|> filter(fn: (r) => r[“unit”] =~ /^${unit2:regex}$/)
|> filter(fn: (r) => r[“_field”] == “con_StackTrackingNumber”)
|> distinct()

Welcome @giacomobornino to the :grafana: R :grafana: A :grafana: F :grafana: A :grafana: N :grafana: A :grafana: forum

I think you need to use the unique() function. Maybe something like this?

from(bucket: “hsl_5m”)
  |> range(start: 2000-01-01T00:00:01Z)
  |> filter(fn: (r) => r[“_measurement”] == “internal”)
  |> filter(fn: (r) => r[“unit”] =~ /^${unit2:regex}$/)
  |> filter(fn: (r) => r[“_field”] == “con_StackTrackingNumber”)
  |> unique(column: "con_StackTrackingNumber")  
  |> count()

Dear Grant, Thanks for your reply. Unfortunately unique() doesn’t work either. It’s strange because if I use the command without specifying the columns (i.e.: |> unique()) it works just like |> distinct(), by giving only the values of con_StrackTrackingNumber of one of the many tags selected. If I add the column name (i.e.: |> unique(column: “con_StackTrackingNumber”)), the ouput is ‘None’.