# Formula in lens metrics in kibana

**URL:** <https://discuss.elastic.co/t/formula-in-lens-metrics-in-kibana/358201>\
**Category:** Kibana\
**Created:** [April 25, 2024, 10:29am UTC](https://discuss.elastic.co/t/formula-in-lens-metrics-in-kibana/358201 "2024-04-25T10:29:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![aram215](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@aram215](https://discuss.elastic.co/u/aram215)\
**Post date:** [April 25, 2024, 10:29am UTC](https://discuss.elastic.co/t/formula-in-lens-metrics-in-kibana/358201/1 "2024-04-25T10:29:14Z")

</div>

How to create a formula in lens-metrics that gives me the sum of 'value A' where there's a max in 'value B'.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 25, 2024, 12:18pm UTC](https://discuss.elastic.co/t/formula-in-lens-metrics-in-kibana/358201/2 "2024-04-25T12:18:59Z")

</div>

I'm not sure I understood this part:

> where there's a max in 'value B'.

Could you clarify with an example? Is that a query that you want to run?

---

<div class="post-metadata">

**Author:** ![aram215](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@aram215](https://discuss.elastic.co/u/aram215)\
**Post date:** [April 25, 2024, 1:28pm UTC](https://discuss.elastic.co/t/formula-in-lens-metrics-in-kibana/358201/3 "2024-04-25T13:28:46Z")

</div>

I have 2 variables. One is a metric and the other could work either as a category or as a phase number.  
What I want is to see the sum of the metric (total) for the last category. I could manually filter it but the phase number increases over time.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 25, 2024, 5:49pm UTC](https://discuss.elastic.co/t/formula-in-lens-metrics-in-kibana/358201/4 "2024-04-25T17:49:24Z")

</div>

Could you share a sample document?

---

<div class="post-metadata">

**Author:** ![aram215](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@aram215](https://discuss.elastic.co/u/aram215)\
**Post date:** [April 29, 2024, 7:18am UTC](https://discuss.elastic.co/t/formula-in-lens-metrics-in-kibana/358201/5 "2024-04-29T07:18:07Z")

</div>

A clear example would be:  
Category1/Phases goes 1,2,3,4,5,6, and so on.  
Metric would be the value per category2 in each phase.  
So, {  
(category1: 1, category2: A, metric:5),  
(category1: 1, category2: B, metric:7),  
(category1: 2, category2: A, metric:8),  
(category1: 2, category2: C, metric:4)}

What I'd like to do is to sum (get the total) category2 per category1  
(category1: 2, category2: A, metric:8), (category1: 2, category2: C, metric:4 = 12) when it's the latest category1/phase, in this case 2, but as the dashboard refreses could be 1 or 5.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 29, 2024, 8:00am UTC](https://discuss.elastic.co/t/formula-in-lens-metrics-in-kibana/358201/6 "2024-04-29T08:00:19Z")

</div>

I still don't understand what exactly you are computing.  
So, could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script is something anyone can **copy and paste in Kibana dev console** , click on the run button to reproduce your use case. It will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

Have a look at the [Elastic Stack and Solutions Help · Forums and Slack | Elastic](https://elastic.co/community/help) page. It contains also lot of useful information on how to ask for help.
