Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
nezuko_kamado
Creator
Creator

(Qliksense script) how to sum units of current value and next row value? (Opposite of Previous())

I need to calculate the sum unit of adjacent dates, such as previous date unit + current unit,  and current unit + next date unit  by ID and Date.

First, I re-arrange original data by ID and Date

test:
LOAD ID,
           Date,
           sum(Unit) as sumUnit
Resident original
Group by ID, Date;

Then,  add the sumUnit of previous Date and current Date.

finalTable:
load *,
if(Previous(ID)=ID and date(Date, 'MM/DD/YYYY')- previous(date(Date, 'MM/DD/YYYY'))=1, RangeSum(previous(sumUnit),sumUnit), 0) as lastcurrent
Resident test
ORDER BY ID, Date;

But fail to calculate the summation of current and next value by using desc 

left join(finalTable)
load*,
if(Previous(ID)=ID and date(Date, 'MM/DD/YYYY')- previous(date(Date, 'MM/DD/YYYY'))=1, RangeSum(previous(sumUnit),sumUnit), 0) as currentnext
Resident finalTable
ORDER BY ID, Date desc;

Is there any function like opposite of previous()? or how can I grab the next row value?

Capture.PNG

original:
load * inline [ Date, Week, ID, Unit
9/1/2021, 1, A, 1
9/1/2021, 1, B, 2
9/7/2021, 2, A, 3
9/7/2021, 2, C, 3
9/14/2021, 3, A, 2
9/15/2021, 3, B, 2
9/16/2021, 3, D, 2
9/20/2021, 4, A, 2
9/20/2021, 4, D, 5
9/21/2021, 4, B, 1
9/27/2021, 5, A, 2
9/27/2021, 5, F, 5
9/28/2021, 5, D, 2
10/4/2021, 6, A, 2
10/5/2021, 6, B, 1
10/6/2021, 6, C, 1
10/11/2021, 7, A, 1
10/11/2021, 7, C, 5
10/9/2021, 7, A, 1
10/10/2021, 8, B, 3
10/11/2021, 8, C, 2
10/11/2021, 7, F, 2
10/12/2021, 8, F, 1
10/13/2021, 8, A, 3
10/14/2021, 8, A, 4
10/15/2021, 8, B, 5
10/16/2021, 8, A, 5
10/17/2021, 9, A, 1
10/17/2021, 9, A, 5
10/17/2021, 9, A
10/18/2021, 9, C, 2
10/19/2021, 9, D, 3
10/16/2021, 8, F, 3
10/17/2021, 9, F, 1
10/20/2021, 9, F, 1];

Labels (3)
1 Solution

Accepted Solutions
vinieme12
Champion III
Champion III

original:
load ID,Date,sum(Unit) as sumUnit inline [ Date, Week, ID, Unit
9/1/2021, 1, A, 1
9/1/2021, 1, B, 2
9/7/2021, 2, A, 3
9/7/2021, 2, C, 3
9/14/2021, 3, A, 2
9/15/2021, 3, B, 2
9/16/2021, 3, D, 2
9/20/2021, 4, A, 2
9/20/2021, 4, D, 5
9/21/2021, 4, B, 1
9/27/2021, 5, A, 2
9/27/2021, 5, F, 5
9/28/2021, 5, D, 2
10/4/2021, 6, A, 2
10/5/2021, 6, B, 1
10/6/2021, 6, C, 1
10/11/2021, 7, A, 1
10/11/2021, 7, C, 5
10/9/2021, 7, A, 1
10/10/2021, 8, B, 3
10/11/2021, 8, C, 2
10/11/2021, 7, F, 2
10/12/2021, 8, F, 1
10/13/2021, 8, A, 3
10/14/2021, 8, A, 4
10/15/2021, 8, B, 5
10/16/2021, 8, A, 5
10/17/2021, 9, A, 1
10/17/2021, 9, A, 5
10/18/2021, 9, A, 2
10/18/2021, 9, C, 2
10/19/2021, 9, D, 3
10/16/2021, 8, F, 3
10/17/2021, 9, F, 1
10/20/2021, 9, F, 1]
Group by ID,Date;

NoConcatenate

t1:
load *
,if(ID=Peek(ID) and (Date#(Date,'MM/DD/YYYY')-Date#(peek(Date),'MM/DD/YYYY'))=1 ,Peek(sumUnit)+sumUnit,0) as lastCurrent
Resident original order by ID,Date Asc;
Drop table original;


t2:
load *
,peek(lastCurrent) as currentnext
Resident t1 order by ID,Date Desc;
Drop table t1;

Vineeth Pujari
If a post helps to resolve your issue, please accept it as a Solution.

View solution in original post

2 Replies
vinieme12
Champion III
Champion III

you need to use the Peek() for this

Vineeth Pujari
If a post helps to resolve your issue, please accept it as a Solution.
vinieme12
Champion III
Champion III

original:
load ID,Date,sum(Unit) as sumUnit inline [ Date, Week, ID, Unit
9/1/2021, 1, A, 1
9/1/2021, 1, B, 2
9/7/2021, 2, A, 3
9/7/2021, 2, C, 3
9/14/2021, 3, A, 2
9/15/2021, 3, B, 2
9/16/2021, 3, D, 2
9/20/2021, 4, A, 2
9/20/2021, 4, D, 5
9/21/2021, 4, B, 1
9/27/2021, 5, A, 2
9/27/2021, 5, F, 5
9/28/2021, 5, D, 2
10/4/2021, 6, A, 2
10/5/2021, 6, B, 1
10/6/2021, 6, C, 1
10/11/2021, 7, A, 1
10/11/2021, 7, C, 5
10/9/2021, 7, A, 1
10/10/2021, 8, B, 3
10/11/2021, 8, C, 2
10/11/2021, 7, F, 2
10/12/2021, 8, F, 1
10/13/2021, 8, A, 3
10/14/2021, 8, A, 4
10/15/2021, 8, B, 5
10/16/2021, 8, A, 5
10/17/2021, 9, A, 1
10/17/2021, 9, A, 5
10/18/2021, 9, A, 2
10/18/2021, 9, C, 2
10/19/2021, 9, D, 3
10/16/2021, 8, F, 3
10/17/2021, 9, F, 1
10/20/2021, 9, F, 1]
Group by ID,Date;

NoConcatenate

t1:
load *
,if(ID=Peek(ID) and (Date#(Date,'MM/DD/YYYY')-Date#(peek(Date),'MM/DD/YYYY'))=1 ,Peek(sumUnit)+sumUnit,0) as lastCurrent
Resident original order by ID,Date Asc;
Drop table original;


t2:
load *
,peek(lastCurrent) as currentnext
Resident t1 order by ID,Date Desc;
Drop table t1;

Vineeth Pujari
If a post helps to resolve your issue, please accept it as a Solution.