InfluxDB Flux - get count of distinct occurrences

I’m trying to get a count of unique occurrences of a tag. I’m using a ‘filter’ to select the relevant data, then distinct to return only the distinct values. The problem is, distinct seems to return 2 tables. My Flux query is:

from(bucket:"areaEvent")
  |> range(start: 2020-07-21T09:00:00.00Z, stop: 2020-07-21T09:10:00Z)
  |> filter(fn: (r) =>
    r._measurement == "areaEvent" and
    r.areaId == "area_2"
  )
  |> keep(columns: ["visitorId"])
  |> distinct(column: "visitorId")
  |> count()

The output is:

Result: _result
Table: keys: [visitorId]
      visitorId:string                  _value:int  
----------------------  --------------------------  
                abc125                           1  
Table: keys: [visitorId]
      visitorId:string                  _value:int  
----------------------  --------------------------  
                abc126                           1  

How would I just get an output value of ‘2’, which is the count of distinct visitor IDs that appear in this result?

I’m still learning this myself, but it seems that flux has a notion of lists of tables in the output of a query. And that operations are applied to each table individually .

Try using group()

More info here

https://v2.docs.influxdata.com/v2.0/reference/flux/stdlib/built-in/transformations/group/

Thank you very much! That is very helpful and provides the solution.

Regards,

Rob

No worries, can you mark the comment with the solution so it helps others that run into this issue in future :+1:

This topic was automatically closed 60 minutes after the last reply. New replies are no longer allowed.