# Tag Variable based on time

**URL:** <https://community.influxdata.com/t/tag-variable-based-on-time/22592>\
**Category:** Fluxlang\
**Created:** [November 20, 2021, 10:39am UTC](https://community.influxdata.com/t/tag-variable-based-on-time/22592 "2021-11-20T10:39:15Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![oneofthemany](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/oneofthemany/32/7644_2.png) [@oneofthemany](https://community.influxdata.com/u/oneofthemany)\
**Post date:** [November 20, 2021, 10:39am UTC](https://community.influxdata.com/t/tag-variable-based-on-time/22592/1 "2021-11-20T10:39:15Z")

</div>

I am trying to get influx to filter tag values based on time, but the query I am using doesn’t seem to work:

```auto
  from(bucket: "my_bucket")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> keep(columns: ["mac_address"])
    |> group()
    |> distinct(column: "mac_address")

```

The plan was then to use this query in Grafana as a variable

Can anyone help as regardless of what time value I set it shows the same count of mac addresses

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [November 22, 2021, 5:40pm UTC](https://community.influxdata.com/t/tag-variable-based-on-time/22592/2 "2021-11-22T17:40:27Z")

</div>

> [@oneofthemany](#):
>
> Can anyone help as regardless of what time value I set it shows the same count of mac addresses

@oneofthemany Flux will only return tag values that exist in the queried data set, so it can’t guarantee that the count of mac addresses will always be the same because there may be a different number of mac addresses depending on the queried time range. With that said, I’d recommend using `schema.tagValues()` to get values of the `mac_address` tag.

```js
import "influxdata/influxdb/schema"

schema.tagValues(
  bucket: "my_bucket",
  tag: "mac_address",
  start: -30d
)
```

---

<div class="post-metadata">

**Author:** ![oneofthemany](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/oneofthemany/32/7644_2.png) [@oneofthemany](https://community.influxdata.com/u/oneofthemany)\
**Post date:** [November 22, 2021, 7:18pm UTC](https://community.influxdata.com/t/tag-variable-based-on-time/22592/3 "2021-11-22T19:18:19Z")

</div>

@scott - thanks for the reply, but that also does not work

re your comment

> Flux will only return tag values that exist in the queried data set, so it can’t guarantee that the count of mac addresses will always be the same

the issue that presents itself is that the list is not affected by the` start: -Xd` function within the `schema.tagValues` query

**Question on the above:**

Is this a known bug?

ideally the solution needs to be flexible based on the range within a given time period to offer better variable granularity

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [November 23, 2021, 4:35pm UTC](https://community.influxdata.com/t/tag-variable-based-on-time/22592/4 "2021-11-23T16:35:06Z")

</div>

@oneofthemany I just tested, and `schema.tagValues()` does appear to be respecting the time range defined in the function. Here’s what I did to test:

1. Created a `test` InfluxDB bucket.

2. Used the following query to write to points to InfluxDB with different tag values at different times.

3. I then queried two different time ranges – one to include one tag value, the other to return both. The tests behaved as expected:

One thing to note here is that `schema.tagValues` does not accept a `stop` parameter, so the right time boundary is always `now()`. However, you can create your own custom function to accept the stop time. Here’s the source definition for `schema.tagValues`: [flux/schema.flux at master · influxdata/flux · GitHub](https://github.com/influxdata/flux/blob/master/stdlib/influxdata/influxdb/schema/schema.flux#L71-L76)

You can create your own using that definition:

```javascript
customTagValues = (bucket, tag, predicate=(r) => true, start=-30d, stop=now()) => from(bucket: bucket)
    |> range(start: start, stop: stop)
    |> filter(fn: predicate)
    |> keep(columns: [tag])
    |> group()
    |> distinct(column: tag)

customTagValues(
    bucket: "my_bucket",
    start: v.timeRangeStart,
    stop: v.timeRangeStop,
)

```

---

<div class="post-metadata">

**Author:** ![oneofthemany](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/oneofthemany/32/7644_2.png) [@oneofthemany](https://community.influxdata.com/u/oneofthemany)\
**Post date:** [August 15, 2022, 11:57am UTC](https://community.influxdata.com/t/tag-variable-based-on-time/22592/5 "2022-08-15T11:57:28Z")

</div>

Apologies for the late reply, as we have been running into several difficulties trying to auto generate the MAC addresses based on the values caught within the DB.

The easiest was for us to create a CSV variable and add that variable to the query.

It is just a shame that you can’t have a multiple MAC query within the drop down and assign each of those the "or function like you do in the script query builder:

`|> filter(fn: (r) => r["mac_addr"] == 00:11:22:33:44:55 or r["mac_addr"] == aa:bb:cc:dd:ee:ff)`

Are you looking to have multiple option with the v.function drop down in the UI, as I would have thought that just appending the “or” function when more than one item is selected would be accomplishable.

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [August 15, 2022, 3:02pm UTC](https://community.influxdata.com/t/tag-variable-based-on-time/22592/6 "2022-08-15T15:02:49Z")

</div>

@oneofthemany You could instead extract the list of MAC addresses as an array and use `contains()` to see if it exists in the array. Also, sidenote, since you originally created this thread, the stop parameter was added to `schema.tagValues()`. You just need to use InfluxDB 2.2 or newer.

```javascript
import "influxdata/influxdb/schema"

mac_addresses = 
    schema.tagValues(
        bucket: "my_bucket",
        tag: "mac_address",
        start: v.timeRangeStart,
        stop: v.timeRangeStop
    )
        |> findColumn(fn: (key) => true, column: "_value")

from(bucket: "example-bucket")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> filter(fn: (r) => contains(set: mac_addresses, value: r.mac_addr))

```

---

<div class="post-metadata">

**Author:** ![oneofthemany](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/oneofthemany/32/7644_2.png) [@oneofthemany](https://community.influxdata.com/u/oneofthemany)\
**Post date:** [August 16, 2022, 3:06pm UTC](https://community.influxdata.com/t/tag-variable-based-on-time/22592/7 "2022-08-16T15:06:29Z")

</div>

@scott

Will look at testing array this week - thanks for the update 🙂

Can you confirm whether multi value select function from variable dropdown is being worked based on the “or” assignment to each value selected?

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [August 16, 2022, 3:44pm UTC](https://community.influxdata.com/t/tag-variable-based-on-time/22592/8 "2022-08-16T15:44:22Z")

</div>

@oneofthemany I don’t know if multi-select variables are planned. It’d certainly be a valuable addition. You should [submit a feature request on the InfluxDB UI repo](https://github.com/influxdata/ui/issues/new?assignees=&labels=enhancement%2C+team%2Fui&template=feature_request.md&title=).

If multi-select variables were available, it wouldn’t extend the query with `OR` statements in a `filter()` call. The way that the current dashboard variable implementation works is that each variable represents a simple assignment (`x = y`). The way a multi-select variable would works is that it would return an array of selected values (`x = [y, z]`). So you would still need to use `contains()` to check if a value is in the “selected” array.

For example, if you had a multi-select variable named `mac_addresses`, that variable would return an array of selected mac addresses and would be stored in the `v` record. To reference the array, you’d use `v.mac_addresses`:

```javascript
from(bucket: "example-bucket")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> filter(fn: (r) => contains(set: v.mac_addresses, value: r.mac_addr))

```

---

<div class="post-metadata">

**Author:** ![oneofthemany](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/oneofthemany/32/7644_2.png) [@oneofthemany](https://community.influxdata.com/u/oneofthemany)\
**Post date:** [May 10, 2023, 10:48am UTC](https://community.influxdata.com/t/tag-variable-based-on-time/22592/9 "2023-05-10T10:48:30Z")

</div>

Hey @scott : apologies for the late reply, but I have been shuffling around other things.

I have tested the above and it yields a zero value.

The query itself uses other value which should limit the value to a single mac which can be accomplished in the standard dashboard query.

The need is for something like the following:

```auto
import "influxdata/influxdb/schema"

mac_addr = 
    schema.tagValues(
        bucket: v.bucket,
        tag: "mac_addr",
        start: v.timeRangeStart,
        stop: v.timeRangeStop
    )
        |> findColumn(fn: (key) => true, column: "_value")

from(bucket: v.bucket)
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == v.measurement)
  |> filter(fn: (r) => r["filter_1"] == v.filter_1)
  |> filter(fn: (r) => r["filter_2"] == v.filter_2)
  |> filter(fn: (r) => contains(set: mac_addr, value: r.mac))

```

filter\_1 and filter\_2 are being used to only provide a value of a single MAC address

Is this achievable based on my query above?

---

<div class="post-metadata">

**Author:** ![maguri](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@maguri](https://community.influxdata.com/u/maguri)\
**Post date:** [December 20, 2023, 12:59pm UTC](https://community.influxdata.com/t/tag-variable-based-on-time/22592/10 "2023-12-20T12:59:21Z")

</div>

Hi @scott,

I was using the `tagValues()` function before I saw your post and figured out the reason the stop time range wasn’t working for me, so first of all, thanks.

I need to retrieve the “\_time” value for each distinct tag in my database, but I cannot find the correct solution. Can you help me?

Here’s my current query:

```flux
from(bucket: "${Bucket}")
    |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
    |> filter(fn: (r) => r["tag_2"] == "${Tag2}")
    |> keep(columns: ["tag", "_time"])
    |> group()
    |> distinct(column: "tag")

```

If I remove the `distinct`, I have the “\_time” in my result, but the tags repeat themselves. I tried using `unique` without success.

Please, if this requires a new thread or post, let me know.

Kind regards,

---

<div class="post-metadata">

**Author:** ![scott](https://sea1.discourse-cdn.com/flex023/user_avatar/community.influxdata.com/scott/32/16492_2.png) [@scott](https://community.influxdata.com/u/scott)\
**Post date:** [December 20, 2023, 3:52pm UTC](https://community.influxdata.com/t/tag-variable-based-on-time/22592/11 "2023-12-20T15:52:52Z")

</div>

@maguri With the nature of tags in the InfluxDB data structure, I would expect them to repeat themselves. Do you have tags that you expect to be unique and unrepeated? If so, that data may be better suited to be stored as a field.

Do you have some sample data you could share?

---

<div class="post-metadata">

**Author:** ![maguri](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@maguri](https://community.influxdata.com/u/maguri)\
**Post date:** [December 20, 2023, 4:47pm UTC](https://community.influxdata.com/t/tag-variable-based-on-time/22592/12 "2023-12-20T16:47:43Z")

</div>

The main purpose of this tag is to filter all measurements received with the specified tag\_value in a query. I believe it is currently effective as it is.

This tag serves as a production batch ID, providing a reference ID for each job or batch.

I have been considering that perhaps what I need to do is execute a query over measurements and retrieve the first measurement for each distinct or unique tag.
