# How to visualize max aggregation inside terms aggregation in Kibana?

**URL:** <https://discuss.elastic.co/t/how-to-visualize-max-aggregation-inside-terms-aggregation-in-kibana/323406>\
**Category:** Kibana\
**Created:** [January 18, 2023, 12:19pm UTC](https://discuss.elastic.co/t/how-to-visualize-max-aggregation-inside-terms-aggregation-in-kibana/323406 "2023-01-18T12:19:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cr\_168328](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@cr\_168328](https://discuss.elastic.co/u/cr_168328)\
**Post date:** [January 18, 2023, 12:19pm UTC](https://discuss.elastic.co/t/how-to-visualize-max-aggregation-inside-terms-aggregation-in-kibana/323406/1 "2023-01-18T12:19:01Z")

</div>

I have an index `exams` which contains data related to students who passed some exams.

A student can attempt an exam multiple times. Hence there may be more than one document in the index for a particular student and exam, with a different `passedOn` date.

I want to create a Kibana Visualization of the count of unique exam completions, ignoring the duplicate values of `student`-`exam` pair.

The following query gives the latest date of passing each `exam` by each `student`:

```auto
GET exams/_search
{
  "size": 0, 
  "_source": false,
  "aggs": {
    "student": {
      "terms": {
        "field": "studentId",
        "size": 20000
      },
      "aggs": {
        "exam": {
          "terms": {
            "field": "examId",
            "size": 1000
          },
          "aggs": {
            "latest": {
              "max": {
                "field": "passedOn"
              }
            }
          }
        }
      }
    }
  }
}

```

How can I visualize the unique count of `student`-`exam` pair, either using the latest `passedOn` date or using `runtime fields` or `scripted fields` or some other way?

---

<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 18, 2023, 2:47pm UTC](https://discuss.elastic.co/t/how-to-visualize-max-aggregation-inside-terms-aggregation-in-kibana/323406/2 "2023-01-18T14:47:45Z")

</div>

Hi @cr_168328 ,

what if instead of focusing on the last value of an exam, you just count the unique\_count of passed exams per student?

I mean using the `unique count` Lens operation, with a filter on `passed: true` in it, and a break down per student.  
Would that work?

---

<div class="post-metadata">

**Author:** ![cr\_168328](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@cr\_168328](https://discuss.elastic.co/u/cr_168328)\
**Post date:** [January 19, 2023, 6:16am UTC](https://discuss.elastic.co/t/how-to-visualize-max-aggregation-inside-terms-aggregation-in-kibana/323406/3 "2023-01-19T06:16:08Z")

</div>

Hi @Marco_Liberati ,

Thanks for the reply.

What I want exactly is not a tabular data, but a count of unique `student`-`exam` pairs.

For eg., if I have the following data:

> **Student** | **Exam** | **Passed On**  
> s1 | e1 | 2022/01/01  
> s1 | e1 | 2022/01/02  
> s1 | e1 | 2022/01/03  
> s1 | e2 | 2022/01/03  
> s1 | e2 | 2022/01/04  
> s1 | e2 | 2022/01/05  
> s1 | e3 | 2022/01/03  
> s1 | e3 | 2022/01/04  
> s2 | e1 | 2022/01/03  
> s2 | e1 | 2022/01/04  
> s2 | e1 | 2022/01/05  
> s2 | e2 | 2022/01/03  
> s2 | e2 | 2022/01/04

The required result is the **count of unique `student`-`exam` pairs = 5**.

---

<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, 2023, 10:16am UTC](https://discuss.elastic.co/t/how-to-visualize-max-aggregation-inside-terms-aggregation-in-kibana/323406/4 "2023-01-19T10:16:00Z")

</div>

So there's no actual information about passing of an exam, other than the latest `PassedOn` date?  
There's no result field?

---

<div class="post-metadata">

**Author:** ![cr\_168328](https://avatars.discourse-cdn.com/v4/letter/c/76d3ee/32.png) [@cr\_168328](https://discuss.elastic.co/u/cr_168328)\
**Post date:** [January 19, 2023, 11:25am UTC](https://discuss.elastic.co/t/how-to-visualize-max-aggregation-inside-terms-aggregation-in-kibana/323406/5 "2023-01-19T11:25:57Z")

</div>

No. There is no `passed` field. Actually, the index contains details of only students who have passed each exam

---

<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 16, 2023, 11:26am UTC](https://discuss.elastic.co/t/how-to-visualize-max-aggregation-inside-terms-aggregation-in-kibana/323406/6 "2023-02-16T11:26:29Z")

</div>

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