Skip to main content
Woohoo! Qlik Community has won “Best in Class Community” in the 2024 Khoros Kudos awards!
Announcements
Nov. 20th, Qlik Insider - Lakehouses: Driving the Future of Data & AI - PICK A SESSION
cancel
Showing results for 
Search instead for 
Did you mean: 
scheibercamo
Contributor III
Contributor III

How do I write a nested if then statement for count?

Currently I have a rather manual process to ultimately determine the count of a KPI under specific conditions, which works for only two fields (in this case, Master Retarget and Master Age Band):

count(distinct(if([Master Retarget]='Yes' AND [Master Age Band]='Due > 90 Days',[Master ID])))

+

count(distinct(if([Master Retarget]='Yes' AND [Master Age Band]='Due 31 - 60 Days',[Master ID])))

+

count(distinct(if([Master Retarget]='Yes' AND [Master Age Band]='Due 0 - 30 Days',[Master ID])))

+

count(distinct(if([Master Retarget]='Yes' AND [Master Age Band]='Due 61 - 90 Days',[Master ID])))

I want to add a third field [Issue Source] under which I only want the count for 'Regulation' and 'Testing' named items to appear in the final Count.

Ultimately what I want to state is "X number of 'Regulation' and 'Testing' Master IDs were 'Retargeted' and 'Due' at this point in time. "

1 Solution

Accepted Solutions
swuehl
MVP
MVP

Why do you need to use several aggregations? Could it happen that a Master ID appears in more than one Master Age Band and you need to aggregate the distinct count of all the bands?

If not, I would assume that something like

=count(distinct

    if([Master Retarget]='Yes' AND

          Match([Master Age Band],'Due > 90 Days','Due 31 - 60 Days','Due 0 - 30 Days','Due 61 - 90 Days')

,[Master ID]))


could do the same?


And how does your data model look like?


You could consider using set analysis to filter your records set:

=Count(

{<[Master Retarget] = {'Yes'},

[Master Age Band] = {'Due > 90 Days','Due 31 - 60 Days','Due 0 - 30 Days','Due 61 - 90 Days'} >}

DISTINCT [Master ID])


If you need to add another condition, just add it like

=Count(

{<[Master Retarget] = {'Yes'},

[Master Age Band] = {'Due > 90 Days','Due 31 - 60 Days','Due 0 - 30 Days','Due 61 - 90 Days'},

[Issue Source] = {'Regulation','Testing'} >}

DISTINCT [Master ID])

View solution in original post

3 Replies
swuehl
MVP
MVP

Why do you need to use several aggregations? Could it happen that a Master ID appears in more than one Master Age Band and you need to aggregate the distinct count of all the bands?

If not, I would assume that something like

=count(distinct

    if([Master Retarget]='Yes' AND

          Match([Master Age Band],'Due > 90 Days','Due 31 - 60 Days','Due 0 - 30 Days','Due 61 - 90 Days')

,[Master ID]))


could do the same?


And how does your data model look like?


You could consider using set analysis to filter your records set:

=Count(

{<[Master Retarget] = {'Yes'},

[Master Age Band] = {'Due > 90 Days','Due 31 - 60 Days','Due 0 - 30 Days','Due 61 - 90 Days'} >}

DISTINCT [Master ID])


If you need to add another condition, just add it like

=Count(

{<[Master Retarget] = {'Yes'},

[Master Age Band] = {'Due > 90 Days','Due 31 - 60 Days','Due 0 - 30 Days','Due 61 - 90 Days'},

[Issue Source] = {'Regulation','Testing'} >}

DISTINCT [Master ID])

scheibercamo
Contributor III
Contributor III
Author

This does almost exactly what I needed it to do, thank you so much!

However, it doesn't show "subtotals" of a row-by-row breakdown when I add it as a measurement in a straight table:

pic for qlik.JPG

The column to the left is how I want the total of 39 broken across each "group"....

This is what the formula in the column next to it looks like:

count(distinct{<[Issue Source]={'Regulation','Testing'}>}[Master ID])

Do you know why I can't get a row-by-row breakdown?

swuehl
MVP
MVP

Well, that's hard to tell without knowing your data, model and context (dimensions etc).