# Aggregation by Maximum Date Doesn't Work Well

**URL:** https://discuss.elastic.co/t/aggregation-by-maximum-date-doesnt-work-well/192752
**Category:** Kibana
**Created:** [July 29, 2019, 5:21pm UTC](https://discuss.elastic.co/t/aggregation-by-maximum-date-doesnt-work-well/192752 "2019-07-29T17:21:36Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Carlos.limaElaw](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@Carlos.limaElaw](https://discuss.elastic.co/u/Carlos.limaElaw)
#### Post date: [July 29, 2019, 5:21pm UTC](https://discuss.elastic.co/t/aggregation-by-maximum-date-doesnt-work-well/192752/1 "2019-07-29T17:21:36Z")

</div>

Hi, there!

I'm trying to sort a vertical bar Chart by a agregation of maximum date like this:  
 ![metricas](https://us1.discourse-cdn.com/elastic/original/3X/c/7/c78b2481e0166f54b87abcfbddf3d907566e7f4c.png)

**"cc\_mes\_ano\_registro"** is a scripted field that returns the Month  
and Year of a date field( **data\_de\_registro** ) as a string (because string fields are aligned in the center of bars as you can see in "December - 2018"), but there's a problem:

![eixoY](https://us1.discourse-cdn.com/elastic/original/3X/6/9/699c5ff40b14491a442043236b498dd3728314ea.png)

As you can see, March, July and August - 2018 are ahead of July 2019.  
In other words, it is not ordered by the maximum date in each bucket.

And if I order by descending it goes like this:

![eixoYDesc](https://us1.discourse-cdn.com/elastic/original/3X/2/0/20b0af64881f3b02113bab013a0e9a5ccecd867a.png)

It's completely different.

This is the "cc\_mes\_ano\_registro" script:

```
if(doc['data_de_registro'].size() > 0 ){
  def registroMillis = doc['data_de_registro'].value.getMillis();
  ZoneId timeZone = ZoneId.of(ZoneId.SHORT_IDS.get('BET'));
  LocalDate dataRegistro = 
  LocalDateTime.ofInstant(Instant.ofEpochMilli(registroMillis),timeZone).toLocalDate();

  def mesAno = dataRegistro.getMonth()+" - "+dataRegistro.getYear();
  return mesAno;
}
return "N/A";

```

Some solution, something wrong that I can't see or it's just a bug?

Thank you!

---

<div class="post-metadata">

### Author: ![nickpeihl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nickpeihl/32/112622_2.png) [@nickpeihl](https://discuss.elastic.co/u/nickpeihl)
#### Post date: [July 29, 2019, 10:56pm UTC](https://discuss.elastic.co/t/aggregation-by-maximum-date-doesnt-work-well/192752/2 "2019-07-29T22:56:15Z")

</div>

Is there a reason you are not using a Date Histogram aggregation? It might make this much easier without the need for a scripted field.

 ![Screenshot_2019-07-29%20Kibana(5)](https://us1.discourse-cdn.com/elastic/original/3X/9/5/95b14e7e9fd1c7a5640e36df6d0033d635002910.png)

---

<div class="post-metadata">

### Author: ![Carlos.limaElaw](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@Carlos.limaElaw](https://discuss.elastic.co/u/Carlos.limaElaw)
#### Post date: [July 31, 2019, 2:28pm UTC](https://discuss.elastic.co/t/aggregation-by-maximum-date-doesnt-work-well/192752/3 "2019-07-31T14:28:12Z")

</div>

Hi, Nick! Thanks for response me.

I'm using a scripted field because Date Histogram put the label in the bar's corner and I need in the middle.

---

<div class="post-metadata">

### Author: ![nickpeihl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nickpeihl/32/112622_2.png) [@nickpeihl](https://discuss.elastic.co/u/nickpeihl)
#### Post date: [July 31, 2019, 6:03pm UTC](https://discuss.elastic.co/t/aggregation-by-maximum-date-doesnt-work-well/192752/4 "2019-07-31T18:03:17Z")

</div>

HI Carlos,

I see now. We have an enhancement request for this already. [https://github.com/elastic/kibana/issues/38494](https://github.com/elastic/kibana/issues/38494)

You can use the Inspect button on your Visualization to see the results of the query. You can also change the View from Data to Requests to see the request and response sent to/from Elasticsearch. This might offer a clue why the ordering is not what you are looking for. [https://www.elastic.co/guide/en/kibana/current/vis-inspector.html](https://www.elastic.co/guide/en/kibana/current/vis-inspector.html)

---

<div class="post-metadata">

### Author: ![Carlos.limaElaw](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@Carlos.limaElaw](https://discuss.elastic.co/u/Carlos.limaElaw)
#### Post date: [July 31, 2019, 7:55pm UTC](https://discuss.elastic.co/t/aggregation-by-maximum-date-doesnt-work-well/192752/5 "2019-07-31T19:55:12Z")

</div>

I discovered something!

My field local\_trabalho.keyword have some null data, and this is breaking the axis order.  
So I did a scripted field that returns "N/I" for null values and solved the problem.

Thanks for answer me anyway!

---

<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: [August 28, 2019, 7:55pm UTC](https://discuss.elastic.co/t/aggregation-by-maximum-date-doesnt-work-well/192752/6 "2019-08-28T19:55:16Z")

</div>

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