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

Announcements
Join us to spark ideas for how to put the latest capabilities into action. Register here!
cancel
Showing results for 
Search instead for 
Did you mean: 
PragyanSubedi
Contributor
Contributor

How To Find Set Difference Between Distinct Set Values?

Hello,

I have two dimensions: 'Month' and 'Name'.

I want to find the count of unique elements that exist in 1/1/2018 and not in 1/2/2018 as shown in the photo:

 

set difference.PNG

I'm trying to do something like this:

Count(({<Month={'1/1/2018'}>} Distinct [Name]) - ({<Month={'1/2/2018'}>} Distinct [Name]))

But the above syntax sadly doesn't work.

To explain my intention, I am trying to:

- First, find the distinct elements in each date to create set A and set B.

A = {'Apple', 'Orange}, B = {'Apple', 'Banana'}

- Then, find the difference between sets A and B (elements that lie in set A and not in set B).

A - B = {'Orange'}

- Finally, find the count of the difference between sets A and B (count of elements that lie in set A and not in set B).

Count(A - B) = 1

How can I do this?

Labels (2)
1 Solution

Accepted Solutions
JordyWegman
Partner - Master
Partner - Master

Hi Pragyan,

You're looking for this calculation:

Count(Distinct {$< Month = {'1/1/2018'}, Name = e( {< Month = {'1/2/2018'} >} ) >} Name)

It's a nice way of using E().

JordyWegman_0-1661179190476.png

 

Example data that I've used:

Load * Inline [
Month, Name
1/1/2018, Apple
1/1/2018, Apple
1/1/2018, Orange
1/1/2018, Strawberry
1/2/2018, Apple
1/2/2018, Banana
];

Jordy
Climber
 

Work smarter, not harder

View solution in original post

6 Replies
BrunPierre
Partner - Master II
Partner - Master II

Please correct me if I'm wrong, but shouldn't "Banana" be the only unique fruit between the two dates?

 

PragyanSubedi
Contributor
Contributor
Author

No. @BrunPierre 

To clarify, if you consider the distinct values for date '1/1/2018' as set and distinct values for date '1/2/2018' as set B, then A - B = {'Orange'} (their set difference).

So, the count of A - B would be 1.

BrunPierre
Partner - Master II
Partner - Master II

In that case, off the top of my head, I'd do something like this

Set A : Count(DISTINCT TOTAL IF(Month='1/1/2018',Name))

Set B: Count(DISTINCT TOTAL IF(Month='1/1/2018',Name))

Expected output: 

Count(DISTINCT TOTAL IF(Month='1/1/2018',Name)) - Count(DISTINCT TOTAL IF(Month='121/2018',Name))

JordyWegman
Partner - Master
Partner - Master

Hi Pragyan,

You're looking for this calculation:

Count(Distinct {$< Month = {'1/1/2018'}, Name = e( {< Month = {'1/2/2018'} >} ) >} Name)

It's a nice way of using E().

JordyWegman_0-1661179190476.png

 

Example data that I've used:

Load * Inline [
Month, Name
1/1/2018, Apple
1/1/2018, Apple
1/1/2018, Orange
1/1/2018, Strawberry
1/2/2018, Apple
1/2/2018, Banana
];

Jordy
Climber
 

Work smarter, not harder
PragyanSubedi
Contributor
Contributor
Author

@BrunPierre  The issue with the recommended solution would be that you are first calculating the count of distinct values for each date and then subtracting the counts which results in 0 since both dates have an equal number of distinct elements.

I'm trying to do something like this:

Count(({<Month={'1/1/2018'}>} Distinct [Name]) - ({<Month={'1/2/2018'}>} Distinct [Name]))

But the above syntax sadly doesn't work.

To explain my intention, I am trying to:
- First, find the distinct elements in each date to create set A and set B.

A = {'Apple', 'Orange}, B = {'Apple', 'Banana'}

- Then, find the difference between sets A and B (elements that lie in set A and not in set B).

A - B = {'Orange'}

- Finally, find the count of the difference between sets A and B (count of elements that lie in set A and not in set B).

Count(A - B) = 1

PragyanSubedi
Contributor
Contributor
Author

Thank you! This works @JordyWegman