# Clamp function for two dynamic value doesn't work

**URL:** <https://discuss.elastic.co/t/clamp-function-for-two-dynamic-value-doesnt-work/294844>\
**Category:** Kibana\
**Tags:** lens\
**Created:** [January 19, 2022, 3:48pm UTC](https://discuss.elastic.co/t/clamp-function-for-two-dynamic-value-doesnt-work/294844 "2022-01-19T15:48:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 19, 2022, 3:48pm UTC](https://discuss.elastic.co/t/clamp-function-for-two-dynamic-value-doesnt-work/294844/1 "2022-01-19T15:48:50Z")

</div>

Hi everyone,

I saw this [post](https://discuss.elastic.co/t/dec-4th-2021-en-advanced-trick-and-tips-in-lens/289935#formula-comparisons-6) and tried.

To get max of fieldA and fieldB,  
I tried Formula:  
`clamp(max(fieldA), max(fieldB), 10000)`,  
but it does not worked and the cell remains empty.

in my case, `max(fieldA)=3` and `max(fieldB)=5`, then `clamp(max(fieldA), max(fieldB), 100000)` must be 5, isn't it? Is something wrong?

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/5/3/53ba1e362c651b06aa20c7c0aab54df24980053c.png)

`clamp(3,5,10000)` returns `5`.

I tried this formula because the formula doesn't work fine if the difference is less than 1.

When max(fieldA)=4.8 and max(fieldB)=5 for example,

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

```auto
clamp(max(fieldA)-max(fieldB),0,1)*max(fieldA) + clamp(max(fieldB)-max(fieldA),0,1)*max(fieldB) 

```

returns `0 + (5-4.8)*5 = 1`. It must be `5`.

How to reproduce:  
case1:

```auto
PUT /test_lens_formula/

POST /test_lens_formula/_bulk
{"index":{}}
{"fieldA":3, "fieldB":5}
{"index":{}}
{"fieldA":1, "fieldB":2}

```

case2:

```auto
PUT /test_lens_formula2/

POST /test_lens_formula2/_bulk
{"index":{}}
{"fieldA":4.8, "fieldB":5}
{"index":{}}
{"fieldA":1, "fieldB":2}

```

---

<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:** [January 19, 2022, 4:32pm UTC](https://discuss.elastic.co/t/clamp-function-for-two-dynamic-value-doesnt-work/294844/2 "2022-01-19T16:32:39Z")

</div>

Hi @Tomo_M

one simple trick would be to multiply the first argument of both `clamp` by an arbitrary big number you're sure it would make the difference \> 1.  
For instance in your example you could use 100:

```auto
clamp( 100 * (max(fieldA)-max(fieldB)) ,0,1)*max(fieldA) + clamp(100 * (max(fieldB)-max(fieldA)) ,0,1)*max(fieldB)

```

Using `100` even if the difference between the 2 max is 0.2, it will become `20`, which will be converted by 1.  
As I mention in the post, the `clamp` is a convenient "trick" rather than a final solution. 😅

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 20, 2022, 2:15pm UTC](https://discuss.elastic.co/t/clamp-function-for-two-dynamic-value-doesnt-work/294844/3 "2022-01-20T14:15:36Z")

</div>

Thank you for a nice workaround! It may cover a lot more cases.

Do you have any idea about why `clamp(max(fieldA), max(fieldB), 100000)` returns empty?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [February 17, 2022, 2:16pm UTC](https://discuss.elastic.co/t/clamp-function-for-two-dynamic-value-doesnt-work/294844/4 "2022-02-17T14:16:24Z")

</div>

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