Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
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?
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];