# Visualizing a metric on two unique counts

**URL:** <https://discuss.elastic.co/t/visualizing-a-metric-on-two-unique-counts/249562>\
**Category:** Kibana\
**Created:** [September 22, 2020, 4:30pm UTC](https://discuss.elastic.co/t/visualizing-a-metric-on-two-unique-counts/249562 "2020-09-22T16:30:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwetzel](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@jwetzel](https://discuss.elastic.co/u/jwetzel)\
**Post date:** [September 22, 2020, 4:30pm UTC](https://discuss.elastic.co/t/visualizing-a-metric-on-two-unique-counts/249562/1 "2020-09-22T16:30:05Z")

</div>

I am collecting custom logs from a web server. I am trying to summarize what % of unique users encounter an error. I am having trouble finding a visualization that will do a % calculation on two unique (cardinatily) values.  
Oversimplified version of a log entry in kibana  
username: "[foo@example.com](mailto:foo@example.com)", loglevel: "ERROR"  
username: "[bar@example.com](mailto:bar@example.com)", loglevel: "WARNING"  
Right now, I can use the Metric visualizer to get the Unique count of the the number of different users that have appeared in my logs (lets say I have 50 unique users to my site), which is good. In a separate vizualizer, I can do the same thing, and add a KQL filter for loglevel:"ERROR", and i can see how many unique users encountered an error (lets say 10 users ran into errors). How can I create a vizualization box to do the 10 divided by 50 calculation and display 20%?

I tried the Filter Ratio on the TSVB, which works in doing the % calculation on other metrics, but i cannot specify Unqiue Count within the filter ratio on TSVB.

I tried the "Extended Metrics" plugin, which lets me do math on two Unique Counts, but that does not allow me to do a separate query for the errors as the denomninator

I tried to use the API and a series of aggregations to get the same numbers, but cannot fit a script into this call anywhere to do the division

```auto
> POST /index-*/_search?size=0
> {
> "aggs": {
> "unique_user_emails": {
> "cardinality": {
> "field": "useremail"
> }
> },
> "found_errors": {
> "filter": { 
> "term": { 
> "loglevel": "ERROR"
> } 
> },
> "aggs": {
> "uniquewitherrors": { 
> "cardinality": { 
> "field": "useremail" 
> } 
> }
> }
> }
> }
> ...
> }

```

Which works, and I get

```auto
> "aggregations" : {
> "unique_user_emails" : {
> "value" : 50
> },
> "found_errors" : {
> "doc_count" : 338,
> "uniquewitherrors" : {
> "value" : 10
> }
> }
> }

```

but I get errors regardless of where i try to a script to do the division

```auto
> "script": {
> "lang": "painless",
> "source": "found_errors>uniquewitherrors.value / unique_user_emails.value"
> }

```

I am beginning to think that what I am suggesting is not possible. Anyone have suggestions of other plugins or other API docs I should read to do calculations between two Unique Counts?

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [September 23, 2020, 12:21pm UTC](https://discuss.elastic.co/t/visualizing-a-metric-on-two-unique-counts/249562/2 "2020-09-23T12:21:38Z")

</div>

Which version of Kibana are you running? TSVB should support using Cardinality (which is the unique count):

 ![Screenshot 2020-09-23 at 14.18.07](https://us1.discourse-cdn.com/elastic/original/3X/5/a/5a20e624a444c3cdbedd2ad739621bfb5605c2dd.png)

Also, by default TSVB only shows the calculated value for the last time bucket, you can change that to the whole time range in the "Panel options:

 ![Screenshot 2020-09-23 at 14.19.04](https://us1.discourse-cdn.com/elastic/original/3X/5/0/5002bc7723062e7fc41f79919eda7e0857a67d30.png)

---

<div class="post-metadata">

**Author:** ![jwetzel](https://avatars.discourse-cdn.com/v4/letter/j/2bfe46/32.png) [@jwetzel](https://discuss.elastic.co/u/jwetzel)\
**Post date:** [September 23, 2020, 1:49pm UTC](https://discuss.elastic.co/t/visualizing-a-metric-on-two-unique-counts/249562/3 "2020-09-23T13:49:05Z")

</div>

Thanks so much for your reply!  
I am running version 7.4.2. Now that I see your screenshot, I realize I was hung up on trying to do a Cardinality aggregation, and then adding an additional math aggregation, not realizing I had to select the filter ratio aggregation, and then select Cardinality as my metric aggregation. That's exactly what I was looking for

---

<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:** [October 21, 2020, 1:49pm UTC](https://discuss.elastic.co/t/visualizing-a-metric-on-two-unique-counts/249562/4 "2020-10-21T13:49:09Z")

</div>

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