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: 
DanielleW1
Contributor III
Contributor III

Using count distinct with set analysis

I have a query around using set analysis with count distinct and have read that this can sometimes not be fully trusted. This happened in my app where the previous year count matched the current year which isn't correct.

What I am hoping to do is calculate the number of distinct transactions but also look at the previous year. This can have outside factors with filtering which I would like to be applicable to the previous year calculation also.

Our transactional data contains multiple lines per transaction to show the line level detail of products, and these also have departments within them that need to apply to set analysis - so I was unable to use a dummy field of '1' for example.

Any help on this would be greatly appreciated, thanks in advance.

 

Labels (1)
1 Solution

Accepted Solutions
Or
MVP
MVP

Looks to be running fine with some sample data I whipped up:

Load * INLINE [
Year, Type, TransactionID
2024, A, 1
2024, A, 2
2024, B, 1
2024, B, 3
2024, C, 3
2025, A, 3
2025, B, 3
2025, B, 4
2025, C, 5
];

Or_0-1752683251640.png

View solution in original post

4 Replies
Or
MVP
MVP

You will have to be more specific (ideally with sample data and an expected result), I think. This simply isn't enough concrete information to try and help. 

DanielleW1
Contributor III
Contributor III
Author

I currently use the following expressions:

COUNT(DISTINCT(({$<Year={'$(vSelectedYear)'},[Transaction Type]={'RE','SA'}>}[Transaction ID])))

COUNT(DISTINCT(({$<Year={'$(vPrevYear)'},[Transaction Type]={'RE','SA'}>}[Transaction ID])))

 

Where vSelectedYear = 2025 and vPrevYear = 2024.

This works as expected with the year changes when using a SUM of Sales for example:

SUM(({$<Year={'$(vPrevYear)'},[Transaction Type]={'RE','SA'}>}[Sales Value]))

However both of the COUNT DISTINCT expressions give me the same result.

Or
MVP
MVP

Looks to be running fine with some sample data I whipped up:

Load * INLINE [
Year, Type, TransactionID
2024, A, 1
2024, A, 2
2024, B, 1
2024, B, 3
2024, C, 3
2025, A, 3
2025, B, 3
2025, B, 4
2025, C, 5
];

Or_0-1752683251640.png

rwunderlich
Partner Ambassador/MVP
Partner Ambassador/MVP

If [Transaction ID] is a key field, yes it's possible to get some unexpected results depending on how your data model is structured. Recommended to count a non-key field instead.

-Rob