# Kibana Visuals - count number of customers with more than X buys

**URL:** <https://discuss.elastic.co/t/kibana-visuals-count-number-of-customers-with-more-than-x-buys/271107>\
**Category:** Kibana\
**Created:** [April 23, 2021, 3:20pm UTC](https://discuss.elastic.co/t/kibana-visuals-count-number-of-customers-with-more-than-x-buys/271107 "2021-04-23T15:20:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cjabrantes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjabrantes/32/78091_2.png) [@cjabrantes](https://discuss.elastic.co/u/cjabrantes)\
**Post date:** [April 23, 2021, 3:20pm UTC](https://discuss.elastic.co/t/kibana-visuals-count-number-of-customers-with-more-than-x-buys/271107/1 "2021-04-23T15:20:46Z")

</div>

Hi all,

Image that i have in elastic something like:  
Customer id  
Product

I need to create a metric that tells me the number of customers that bought the product Y more than X time.

Example number of customer that bought a t-shirt more than 5 times.

Basically i guess we need to filter product = t-shirt and aggregate by customer id with min\_doc\_count 5.

But i dont see how this can be done in kibana visuals i had read that TSVB dont support min\_doc\_count, is this possible to archive in any other visual?

i'm running 7.5.

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:** [April 23, 2021, 7:55pm UTC](https://discuss.elastic.co/t/kibana-visuals-count-number-of-customers-with-more-than-x-buys/271107/2 "2021-04-23T19:55:25Z")

</div>

You can add `{ "min_doc_count": 5 }` as an advanced JSON input on the Terms aggregation in your Visualize table/bar/line etc. However, I'm not sure that this is what you want, because you said that you want basically the following metrics:

Buckets: `Terms of product`, sorted by the metric  
Metric: `Cardinality of customer_id`

So if the only one customer buys a product 5 times, then your `min_doc_count` would be satisfied, but your unique count is only 1.

This leaves you with two main options:

1. Build a visualization that shows the top products sorted by unique count, but without hiding the bottom products

2. Use [Elasticsearch transforms](https://www.elastic.co/guide/en/elasticsearch/reference/current/transforms.html) to pivot your data and then query the pivoted index.

---

<div class="post-metadata">

**Author:** ![cjabrantes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjabrantes/32/78091_2.png) [@cjabrantes](https://discuss.elastic.co/u/cjabrantes)\
**Post date:** [April 26, 2021, 4:27pm UTC](https://discuss.elastic.co/t/kibana-visuals-count-number-of-customers-with-more-than-x-buys/271107/3 "2021-04-26T16:27:21Z")

</div>

Hi Wylie,

Sorry the late response i was reading a bit about what you sent - elastic transforms (seems a bit complex, not sure if i explained correctly my use case, because i think option 1 does not give what i need).

Image i have this data is last 5m and i want the count of customer with more than 5 buys:  
customerid product  
1 t-shirt  
1 t-shirt  
1 t-shirt  
1 t-shirt  
1 t-shirt  
1 t-shirt  
2 t-shirt  
2 t-shirt  
3 t-shirt

i want the count of customer with more than 5 buys (i can use filters to filter products if needed).  
So in this case the values i m looking for is 1, because only 1 customer had more than 5 buys.

The ideal i have a chart with this value across the time, line, TSVB(time series), bar...

Is there any visual that can give me this? Do we need to use Elastic transforms for this?

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:** [April 26, 2021, 7:12pm UTC](https://discuss.elastic.co/t/kibana-visuals-count-number-of-customers-with-more-than-x-buys/271107/4 "2021-04-26T19:12:13Z")

</div>

Yes, we are talking about exactly the same problem. Your data should be pivoted so that it contains `{ product: 't-shirt', unique: 3 }` given your example above. This is something transforms can do for you, or you could do it using some other kind of script.

The main reason is what I already explained above: you can't do this using the terms aggregation. I would welcome you to read through the docs on [Elasticsearch bucket aggregations](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html) if you'd like to try out a few queries yourself.

Edit: If your data is very small, like under 10000 products, you may be interested in using [Vega](https://www.elastic.co/guide/en/kibana/current/vega.html) visualizations, which are the most powerful tool that we offer. If you are able to construct a query you like you can visualize it in Vega.

---

<div class="post-metadata">

**Author:** ![cjabrantes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjabrantes/32/78091_2.png) [@cjabrantes](https://discuss.elastic.co/u/cjabrantes)\
**Post date:** [April 27, 2021, 12:19am UTC](https://discuss.elastic.co/t/kibana-visuals-count-number-of-customers-with-more-than-x-buys/271107/5 "2021-04-27T00:19:41Z")

</div>

Hi,

I m reading it, but do you think it should be possible with bucket aggregation? from my reading seems that the sub aggregation runs only in each bucket, can you confirm? Or is the pipeline aggregation more suitable for this task?

Trying this pipeline aggregation I m having the error " [cardinality] unknown field [buckets\_path], parser not found", i will give it a few more tries.

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:** [April 27, 2021, 3:12pm UTC](https://discuss.elastic.co/t/kibana-visuals-count-number-of-customers-with-more-than-x-buys/271107/6 "2021-04-27T15:12:06Z")

</div>

You can do something kind of similar using bucket aggregations, but it's not going to be precise:

a. Kibana visualizations as a whole let you build a Terms/Top Values agg with a Cardinality/Unique Count metric, and then you can see the "number of customers per product". But you can't filter this number because it's aggregated in Kibana.

b. You can build the same query above in Vega, but you can use a bucket selector pipeline agg to limit it to "\> 5" only. This requires that your total number of buckets is under 10k or 65k, depending on your ES version

---

<div class="post-metadata">

**Author:** ![cjabrantes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cjabrantes/32/78091_2.png) [@cjabrantes](https://discuss.elastic.co/u/cjabrantes)\
**Post date:** [April 29, 2021, 9:41am UTC](https://discuss.elastic.co/t/kibana-visuals-count-number-of-customers-with-more-than-x-buys/271107/7 "2021-04-29T09:41:08Z")

</div>

Hi,

Still no luck, can you confirm if cardinality is supported in pipeline aggregations?

I have this error:  
"reason": "[29:16] [cardinality] unknown field [buckets\_path], parser not found"

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:** [April 29, 2021, 2:38pm UTC](https://discuss.elastic.co/t/kibana-visuals-count-number-of-customers-with-more-than-x-buys/271107/8 "2021-04-29T14:38:26Z")

</div>

Cardinality is a metric aggregation, not a pipeline aggregation.

---

<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 27, 2021, 2:38pm UTC](https://discuss.elastic.co/t/kibana-visuals-count-number-of-customers-with-more-than-x-buys/271107/9 "2021-05-27T14:38:32Z")

</div>

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