Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Conditional Count

Hi all

I have a list of Patrons "Patron_ID" (numeric) and a list of products "PRODUCT_ID" (numeric)

2.JPG

At the front end, I would like to have an expression that calculates the number of returning customers, ie customers who purchased more than one product. However, I am struggling with the expression script at the moment. As you can see from the above image, the "Returning Customers" should indicate as "0" as the patron has only bought one show ticket

I've tried the following to no avail:

if(PRODUCT_ID >=2, count(DISTINCT(PATRON_ID)))

//i clicked on a patron who purchased only 1 product, but it still shows the returning customers as "1".

count((if(count (PRODUCT_ID>1),PATRON_ID)))

//error in expression

Any guidance will be much appreciated!

1 Solution

Accepted Solutions
gandalfgray
Specialist II
Specialist II

Using my original stmt will give one too much in some situations because it also counts products not connected (bought) by a patron.

This change will fix that:

-sum( aggr(count(DISTINCT PRODUCT_ID), PATRON_ID)>1 and PATRON_ID)

Ashoks suggestion also works if you add DISTINCT.

Count(if(Aggr(Count(DISTINCT PRODUCT_ID),PATRON_ID)>=2,PATRON_ID))



It is a good practice to try to avoid "if"  in chart expressions, but in a case like this it may be easier to understand.


anyway both statements above give the same answers


/gg

View solution in original post

14 Replies
amit_saini
Master III
Master III

Try,

Similar below:

=Count(if (match([ECO state],'Define Components','Design Work' ),[ECO state]))

Thanks,
AS

amit_saini
Master III
Master III

Also:

=Count(if(PRODUCT_ID >=2, PATRON_ID)

Thanks,
AS

amit_saini
Master III
Master III

Also:

Count({$<PRODUCT_ID={">=2"}>}Dimesion)

Thanks,
AS

Not applicable
Author

Hi Amit

Tried your suggestions but I am still unable to achieve the correct count (still showing as "1" when correct count should be 0)

Attached QVW for your consideration

stabben23
Partner - Master
Partner - Master

Hi,

is this what you are looking for?

sum(aggr(sum(TICKETS_SOLD)>1,PRODUCT_NAME))

Not applicable
Author

Hi

Unfortunately not, as a single patron can purchase multiple tickets for a single product.

I am looking to do a count of the number of patrons who purchase different products, regardless of the quantity of tickets purchased

gandalfgray
Specialist II
Specialist II

Hi Chay

Try this:

-sum( aggr(count(DISTINCT PRODUCT_ID), PATRON_ID)>1 )

hth/gg

gandalfgray
Specialist II
Specialist II

The

aggr(count(DISTINCT PRODUCT_ID), PATRON_ID)

part will return the number of distinct products bought by a patron.

The comparison with 1 will return a boolean TRUE, which is the numeric -1, if there is more than 1 distinct product bought, and FALSE 0 otherwise.

So for each patron (in your current selection) this will return -1 if he/she bought more than one unique product (in your current selection), and 0 if not.

summing that will give you the number of patrons which bought more than one unique product, but as a negative number.

So the preceding minus is to get the correct sign.

Not applicable
Author

Hi Chay,

try this it will work

Expression:

Count(if(Aggr(Count(PRODUCT_ID),PATRON_ID)>=2,PATRON_ID))

Thanks,
Ashok.