Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
Hi All,
What is the meaning of the expression in script?
Thanks in advance.
if(KEY = previous(KEY), Rangesum(Quantity, peek(CumulSales)), Quantity) as CumulSales,
Hi,
Check with below example
KEY , Quantity
1, 10
1, 20
1, 15
2, 5
2, 4
LOAD
*,
If(KEY = previous(KEY), Rangesum(Quantity, peek(CumulSales)), Quantity) as CumulSales
FROM DataSource
ORDER BY KEY;
The output of above script
KEY , Quantity, CumulSales
1, 10, 10
1, 20, 30 (10 +20) (Since previous key is 1 = Current key 1)
1, 15, 45 (30 + 15) (Since previous key is 1 = Current key 1) Peek(CumuSales) = 30
2, 5, 5 (5) (Here the Previous Key is 1 <> Current Key 2) So gettting only current quantity.
2, 4, 9 (5+ 4) (Since previous key is 2 = Current key 2) Peek(CumuSales) = 5
Hope this helps you.
Regards,
Jagan.
Hi,
Previous() will return the value of the given column in the previous row, in the script we are calculating the Cumulative sales based on the key.
If previous key value and the current key value is same then we are doing the sum of those values, otherwise we are starting the calculation again with the Current Quantity.
Hope this helps you.
Regards,
Jagan.
Hi,
Check with below example
KEY , Quantity
1, 10
1, 20
1, 15
2, 5
2, 4
LOAD
*,
If(KEY = previous(KEY), Rangesum(Quantity, peek(CumulSales)), Quantity) as CumulSales
FROM DataSource
ORDER BY KEY;
The output of above script
KEY , Quantity, CumulSales
1, 10, 10
1, 20, 30 (10 +20) (Since previous key is 1 = Current key 1)
1, 15, 45 (30 + 15) (Since previous key is 1 = Current key 1) Peek(CumuSales) = 30
2, 5, 5 (5) (Here the Previous Key is 1 <> Current Key 2) So gettting only current quantity.
2, 4, 9 (5+ 4) (Since previous key is 2 = Current key 2) Peek(CumuSales) = 5
Hope this helps you.
Regards,
Jagan.
Hi jagan,
I got it. Thank you so much for the detailed explanation.