# Custom "Order By" Aggregation for Kibana Bar Chart

**URL:** <https://discuss.elastic.co/t/custom-order-by-aggregation-for-kibana-bar-chart/218642>\
**Category:** Kibana\
**Created:** [February 10, 2020, 4:51pm UTC](https://discuss.elastic.co/t/custom-order-by-aggregation-for-kibana-bar-chart/218642 "2020-02-10T16:51:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jade](https://avatars.discourse-cdn.com/v4/letter/j/a8b319/32.png) [@Jade](https://discuss.elastic.co/u/Jade)\
**Post date:** [February 10, 2020, 4:51pm UTC](https://discuss.elastic.co/t/custom-order-by-aggregation-for-kibana-bar-chart/218642/1 "2020-02-10T16:51:33Z")

</div>

I have a bar chart and have used split series into 2 time buckets - the past 3 months (now/M-3M to now/M), and 3 months prior to that (now/M-6M to now/M-3M).

I would simply like to order the chart by the values of only the past 3 months (now/M-3M to now/M), rather than the summation of the 2 buckets.

I figured I would need to use the JSON input under Order By \> Custom Metric, with the following code:  
{ "query" : {"range": { "time\_parm": { "gte" : "now/M-3M" }}}}

 ![JSONInput](https://us1.discourse-cdn.com/elastic/original/3X/8/a/8a85741e20365ad6567aff5d8b22346c849a8dc4.jpeg)

The above code gives an "Internal Server Error". How can I do this? Please help!

Kibana 7.1.0

---

<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:** [February 12, 2020, 2:30pm UTC](https://discuss.elastic.co/t/custom-order-by-aggregation-for-kibana-bar-chart/218642/2 "2020-02-12T14:30:19Z")

</div>

Unfortunately you can't put a `query` inside a metric definition. However if you put the "Split series" aggregation first (above the X-Axis aggregation), then the visualization will plot the terms in the order of the first bucket of the top level aggregation which is the behavior you need:

- Split series by your time ranges
- X axis terms aggregation sort by the metric you need (as this is nested within the ranges aggregation, the ordering will be done separately for both series - as bars can only appear in one of the two orders, the order of the first defined range is picked)

 ![Screenshot 2020-02-12 at 15.27.21](https://us1.discourse-cdn.com/elastic/original/3X/7/e/7ef08f8b56718d575ad47962eab705ff45f7567b.png)

---

<div class="post-metadata">

**Author:** ![Jade](https://avatars.discourse-cdn.com/v4/letter/j/a8b319/32.png) [@Jade](https://discuss.elastic.co/u/Jade)\
**Post date:** [February 13, 2020, 7:04pm UTC](https://discuss.elastic.co/t/custom-order-by-aggregation-for-kibana-bar-chart/218642/3 "2020-02-13T19:04:38Z")

</div>

thank you! This was helpful. However, for some reason, regardless of the order I put the date ranges in, it will only order by the oldest date range (which, unfortunately, is not the one I want)

Purple: Last 3 months  
Blue: 6 months ago - 3 months ago

 ![Bar1](https://us1.discourse-cdn.com/elastic/original/3X/a/4/a41cdeadccecfebb48aaf7a4821743c8ba081472.jpeg)

 ![Bar2](https://us1.discourse-cdn.com/elastic/original/3X/1/b/1b02121152412d9d6fdbc0ba801afc00ca5d4db6.jpeg)

---

<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:** [February 14, 2020, 8:18am UTC](https://discuss.elastic.co/t/custom-order-by-aggregation-for-kibana-bar-chart/218642/4 "2020-02-14T08:18:48Z")

</div>

Ah, I missed that. Seems like Elasticsearch is doing some implicit ordering for ranges. Can you try using the "filters" aggregation instead with the same ranges? At least for my tests this orders them by `Filter 1`.

 ![Screenshot 2020-02-14 at 09.16.45](https://us1.discourse-cdn.com/elastic/original/3X/4/3/4325ffe2c6f2e4d18f4fde44ada9fc2d85d2a9a7.png)

---

<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:** [March 13, 2020, 8:18am UTC](https://discuss.elastic.co/t/custom-order-by-aggregation-for-kibana-bar-chart/218642/5 "2020-03-13T08:18:49Z")

</div>

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