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

Sum with condition for less than specific dimension value

Hi experts,

I believe this should be easy, but I cannot get it correct

My data like below:

ScoreNumOfStudents
2608
2625
2671
2734
2778
2782

I would like to sum the NumOfStudents for the score less than the specific score. ex. for score 273, it should sum up NumOfStudents for score 267, 262, 260 which is 8+ 5 +1 = 14. Score is the dimension.

I have tried:

sum([$<Score= {">=$(=Min(Score)) < $(=(Score))"}>} NumOfStudents)  and

sum({$<Score= {">=$(=Min(Score))"}>} NumOfStudents)

but none of them are working.

How should I achieve this? The solution either in expression or data load script will be fine with me .

Thanks,

Jing

15 Replies
Gysbert_Wassenaar

Try this:

Temp:

LOAD

     ID,

     Score,

     NumOfStudents

FROM

     ...source...

     ;

Result

LOAD

     ID,

     Score,

     NumOfStudents,

     If(ID<>Previous(ID), NumOfStudents, rangesum(NumOfStudents,peek('Cumulative'))) as Cumulative

RESIDENT

     Temp

ORDER BY ID, Score   

     ;

DROP TABLE Temp;


talk is cheap, supply exceeds demand
Not applicable
Author

Gysbert, it is not working properly...

Please see my attachment.

brunobertels
Master
Master

Hi

Don't know how to handle your requierment in the script but in a table try this expression

max(aggr(rangesum(above(sum({<Score>}NumOfStudents),0,Rowno())),ID,Score)) - NumOfStudents

Not applicable
Author

Thanks Bruno. It works in the table and I learned more. However I do need this cumulative data stored in data model for further computing purpose.

Gysbert_Wassenaar

Change ID=Previous(ID) to ID<>Previous(ID)


talk is cheap, supply exceeds demand
Not applicable
Author

Thank you very much Gysber! It works perfectly now. I just wondering why we have to use <> instead of = which makes such a big difference.

I have attached the final qvf for others reference.

And here is the script in case people don't use Sense:

Temp:

LOAD

     ID,

     Score,

     NumOfStudents

FROM

     source;

Result:

LOAD

     ID,

     Score,

     NumOfStudents,

     If(ID<>Previous(ID), NumOfStudents, rangesum(NumOfStudents,peek('Cumulative'))) as Cumulative

RESIDENT

     Temp

ORDER BY ID, Score   

     ;

DROP TABLE Temp;