# Create histogram with group by count (binned data)

**URL:** <https://discuss.elastic.co/t/create-histogram-with-group-by-count-binned-data/250512>\
**Category:** Kibana\
**Created:** [September 30, 2020, 1:20pm UTC](https://discuss.elastic.co/t/create-histogram-with-group-by-count-binned-data/250512 "2020-09-30T13:20:14Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pond](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@Pond](https://discuss.elastic.co/u/Pond)\
**Post date:** [September 30, 2020, 1:20pm UTC](https://discuss.elastic.co/t/create-histogram-with-group-by-count-binned-data/250512/1 "2020-09-30T13:20:14Z")

</div>

Hi,

I have devices users are using:

{"userID":"1", "deviceID": "11"}  
{"userID":"1", "deviceID": "12"}  
{"userID":"1", "deviceID": "13"}  
{"userID":"2", "deviceID": "21"}  
{"userID":"2", "deviceID": "22"}  
{"userID":"3", "deviceID": "31"}  
{"userID":"3", "deviceID": "32"}  
{"userID":"4", "deviceID": "41"}

As the result, I want to receive how many users are using 1-2 devices, who use 3-4 devices etc.:  
1-2 devices — 3 users  
3-4 devices — 1 user

I found [this](https://discuss.elastic.co/t/how-to-make-histogram-by-grouped-aggregation-count/124709) topic, which is very similar to what I need. But this solution didn't work for me:

 ![Screenshot (50)](https://us1.discourse-cdn.com/elastic/original/3X/4/d/4d5451f5a0ecabb8eabb1b0ad1122032afcf6c1b.png)

I also tried to create a histogram with binned data based on [this](https://vega.github.io/vega-lite/examples/bar_binned_data.html) one. But I'm not sure how to use data I have, cause I need first count the number of devices each user use and then group users based on that aggregated data. Are there any options to make it? Thanks!

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [September 30, 2020, 3:30pm UTC](https://discuss.elastic.co/t/create-histogram-with-group-by-count-binned-data/250512/2 "2020-09-30T15:30:43Z")

</div>

I can help debug your Vega spec and ES queries. The first thing I notice is that you have `index: "/events-*"` instead of `index: event-*`. Secondly, it might help if you showed the rest of the Vega spec in your `encoding` section.

---

<div class="post-metadata">

**Author:** ![Pond](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@Pond](https://discuss.elastic.co/u/Pond)\
**Post date:** [October 1, 2020, 7:47am UTC](https://discuss.elastic.co/t/create-histogram-with-group-by-count-binned-data/250512/3 "2020-10-01T07:47:16Z")

</div>

Sure, I attached all the code below. Thank you!

````
``` 
{
      "$schema": "https://vega.github.io/schema/vega-lite/v2.json"
      "data": {
        "url": {
          "%timefield%": "@timestamp"
          "%context%": "true"
          "index": "/events-*"
          "body": {
        "size": 0, 
      "aggs": {
              "users": {
                "terms": {
                  "field": "userID", 
                  "size": 10000,
                  "order" : {"eventCount": "desc"}
                }, 
      "aggs": {
                  "eventCount": {
                    "cardinality": {
                      "field": "deviceID"}
                  }
                }
              }
            }
          }
        }
      "format": {"property": "aggregations.users.buckets"}
      }
      "mark": "bar"
      "transform": [
        {
          "aggregate": [
            {"op": "count", "field": "key", "as": "usercount"}
          ]
          "groupby": ["eventCount.value"]
        }
      ]
      "encoding": {
        "x": { "field": "eventCount\\.value", "type": "ordinal", "sort": "descending" }
        "y": { "field": "usercount", "type": "quantitative" }
      }

````

```auto

```

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [October 1, 2020, 3:01pm UTC](https://discuss.elastic.co/t/create-histogram-with-group-by-count-binned-data/250512/4 "2020-10-01T15:01:00Z")

</div>

It looks fine except for the issue I already pointed out, your index name should likely be `events-*` without a forward slash

---

<div class="post-metadata">

**Author:** ![Pond](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@Pond](https://discuss.elastic.co/u/Pond)\
**Post date:** [October 1, 2020, 3:22pm UTC](https://discuss.elastic.co/t/create-histogram-with-group-by-count-binned-data/250512/5 "2020-10-01T15:22:18Z")

</div>

I changed the index name, and the error disappeared, but no results are showing:

 ![Screenshot](https://us1.discourse-cdn.com/elastic/original/3X/f/8/f8bf192cedea2e157386106b197c2fb0e63d38f3.png)

What could be the problem?

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [October 1, 2020, 5:52pm UTC](https://discuss.elastic.co/t/create-histogram-with-group-by-count-binned-data/250512/6 "2020-10-01T17:52:39Z")

</div>

When it's empty like that, Vega is matching no results with the field names you're using. Maybe the issue is that you've written `eventCount\\.value` instead of `eventCount.value` for the X axis?

---

<div class="post-metadata">

**Author:** ![Pond](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@Pond](https://discuss.elastic.co/u/Pond)\
**Post date:** [October 2, 2020, 7:44am UTC](https://discuss.elastic.co/t/create-histogram-with-group-by-count-binned-data/250512/7 "2020-10-02T07:44:19Z")

</div>

Thanks for you help!

I tried but no result (

---

<div class="post-metadata">

**Author:** ![Pond](https://avatars.discourse-cdn.com/v4/letter/p/7ab992/32.png) [@Pond](https://discuss.elastic.co/u/Pond)\
**Post date:** [October 2, 2020, 7:45am UTC](https://discuss.elastic.co/t/create-histogram-with-group-by-count-binned-data/250512/8 "2020-10-02T07:45:15Z")

</div>

Is there any other way to create a histogram with binned data?

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [October 2, 2020, 5:57pm UTC](https://discuss.elastic.co/t/create-histogram-with-group-by-count-binned-data/250512/9 "2020-10-02T17:57:26Z")

</div>

Let's make sure that you are getting results from your query first. In your Vega visualization, can you open the browser dev tools and type `VEGA_DEBUG.vegalite_spec`? This will show the results of your Elasticsearch query as the `data`, and this lets you copy it into the [online vega editor](https://vega.github.io/editor/#/custom/vega-lite).

If you're willing to share the spec with realistic-looking data, then I can test it out and help you find the problem.

---

<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 30, 2020, 5:57pm UTC](https://discuss.elastic.co/t/create-histogram-with-group-by-count-binned-data/250512/10 "2020-10-30T17:57:38Z")

</div>

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