# How to apply range using calculated metric field

**URL:** https://discuss.elastic.co/t/how-to-apply-range-using-calculated-metric-field/368846
**Category:** Kibana
**Tags:** lens, visualisation
**Created:** [October 15, 2024, 11:53am UTC](https://discuss.elastic.co/t/how-to-apply-range-using-calculated-metric-field/368846 "2024-10-15T11:53:13Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![kishorkumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kishorkumar/32/132930_2.png) [@kishorkumar](https://discuss.elastic.co/u/kishorkumar)
#### Post date: [October 15, 2024, 11:53am UTC](https://discuss.elastic.co/t/how-to-apply-range-using-calculated-metric-field/368846/1 "2024-10-15T11:53:13Z")

</div>

I wanted to create the buckets for the total sales on the basis of the calculated value form metric aggs in lens  
i have total field in doc

so if use agg of avg in lens then i want to create the range built on top of the return value this way i will have the avg\_bucket of the range of total

and second the count of the customer fall in that avg\_range bucket.

like this

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/e/7/e73fe48fa14e34dc59cb6b2fbdae2be3688280c1.png)

is it possible without transfrom?

---

<div class="post-metadata">

### Author: ![Marco\_Liberati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marco_liberati/32/82953_2.png) [@Marco\_Liberati](https://discuss.elastic.co/u/Marco_Liberati)
#### Post date: [October 16, 2024, 7:34am UTC](https://discuss.elastic.co/t/how-to-apply-range-using-calculated-metric-field/368846/2 "2024-10-16T07:34:15Z")

</div>

Hi @kishorkumar

that looks like what it is called a "custom range" interval in Lens.  
To build configure an `Interval` and click on `create custom ranges`:

 ![Screenshot 2024-10-16 at 09.32.24](https://us1.discourse-cdn.com/elastic/original/3X/9/a/9a0c0f5c6ba437043e225b03d434480fbbc1275a.png)

at this point configure manually all the different intervals:

 ![Screenshot 2024-10-16 at 09.32.13](https://us1.discourse-cdn.com/elastic/original/3X/8/b/8b37f2564cdf733bdbc107fa798c4ca437a15673.png)

---

<div class="post-metadata">

### Author: ![kishorkumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kishorkumar/32/132930_2.png) [@kishorkumar](https://discuss.elastic.co/u/kishorkumar)
#### Post date: [October 16, 2024, 8:07am UTC](https://discuss.elastic.co/t/how-to-apply-range-using-calculated-metric-field/368846/3 "2024-10-16T08:07:15Z")

</div>

> [@Marco\_Liberati](#):
>
> ks like what it is called a "custom range" interval in Lens.  
> To build configure an `Interval` and click on `create custom ranges`:

i think you have missed the point here i know there custom ranges and interval i wanted to utilize the resutlt of the metric aggs not the field directly.

---

<div class="post-metadata">

### Author: ![Marco\_Liberati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marco_liberati/32/82953_2.png) [@Marco\_Liberati](https://discuss.elastic.co/u/Marco_Liberati)
#### Post date: [October 16, 2024, 8:34am UTC](https://discuss.elastic.co/t/how-to-apply-range-using-calculated-metric-field/368846/4 "2024-10-16T08:34:53Z")

</div>

Sorry, I think I still do not fully understand what's your request then.  
Can you clarify your request?

---

<div class="post-metadata">

### Author: ![kishorkumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kishorkumar/32/132930_2.png) [@kishorkumar](https://discuss.elastic.co/u/kishorkumar)
#### Post date: [October 16, 2024, 8:55am UTC](https://discuss.elastic.co/t/how-to-apply-range-using-calculated-metric-field/368846/5 "2024-10-16T08:55:57Z")

</div>

> [@kishorkumar](#):
>
> top of the return value

Yes, here is the output i am looking for i know by transform its achieveable. but i want if its possible via TSVB/Lens

Suppose we have a field called `total` and `customerId`, and each customer has multiple orders in the index.

We need a monthly-wise visualization like this:

**Average Monthly Sales Bucket Range**

| Bucket Name | # of Customers |
| --- | --- |
| $0 - $10 | 41 |
| $11 - $20 | |

Now, we will create a table visualization using Lens / TSVB

In the metric aggregations, I will add the **avg(total)** and unique\_count(customerId). Now, I want to create a range bucket from the returned value of this **avg(total)** metric. I don’t want to create the range on the `total` field that already exists. Instead, I want to utilize the `avg(total)`.

How we can do that in Lens / TSVB?

---

<div class="post-metadata">

### Author: ![Marco\_Liberati](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/marco_liberati/32/82953_2.png) [@Marco\_Liberati](https://discuss.elastic.co/u/Marco_Liberati)
#### Post date: [October 17, 2024, 9:14am UTC](https://discuss.elastic.co/t/how-to-apply-range-using-calculated-metric-field/368846/6 "2024-10-17T09:14:32Z")

</div>

Ok, I think I've got it.  
Interval buckets cannot be computed on top of aggs, so this won't be possible with regular Lens.  
What you could do instead is use an ES|QL table visualization with a query like this:

```auto
FROM index 
| stats t = avg(total) by customer_id 
| stats c = COUNT_DISTINCT(customer_id) by rangeFrom = (v / 500)::integer
| sort rangeFrom
| eval range = concat(
  to_String(rangeFrom * 500),
  "-",
  to_string((rangeFrom + 1) * 500)
  )
| rename c as `# of Customers`, range as Bucket
| keep Bucket, `# of Customers`

```

I've built something along the lines with `bytes` and `agent` in place of `total` and `customer_id` but the final result was this:

 ![Screenshot 2024-10-17 at 11.11.36](https://us1.discourse-cdn.com/elastic/original/3X/6/3/63d94006f8f783a470df5ffdd0f3c9b93a5d7321.png)

Note that `500` is the size of the interval, so adjust it to your use case.  
As for empty buckets, that's the limit of the query at the moment.

---

<div class="post-metadata">

### Author: ![kishorkumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kishorkumar/32/132930_2.png) [@kishorkumar](https://discuss.elastic.co/u/kishorkumar)
#### Post date: [October 17, 2024, 9:27am UTC](https://discuss.elastic.co/t/how-to-apply-range-using-calculated-metric-field/368846/7 "2024-10-17T09:27:09Z")

</div>

Yes this was possible but issue i have 8.9 version kibana 🥲 and its in new version of kibana then i will have to update the kibana first

but thanks you 🤗 for this
