Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
mskusace
Creator
Creator

Count based on another Field

I would like to count a column called "ID" based on the criteria of another column "Color". The problem is, I have another table that is associated and has the same "ID", but multiple instances because it has another field called "ROLE".

I would like to count the UNIQUE "ID"s based for "Color" =  "RED". It is actually the word "RED", so text.

The table below would yield 3 and 50%. How can I do this? I am trying to use the KPI chart.

IDColor
WA13GREEN
WB14RED
WC15ORANGE
ZS15GREEN
DB09RED

 

The other associated table

IDROLEUSER
WA13MANAGERUSER1
WA13SUPERVISORUSER2
WA13ANALYSTUSER3
WB14ANALYSTUSER3
WB14MANAGERUSER4
WC15SUPERVISORUSER5
WC15MANAGERUSER6
WC15SECRETARYUSER7
ZS15MANAGERUSER4
ZS15ANALYSTUSER8
ZS15SUPERVISORUSER2
ZS15SECRETARYUSER9
DB09SECRETARYUSER10
DB09SECRETARYUSER11
DB09ANALYSTUSER12
1 Solution

Accepted Solutions
rogerpegler
Creator II
Creator II

The count of unique ID's where Color is RED is 2, being WD14 and DB09. To get this use:

count({<Color={'RED'}>}DISTINCT ID)

The total count of all ID records where Color is RED would be 5 based on the instances of the same two IDs in the role table. To get this use:

Count({<Color={'RED'}>}ID)

I'm not sure how your desired results of 3 and 50% relate to the sample data.

In general though to avoid possible unexpected results (eg another unexpected linked table), I would use the load script to duplicate ID into another field in the Color table and count that field.

View solution in original post

20 Replies
rogerpegler
Creator II
Creator II

The count of unique ID's where Color is RED is 2, being WD14 and DB09. To get this use:

count({<Color={'RED'}>}DISTINCT ID)

The total count of all ID records where Color is RED would be 5 based on the instances of the same two IDs in the role table. To get this use:

Count({<Color={'RED'}>}ID)

I'm not sure how your desired results of 3 and 50% relate to the sample data.

In general though to avoid possible unexpected results (eg another unexpected linked table), I would use the load script to duplicate ID into another field in the Color table and count that field.

ToniKautto
Employee
Employee

Not sure what 3 and 50% represents in your data.

As best practice I would suggest that you never use key fields in aggregations. This will make it easier for you to anticipate the aggregation results. For example prefix your keyfields with % to make them easy to avoid and hide. 

%ID ID Color
WA13 WA13 GREEN
WB14 WB14 RED
WC15 WC15 ORANGE
ZS15 ZS15 GREEN
DB09 DB09 RED

 

%ID ROLE USER
WA13 MANAGER USER1
WA13 SUPERVISOR USER2
WA13 ANALYST USER3
WB14 ANALYST USER3
WB14 MANAGER USER4
WC15 SUPERVISOR USER5
WC15 MANAGER USER6
WC15 SECRETARY USER7
ZS15 MANAGER USER4
ZS15 ANALYST USER8
ZS15 SUPERVISOR USER2
ZS15 SECRETARY USER9
DB09 SECRETARY USER10
DB09 SECRETARY USER11
DB09 ANALYST USER12


count({<Color={"RED"}>} ID)  will return 2

count({<Color={"RED"}>} USER) returns 5

count(DISTINCT {<Color={"RED"}>} USER) returns 4

mskusace
Creator
Creator
Author

count({<Color={'RED'}>}DISTINCT ID) worked perfectly. 

I'm not quite understanding why to duplicate ID into another field as opposed to using that field directly. So you are saying that to remove any association with any other tables and use the duplicated field since it has no association with other tables?

mskusace
Creator
Creator
Author

So now I would like to add more complexity to it. I want to count distinct ID for all COLOR = 'RED' for all STATUS <>'FINAL'. I realized I was doing some set formulas which did not display my data on the table, but I believe it is still being used in the calculation.

abhi1693r
Contributor II
Contributor II

Hi Stephan,
I had a similar issue. So I have two columns, PART AND CUBES, with one to many relationship.(one PART can have many CUBES or a PART can have no CUBE).
Scenario: If I select 3 PARTS which have 2 CUBES, wherein 2 PARTS have 2 CUBES and 1 PART has no CUBE. I want a count of all the CUBES plus any PART which has no CUBES.

PARTCUBES
SW10011415TV
DC10031516TD
BW1002 

 

So the count should be 2+1=3.
How can I implement this?


Regards,
Abhishek

mskusace
Creator
Creator
Author

So if this was your sample data set was below, what would you expect as the count? 4?

PARTCUBES
SW10011415TV
SW10011714DC
DC10031516TD
BW1002 
abhi1693r
Contributor II
Contributor II

Yes, I would expect the count to come out as 4.

mskusace
Creator
Creator
Author

Why not just do a count of "PARTS" and do not use distinct so it counts all the duplicates? That may get you what you want. If I misinterpreted, please let me know.

Count(PARTS)

abhi1693r
Contributor II
Contributor II

 So, this is just one example scenario. I am actually supposed to count the CUBES and any PART which does not have a CUBE. Hence, 3+1.

Tried a couple of countif statements, but no luck.