# Timelion - Find unique count on multiple conditions

**URL:** <https://discuss.elastic.co/t/timelion-find-unique-count-on-multiple-conditions/195423>\
**Category:** Kibana\
**Tags:** timelion\
**Created:** [August 16, 2019, 4:38am UTC](https://discuss.elastic.co/t/timelion-find-unique-count-on-multiple-conditions/195423 "2019-08-16T04:38:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thomasdang](https://avatars.discourse-cdn.com/v4/letter/t/dec6dc/32.png) [@thomasdang](https://discuss.elastic.co/u/thomasdang)\
**Post date:** [August 16, 2019, 4:38am UTC](https://discuss.elastic.co/t/timelion-find-unique-count-on-multiple-conditions/195423/1 "2019-08-16T04:38:10Z")

</div>

Hi Experts,

I need to calculate the unique number of string values in a field with multiple query conditions. Here's an example:

There are 2 fields:  
CUSTOMER\_NAME (with values like "John", "Jack", "Marie", etc.)  
BOUGHT\_ITEM (with values like 1, 2, 3, 4, etc. - these are string values, not numbers)

Example table:  
CUSTOMER\_NAME, BOUGHT\_ITEM  
John, 1  
Jack, 2  
Marie, 1  
John, 2  
Jack, 3  
Marie, 2  
Bob, 2  
Jack, 4

I need to run a Timelion command to select unique count of CUSTOMER\_NAME who bought (at least) both items 1 and 2 (at different times). Therefore, the result would be 2 (customers) because John and Marie bought items 1 and 2.

The following Timelion query ONLY counts unique customer who bought item 1. How do I make it so to get unique customer count who bought both items 1 AND 2?

.es(index=customer, q='BOUGHT\_ITEM:1', metric=cardinality:CUSTOMER\_NAME.keyword)

Please help! Thanks so much!

---

<div class="post-metadata">

**Author:** ![alisongoryachev](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alisongoryachev/32/111352_2.png) [@alisongoryachev](https://discuss.elastic.co/u/alisongoryachev)\
**Post date:** [August 19, 2019, 3:19pm UTC](https://discuss.elastic.co/t/timelion-find-unique-count-on-multiple-conditions/195423/2 "2019-08-19T15:19:06Z")

</div>

Hi @thomasdang!

Timelion uses [query-string syntax](https://www.elastic.co/guide/en/elasticsearch/reference/master/query-dsl-query-string-query.html#query-string-syntax), so I believe you should be able to use `AND` in the query. Something like: .`es(index=customer, q='BOUGHT_ITEM:(1 AND 2)', metric=cardinality:CUSTOMER_NAME.keyword)`

---

<div class="post-metadata">

**Author:** ![thomasdang](https://avatars.discourse-cdn.com/v4/letter/t/dec6dc/32.png) [@thomasdang](https://discuss.elastic.co/u/thomasdang)\
**Post date:** [August 19, 2019, 8:49pm UTC](https://discuss.elastic.co/t/timelion-find-unique-count-on-multiple-conditions/195423/3 "2019-08-19T20:49:30Z")

</div>

Thanks @alisongoryachev. When I tried that, it gave me no result! But when I tried ... (1 OR 2) then it works, but of course that's not what I need. I think the "AND" doesn't work because it thinks the field (BOUGHT\_ITEM) cannot be both 1 and 2 at the same time! But what we need here is to find the same CUSTOMER\_NAME that bought items 1 and 2 perhaps at different time. I guess what we're looking for is the "union" of items 1 and 2. So what I had to do manually is to query customers who bought item 1, and do another query for customers who bought item 2, then union those 2 sets and find the common CUSTOMER\_NAME who bought both items 1 & 2. But that's a long way, I need to do all that in the same query. Thanks!

---

<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:** [September 16, 2019, 8:49pm UTC](https://discuss.elastic.co/t/timelion-find-unique-count-on-multiple-conditions/195423/4 "2019-09-16T20:49:31Z")

</div>

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