# How to convert customer interval variable to seconds

**URL:** <https://community.grafana.com/t/how-to-convert-customer-interval-variable-to-seconds/33752>\
**Category:** Prometheus\
**Created:** [July 16, 2020, 8:03pm UTC](https://community.grafana.com/t/how-to-convert-customer-interval-variable-to-seconds/33752 "2020-07-16T20:03:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikeclarke2500](https://avatars.discourse-cdn.com/v4/letter/m/d07c76/32.png) [@mikeclarke2500](https://community.grafana.com/u/mikeclarke2500)\
**Post date:** [July 16, 2020, 8:03pm UTC](https://community.grafana.com/t/how-to-convert-customer-interval-variable-to-seconds/33752/1 "2020-07-16T20:03:46Z")

</div>

Hi Community member,

I have a interval variable by name “period” and its possible values are 1m, 10m, 30m, 1h.

I want to use the same variable and pass it to prometheus query, however, Before passing i need to convert it into “seconds” (For example if i select 1m it want to pass as 60 in the prometheus query)

 ![image](https://us1.discourse-cdn.com/grafana/original/2X/6/63d331ba487283c8067f7213448cb8108ba571f0.png)

Best  
Grafana Newbie

---

<div class="post-metadata">

**Author:** ![w4rgrum](https://sea2.discourse-cdn.com/grafana/user_avatar/community.grafana.com/w4rgrum/32/14053_2.png) [@w4rgrum](https://community.grafana.com/u/w4rgrum)\
**Post date:** [December 11, 2020, 3:13pm UTC](https://community.grafana.com/t/how-to-convert-customer-interval-variable-to-seconds/33752/2 "2020-12-11T15:13:50Z")

</div>

You can try the workaround below (crappy way to do it, would be nice to have what you ask as a feature in Grafana by adding \_s to the variable or something like this)

Create a new variable “period\_s” below the “period” variable of type Query on your Prometheus datasource and use this as your expression:

```
query_result(label_replace(label_replace(label_replace(label_replace(vector(1),"period_s","${period}","",""),"period_s","(60*$1)","period_s","([0-9]+)m"),"period_s","(3600*$1)","period_s","([0-9]+)h"),"period_s","(86400*$1)","period_s","([0-9]+)d"))

```

Then filter with a standard regex:

```
/^.*period_s="([^"]+)".*$/

```

This will use several label\_replace on a dummy vector(1) query on your Prometheus datasource to generate the string representing the arithmetic expression you need to have the interval converted to seconds. You can then use the period\_s Grafana variable in your prometheus queries (note that as this generates a string representing an arithmetic expression it cannot be used in Grafana attributes such as min step or others)

---

<div class="post-metadata">

**Author:** ![mleone607](https://avatars.discourse-cdn.com/v4/letter/m/c6cbf5/32.png) [@mleone607](https://community.grafana.com/u/mleone607)\
**Post date:** [March 16, 2021, 10:03pm UTC](https://community.grafana.com/t/how-to-convert-customer-interval-variable-to-seconds/33752/3 "2021-03-16T22:03:24Z")

</div>

This was very helpful. Thanks.

The regex in the solution has to be modified to work with later versions of Grafana (I suspect 7.4+ when support for named text and value capture groups was added). If you don’t specify “value” as the name of the capture group, the result will include a name and a value separated by whitespace. The name will be the unfiltered string, and the value will be a number that is not even close to the expected value.

This worked correctly for me:

`/^.*period_s="(?<value>[^"]+)".*$/`
