# Format for multi-level aggregation in Vega

**URL:** <https://discuss.elastic.co/t/format-for-multi-level-aggregation-in-vega/127193>\
**Category:** Kibana\
**Created:** [April 8, 2018, 6:43am UTC](https://discuss.elastic.co/t/format-for-multi-level-aggregation-in-vega/127193 "2018-04-08T06:43:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steven\_Ensslen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/steven_ensslen/32/26984_2.png) [@Steven\_Ensslen](https://discuss.elastic.co/u/Steven_Ensslen)\
**Post date:** [April 8, 2018, 6:43am UTC](https://discuss.elastic.co/t/format-for-multi-level-aggregation-in-vega/127193/1 "2018-04-08T06:43:03Z")

</div>

If I have an ES query that returns JSON like:

```
 "aggregations": {
  "per_buyer": {
   "doc_count_error_upper_bound": 0,
   "sum_other_doc_count": 0,
   "buckets": [
    {
      "key": "Example-buyer",
      "doc_count": 45,
      "per_cart": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 6,
        "buckets": [
          {
            "key": "397105FB",
            "doc_count": 8,
            "net": {
              "value": 10
            }
          }
        ]
      }
    }
  ]
  }
  }
 },

```

What is the correct "format" to display data per "per\_cart" bucket? Anything deeper than `aggregations.per_buyer.buckets` returns the error "\_.aggregations.per\_buyer.buckets.per\_cart is undefined" . `VEGA_DEBUG.view.data` shows that some `aggregations.per_buyer.buckets` have a per\_cart object, which in turn have buckets. Filtering so that all buckets have per\_cart objects does not change anything.

---

<div class="post-metadata">

**Author:** ![rashmi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rashmi/32/16391_2.png) [@rashmi](https://discuss.elastic.co/u/rashmi)\
**Post date:** [April 9, 2018, 4:47am UTC](https://discuss.elastic.co/t/format-for-multi-level-aggregation-in-vega/127193/2 "2018-04-09T04:47:05Z")

</div>

@nyuriks - My colleague Yuri can answer this question when he finds time.

Cheers  
Rashmi

---

<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:** [May 7, 2018, 4:47am UTC](https://discuss.elastic.co/t/format-for-multi-level-aggregation-in-vega/127193/3 "2018-05-07T04:47:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nyuriks](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nyuriks/32/26171_2.png) [@nyuriks](https://discuss.elastic.co/u/nyuriks)\
**Post date:** [October 15, 2018, 8:41pm UTC](https://discuss.elastic.co/t/format-for-multi-level-aggregation-in-vega/127193/4 "2018-10-15T20:41:35Z")

</div>

@Steven_Ensslen your `format` must be `aggregations.per_buyer.buckets`, simply because that's the list of data that you need. Each data element in that bucket may contain a sub-list of buckets which you have to access from Vega itself. Now, if the sub-list will always have just a single bucket (e.g. if you are summing-up the total per main bucket), you can access it either directly from a mark, e.g. `datum.per_cart.buckets[0].net.value`, or you can create a formula transform that would copy that value into a top level field, e.g. `{type:'formula', as:'net_value', expr: 'datum.per_cart.buckets[0].net.value'}`, and use the `net_value` field in the mark. If on the other hand you have multiple items in the sub-list, you could use the `flatten` transform to flatten out the sub-buckets into a non-hierarchical list of items, and then use various transformations to get the data into the format you need.
