Announcements
cancel
Showing results for
Did you mean:
Master II

## expression in script - Meaning

Hi All,

What is the meaning of the expression in script?

if(KEY = previous(KEY), Rangesum(Quantity, peek(CumulSales)), Quantity) as CumulSales,

1 Solution

Accepted Solutions
Luminary Alumni

Hi,

Check with below example

KEY , Quantity

1, 10

1, 20

1, 15

2, 5

2, 4

*,

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.

3 Replies
Luminary Alumni

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.

Luminary Alumni

Hi,

Check with below example

KEY , Quantity

1, 10

1, 20

1, 15

2, 5

2, 4

*,

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.

Master II
Author

Hi jagan,

I got it. Thank you so much for the detailed explanation.

Community Browser