-1

I wish to derive cumulative column based on initially adding the daily amount to the Value, then subsequently adding the daily amount to the resulting figure.

Could you please help, thanks.

Date Type Value Rate Cummulative
29/04/2022 A 128.61 32.00 256.61
28/04/2022 A 128.61 32.00 224.61
27/04/2022 A 128.61 32.00 192.61
26/04/2022 A 128.61 32.00 160.61
Stefan Stoichev
  • 4,615
  • 3
  • 31
  • 51

1 Answers1

0

Have a look at the example script below. Once reloaded the CumulativeData table will contain new column Cumulative which will be the result.

The "magic" is happening in the following expression:

if(RecNo() = 1, 
     Value + Rate, 
     peek(Cumulative) + Rate
) as Cumulative

In the expression we are saying:

  • if the record number is 1 (first row of the table) then sum Value and Rate values. This is our "base" and we'll accumulate to this value
  • for the next rows get the above (previous row) value of Cumulative column (peek(Cumulative)) and add the current row Rate value

More about peek function can be found at the documentation page

Example script:

RawData:
Load * Inline [
Date      , Type,   Value , Rate
29/04/2022, A   ,   128.61, 32.00
28/04/2022, A   ,   128.61, 32.00
27/04/2022, A   ,   128.61, 32.00
26/04/2022, A   ,   128.61, 32.00
];

// Dont foget to order the table in ascending order
CumulativeData:
Load 
  *,
  if(RecNo() = 1, 
       Value + Rate, 
       peek(Cumulative) + Rate
  ) as Cumulative
Resident
  RawData
Order By
  Date ASC
;

Drop Table RawData;

Result table:

Result table

Stefan Stoichev
  • 4,615
  • 3
  • 31
  • 51
  • Hi @Stefan Stoichev I omiited an essential part of my requirement, that's why a type has multiple IDs. Could you kindly help me factor this in the initial solution you provided. Date Type ID Value Rate Cumulative 29/04/2022 A A2126334530 408.36 1.77 410.13 29/04/2022 A A2126394105 3,610.75 16.05 3,626.80 29/04/2022 A A2126584230 4,654.73 22.06 4,676.79 29/04/2022 A A2130920480 10,107.63 60.16 10,167.79 29/04/2022 A A2204715054 4,824.64 73.10 4,897.74 – LoKi_Asterix Apr 27 '22 at 12:59
  • if you add the `ID` column to the `Order By`? `... Order By Date, ID ASC ...` – Stefan Stoichev Apr 27 '22 at 14:11