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

Announcements
Discover how organizations are unlocking new revenue streams: Watch here
cancel
Showing results for 
Search instead for 
Did you mean: 
NattiNatey
Contributor II
Contributor II

Getting DISTINCT COUNT of Dimension values based on condition with aggregation in Qlik expression for KPI

Suppose I have a table called DataTable with column [Dim1] and Measure [Num1]. I want to get all distinct values in [Dim1] based on the condition that [Num1] aggregated by [Dim1] > 500.

This can be done with SQL like this:

SELECT COUNT (DISTINCT [Dim1]) FROM
    (SELECT [Dim1], SUM([Num1])
    FROM DataTable GROUP BY [Dim1]
    HAVING SUM([Num1]) >= 500)

Ideally I would like to do this in an expression for a new measure and display the count in a KPI, but I have not been able to get it working with aggregation. For example I tried the following, but it seems to filter the rows BEFORE aggregation, so the result is not correct.

Count(
  Distinct
  If(
    Aggr(Sum([Num1]), [Dim1]) >= 500,
    [Dim1]
  )
)

I am new to Qlik and any suggestions are greatly appreciated!

 

Labels (6)
1 Solution

Accepted Solutions
marcus_sommer

You may try:

If(sum(Aggr(Sum([Num1]), [Dim1])) >= 500, Count(Distinct [Dim1]))

View solution in original post

2 Replies
marcus_sommer

You may try:

If(sum(Aggr(Sum([Num1]), [Dim1])) >= 500, Count(Distinct [Dim1]))

NattiNatey
Contributor II
Contributor II
Author

Thanks @marcus_sommer this solution worked! I realized I am having an additional issue with adding an alternate state filter to the query, but this is unrelated to the question asked.