Skip to main content
Announcements
Qlik Introduces a New Era of Visualization! READ ALL ABOUT IT
cancel
Showing results for 
Search instead for 
Did you mean: 
oddgeir
Contributor III
Contributor III

Cumulative sum for two (or more) dimensions

In QlikSense load script, how do i do cumulative sum for several dimensions?

Given the following input data (regarding planned changes in capacity)

StaffChanges:
  Load * inline [
    Period, Department, Change, Text
    202401, Dept1, 100, Baseline
    202402, Dept2, 100, Baseline
    202401, Dept3, 100, Baseline
    202403, Dept1, 10, Staff change
    202404, Dept2, -15, Staff change
    202404, Dept3, 25, Staff change
    202407, Dept3, -10, Staff change
    202401, Dept2, 50, Staff change
  ];

How do I create a new table, showing the current staff in each department, by month?

I want to do this in load script to ensure the data is fixed. 

Labels (1)
1 Solution

Accepted Solutions
LorantPataki
Contributor III
Contributor III

Hi there,

 

try this:

StaffChanges:
  Load * inline [
    Period, Department, Change, Text
    202401, Dept1, 100, Baseline
    202402, Dept2, 100, Baseline
    202401, Dept3, 100, Baseline
    202403, Dept1, 10, Staff change
    202404, Dept2, -15, Staff change
    202404, Dept3, 25, Staff change
    202407, Dept3, -10, Staff change
    202401, Dept2, 50, Staff change
  ];
  
  
CurrentStaff:
NoConcatenate Load
Department,
    Period,
    If(Department = Previous(Department), Peek(CurrentStaff) + Change, Change) as CurrentStaff
Resident StaffChanges
Order By Department, Period;
 
Drop Table StaffChanges;

 

And the results are:

LorantPataki_0-1717752467323.png

 

My only remark is that the oldest month for each department should contain the "Baseline", which is not the case for Dept2.

View solution in original post

3 Replies
LorantPataki
Contributor III
Contributor III

Hi there,

 

try this:

StaffChanges:
  Load * inline [
    Period, Department, Change, Text
    202401, Dept1, 100, Baseline
    202402, Dept2, 100, Baseline
    202401, Dept3, 100, Baseline
    202403, Dept1, 10, Staff change
    202404, Dept2, -15, Staff change
    202404, Dept3, 25, Staff change
    202407, Dept3, -10, Staff change
    202401, Dept2, 50, Staff change
  ];
  
  
CurrentStaff:
NoConcatenate Load
Department,
    Period,
    If(Department = Previous(Department), Peek(CurrentStaff) + Change, Change) as CurrentStaff
Resident StaffChanges
Order By Department, Period;
 
Drop Table StaffChanges;

 

And the results are:

LorantPataki_0-1717752467323.png

 

My only remark is that the oldest month for each department should contain the "Baseline", which is not the case for Dept2.

oddgeir
Contributor III
Contributor III
Author

Ahhh... thank you. 

Yes this seems to be exactly what I was looking for, but just couldn't wrap my head around at the moment. 

Sorry for poor data quality in my dummy data. I was just making a few rows quckly and didn't capture everything. 

LorantPataki
Contributor III
Contributor III

Glad it worked 🙂

Cheers