# Grouping documents in kibana based on a column without losing the rest columns

**URL:** <https://discuss.elastic.co/t/grouping-documents-in-kibana-based-on-a-column-without-losing-the-rest-columns/206976>\
**Category:** Kibana\
**Created:** [November 7, 2019, 2:43pm UTC](https://discuss.elastic.co/t/grouping-documents-in-kibana-based-on-a-column-without-losing-the-rest-columns/206976 "2019-11-07T14:43:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kakavoul](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kakavoul/32/57328_2.png) [@kakavoul](https://discuss.elastic.co/u/kakavoul)\
**Post date:** [November 7, 2019, 2:43pm UTC](https://discuss.elastic.co/t/grouping-documents-in-kibana-based-on-a-column-without-losing-the-rest-columns/206976/1 "2019-11-07T14:43:55Z")

</div>

Hello,

Let's assume that i have something like that in elastic search

```
alarm_id sequence date alarm_text
1 1 2019-11-7 text of alarm1
2 1 2019-11-7 text of alarm2
2 2 2019-11-8 text of alarm2
2 3 2019-11-9 text of alarm2
3 1 2019-11-8 text of alarm3
3 2 2019-11-10 text of alarm3

```

and i want to group this information like that in kibana:

```
alarm_id count date alarm_text
1 1 2019-11-7 text of alarm1
2 3 2019-11-9 text of alarm2
3 2 2019-11-10 text of alarm3

```

which means that i want to have a grouping by alarm id, a column "count" which keeps the maximum number of "sequence" column for that alarm\_id (or alternatively the number of occurrences of that alarm\_id), the newest date and its alarm text

i managed to have only the first two columns using a data table visualization but i couldn't have the rest columns too. Is there any way (visualization or search) to do it in kibana or is not feasible?

Thanks in advance

---

<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:** [November 7, 2019, 2:58pm UTC](https://discuss.elastic.co/t/grouping-documents-in-kibana-based-on-a-column-without-losing-the-rest-columns/206976/2 "2019-11-07T14:58:54Z")

</div>

Hi, you should be able to do this with the "Top Hit" metric aggregation. It can be used to use the value of one document out of the bucket - in your case every value of the field will be the same in the bucket, so you can just do a "Top Hit" on the desired field, use the "concatenate" aggregate option and the order doesn't even matter because there will be a single value anyway.

 ![54](https://us1.discourse-cdn.com/elastic/original/3X/b/2/b2e13c94c7a7e9ec652cab107ff38de90dbf38f4.png)

---

<div class="post-metadata">

**Author:** ![kakavoul](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kakavoul/32/57328_2.png) [@kakavoul](https://discuss.elastic.co/u/kakavoul)\
**Post date:** [November 8, 2019, 9:27am UTC](https://discuss.elastic.co/t/grouping-documents-in-kibana-based-on-a-column-without-losing-the-rest-columns/206976/3 "2019-11-08T09:27:25Z")

</div>

It worked, thank you very much.

But i have one extra question. Is it possible for one or more columns to not take into consideration some rows? for example for "count" column can i exclude every row that date = 2019-11-8 (without these rows being excluded for example from alarm\_text column)?

---

<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:** [November 8, 2019, 10:41am UTC](https://discuss.elastic.co/t/grouping-documents-in-kibana-based-on-a-column-without-losing-the-rest-columns/206976/4 "2019-11-08T10:41:44Z")

</div>

When you are applying filters they are applied before the aggregation is run, so you can not apply them partially.

---

<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:** [December 6, 2019, 10:41am UTC](https://discuss.elastic.co/t/grouping-documents-in-kibana-based-on-a-column-without-losing-the-rest-columns/206976/5 "2019-12-06T10:41:53Z")

</div>

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