# Can I create a Data table which aggregates a top hit per ID?

**URL:** <https://discuss.elastic.co/t/can-i-create-a-data-table-which-aggregates-a-top-hit-per-id/285089>\
**Category:** Kibana\
**Created:** [September 24, 2021, 2:28pm UTC](https://discuss.elastic.co/t/can-i-create-a-data-table-which-aggregates-a-top-hit-per-id/285089 "2021-09-24T14:28:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joao\_Silva](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joao_silva/32/100378_2.png) [@Joao\_Silva](https://discuss.elastic.co/u/Joao_Silva)\
**Post date:** [September 24, 2021, 2:28pm UTC](https://discuss.elastic.co/t/can-i-create-a-data-table-which-aggregates-a-top-hit-per-id/285089/1 "2021-09-24T14:28:59Z")

</div>

Imagine that I have documents with the fields ID, ValueA, ValueB and that my index for each ID, let's say from 1 to 50 have some random values for A and B.

Is it possible to build a visualization with 50 rows, one per ID and aggregate for each row the latest value for each property (ValueA, ValueB), filtered by the ID key?

---

<div class="post-metadata">

**Author:** ![ghudgins](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ghudgins/32/138532_2.png) [@ghudgins](https://discuss.elastic.co/u/ghudgins)\
**Post date:** [September 27, 2021, 2:38pm UTC](https://discuss.elastic.co/t/can-i-create-a-data-table-which-aggregates-a-top-hit-per-id/285089/2 "2021-09-27T14:38:47Z")

</div>

"Last Value" in Lens uses top\_hits under the hood and likely could accomplish this. Here's an example where "host" is an example field and my "host.cpu.usage" is an example metric that i'm using last value / top\_hits on

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

---

<div class="post-metadata">

**Author:** ![Joao\_Silva](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joao_silva/32/100378_2.png) [@Joao\_Silva](https://discuss.elastic.co/u/Joao_Silva)\
**Post date:** [September 27, 2021, 2:58pm UTC](https://discuss.elastic.co/t/can-i-create-a-data-table-which-aggregates-a-top-hit-per-id/285089/3 "2021-09-27T14:58:21Z")

</div>

Hi Graham,

Thank you for the advice, I was able to solve it a while ago, not sure if my solution was the best but what I did was a scripted field for my ID (the id was text and I converted it to a number), the I added the Top values for the IDs and sorted it, then in the metrics I just added the properties I needed using the Last value and it worked fine.

Sorry for not updating the status of this question earlier but I sorted it out just a while ago.

---

<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 25, 2021, 2:59pm UTC](https://discuss.elastic.co/t/can-i-create-a-data-table-which-aggregates-a-top-hit-per-id/285089/4 "2021-10-25T14:59:21Z")

</div>

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