# How do I count and visualize only latest doc based on certain field?

**URL:** <https://discuss.elastic.co/t/how-do-i-count-and-visualize-only-latest-doc-based-on-certain-field/343457>\
**Category:** Kibana\
**Tags:** dashboard, data-views, visualisation\
**Created:** [September 20, 2023, 12:24pm UTC](https://discuss.elastic.co/t/how-do-i-count-and-visualize-only-latest-doc-based-on-certain-field/343457 "2023-09-20T12:24:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Doron\_Abramovich](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doron_abramovich/32/131186_2.png) [@Doron\_Abramovich](https://discuss.elastic.co/u/Doron_Abramovich)\
**Post date:** [September 20, 2023, 12:24pm UTC](https://discuss.elastic.co/t/how-do-i-count-and-visualize-only-latest-doc-based-on-certain-field/343457/1 "2023-09-20T12:24:35Z")

</div>

Hi everyone 🙂  
First post here after working for over year with elastic.

I have an index with docs representing items moving from station to station, each doc represents a station.

For example - an item moved between two station will have two docs (random id's):

```auto
{
          "sn": "N00000000000",
          "process_id": "0001",
          "process_name": "distance",
          "station_name": "LAPTOP-999",
          "user": "doron",
          "logged": "2023-09-19T14:09:49.342013+03:00"
}
{
          "sn": "N00000000000",
          "process_id": "0002",
          "process_name": "flow",
          "station_name": "LAPTOP-999",
          "user": "doron",
          "logged": "2023-09-20T11:04:12.342013+03:00"
}

```

I am trying to do a simple metric box in the kibana dashboard to show how many items are in each station, but I want to exclude the ones that already moved to the next station.

That means I'm trying to use count for a sub data only contains the latest occurrence of each **"sn"**

In our example case, The count boxes will be:  
**Distance:** 0  
**Flow:** 1

Please let me know if you further questions!  
I have been stuck on this for a while and really like to understand how to do such a thing generally

_Thanks alot!,_  
_Doron_

---

<div class="post-metadata">

**Author:** ![sholzhauer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sholzhauer/32/110282_2.png) [@sholzhauer](https://discuss.elastic.co/u/sholzhauer)\
**Post date:** [September 20, 2023, 12:36pm UTC](https://discuss.elastic.co/t/how-do-i-count-and-visualize-only-latest-doc-based-on-certain-field/343457/2 "2023-09-20T12:36:14Z")

</div>

Hi, you can utilize the transform feature to get to your desired result.

If you created a [latest transform](https://www.elastic.co/guide/en/elasticsearch/reference/current/transform-overview.html#latest-transform-overview) on your data with a `group_by` on the item id the result will be an index with a single document per item.

This index can then be used as your source data to create the visualizations you need.

---

<div class="post-metadata">

**Author:** ![Doron\_Abramovich](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/doron_abramovich/32/131186_2.png) [@Doron\_Abramovich](https://discuss.elastic.co/u/Doron_Abramovich)\
**Post date:** [September 20, 2023, 12:54pm UTC](https://discuss.elastic.co/t/how-do-i-count-and-visualize-only-latest-doc-based-on-certain-field/343457/4 "2023-09-20T12:54:47Z")

</div>

Amazing!  
Worked and was easier then I thought

Don't understand how I missed it after so many searches in Google haha

Glad I posted here and thanks a lot! 😉

---

<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 18, 2023, 12:55pm UTC](https://discuss.elastic.co/t/how-do-i-count-and-visualize-only-latest-doc-based-on-certain-field/343457/5 "2023-10-18T12:55:20Z")

</div>

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