Skip to main content
Announcements
NEW: Seamless Public Data Sharing with Qlik's New Anonymous Access Capability: TELL ME MORE!
cancel
Showing results for 
Search instead for 
Did you mean: 
MZambo89
Contributor III
Contributor III

sum if 2 conditions are met

Hi all.
I want to "sum" the field "value" or count the row id's (customers) who have logged in both in January and February for example 202401, 202402. If a customer has not logged in, I won't have any existing row. 

Hence, I want to sum those values when (period='202401' and value is 1 )and (period='202402' and value=1) then sum. Both statement needs to be there.
I got a field with the id (cust id), period (ex 202401, 202402 etc) and a field with value 1 only if exists.

The 1 means that the customer has visited the app during that particular month.

What would be the best way to tackle this? 


Labels (4)
1 Solution

Accepted Solutions
MZambo89
Contributor III
Contributor III
Author

I sorted it 
count({$< cust_id=
P({1<dt ={'2024-04-01'},value={1}>})
* P({1<dt ={'2024-05-01'},value={1}>})
>}distinct cust_id)
Thank you for reminding me to use indirect set analysis! 
I got a bit rusty lately as I wasn't using Qlik too often. 

View solution in original post

3 Replies
Sayed_Mannan
Creator II
Creator II

you can use set expression to tackle this, here is a sample based on above details:

// Define Variables for periods

SET vPeriod1 = '202401';
SET vPeriod2 = '202402';

// Create a flag for customers who logged in both periods

Sum({<cust_id = P({<period = {"$(vPeriod1)"}, value = {1}>} cust_id) * P({<period = {"$(vPeriod2)"}, value = {1}>} cust_id)>} value)

// P({<period = {"$(vPeriod1)"}, value = {1}>} cust_id) : this code will identify customers who logged in during first period.

//P({<period = {"$(vPeriod2)"}, value = {1}>} cust_id) : this code will identify customers who logged in during second period.

// * : this will ensure only customers who logged in both periods are considerd.

// Sum({<cust_id =......>} value): this will sums the values for the identified customers.

 

Hope this helps/

 

MZambo89
Contributor III
Contributor III
Author

Hi Sayed. Thank you for explaining. I see the rationale, however, when I apply it will sum them up instead of counting them only 1x. 
I basically want to count the distinct id's or sum up the value 1 (each way will give the same result).

If I use this method: =Count(distinct{< period={'202401','202402'} >}single_id)
then it gives me the unique users whohave been either in one or the other but not in both... (the majority will be the same but not all of them). 

MZambo89
Contributor III
Contributor III
Author

I sorted it 
count({$< cust_id=
P({1<dt ={'2024-04-01'},value={1}>})
* P({1<dt ={'2024-05-01'},value={1}>})
>}distinct cust_id)
Thank you for reminding me to use indirect set analysis! 
I got a bit rusty lately as I wasn't using Qlik too often.