Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
jessica_webb
Creator III
Creator III

Count over several rows

Hi,

I have a rather large table with the following dimensions:

IDGroupYearLocLocCountStatus
123R2012ABC3A
123R2013ABC5A
123R2014ABC2A
123R2015ABC3A
45612012ABC1C
45612013ABC-C
45612014ABC3C
45612015ABC2C
789R2012DEF-D
789R2013DEF6D
789R2014DEF1D
789R2015DEF2D
789R2012GHI1E
789R2013GHI1E
789R2014GHI-E
789R2015GHI-E

From this, I want an expression for the 'Status' column using the following criteria (in the hierarchical order shown below):

A - If the LocCount for the ID, Group and Loc is at least 2 in every year

B - If the LocCount for the ID, Group and Loc is at least 1 in every year

C - If the LocCount for the ID, Group and Loc is at least 2 in 2015 and 2014

D - If the LocCount for the ID, Group and Loc is at least 1 in 2015 and 2014

E - If above criteria not met for any ID, Group and Loc

I've completed the status column in the example above with what I would expect to see.

What's the best way to achieve this? In the script or by expression only?

Help would be really appreciated on this as I'm on a very tight schedule and not sure where to start!!

24 Replies
YoussefBelloum
Champion
Champion

Sun is shining

thank you Sunny !

jessica_webb
Creator III
Creator III
Author

Hi Sunny,

Thank you as always!!!

It seems to work perfectly, but I don't really understand why! Are you able to explain a bit?


If(Min(TOTAL <ID, Group, Loc> Alt(LocCount, 0)) = 2, 'A',


This seems to mean that it should return 'A' if the minimum value for that ID, Group, Loc combo is 2. Which obviously isn't right. So where in the code is it specifying that this should be for every year?

sunny_talwar

You have two main conditions

1) Min(TOTAL <ID, Group, Loc> Alt(LocCount, 0))

2) Min(TOTAL <ID, Group, Loc> {<Year = {2015, 2014}>} Alt(LocCount, 0))

Create them as expression in your chart and see what happens here

Capture.PNG

Shows the minimum value based on ID, Group, Loc for all the rows grouped by ID, Group, Loc

jessica_webb
Creator III
Creator III
Author

Hi Sunny,

I've hit upon an issue I hadn't thought of that gives me incorrect results:

 

IDGroupYearLocLocCountStatus
64571102014AB13
64571102014BC13
64571102014CD13
64571102014-5-
64571102015DE13
64571102015EF13
64571102015FG13
64571102015GH13
64571102015HI13
64571102015IJ13
64571102015JK13
64571102015-1-
64571102016KL13
64571102016LM13
64571102016FG13
64571102016MN13
64571102016NO13
64571102016OP13
64571102016PQ13
64571102016QR13
64571102016RS13
64571102016ST13
64571102016-1-

If a 'Loc' doesn't have a 'LocCount' in previous years, it doesn't show in the data. Which means each value appears to have appeared once in every year. But every year should in fact be checkign that it appears in 2013, 2014, 2015, 2016… Any ideas?

sunny_talwar

Are you talking about the places where you have Null or '-' Loc? Is that where the problem is?

jessica_webb
Creator III
Creator III
Author

The table I've just posted should have 'E' as the status for all, except 'FG' as they don't have a LocCount for every year - every year being 2014, 2015 and 2016 . However, they are coming up as status 'B' (sorry for confusion in table, I swapped the statuses from letters to numbers in my code). - because the code is looking for LocCount in every year that they are listed in.

Does that make sense??

sunny_talwar

Let's take a step back and put down for me the conditions in words again....

sunny_talwar

Why is "FG" B.... FG is not available for all the years also? FG is only available in 2015 and 2016? Should it not be E also in that case?

jessica_webb
Creator III
Creator III
Author

A - If the LocCount for the ID, Group and Loc is at least 2 in every year

B - If the LocCount for the ID, Group and Loc is at least 1 in every year

C - If the LocCount for the ID, Group and Loc is at least 2 in 2016 and 2015

D - If the LocCount for the ID, Group and Loc is at least 1 in 2016 and 2015

E - If above criteria not met for any ID, Group and Loc

I've had to change the years - so apologies for causing a bit of confusion there.

So with the years changed in the criteria above, FG should be showing as 'B', but the rest should all be 'E'.

sunny_talwar

For FG to be 'B', it needs to have at least 1 in every year? Meaning all the three years (2014, 2015, 2016), right? By FG is only available in 2015 and 2016

Capture.PNG