microsoft-dynamicserpnavisioncaldynamics-al

How to subtract current value from previous value in a column using C/AL code


I have a column that I need to subtract a current value from the previous value in Bid Price LCY column and save the result on another column called Daily Return(Offer). The value is incrementing instead of decrementing in Daily Return(Offer) column.

Here is the image of what I am getting enter image description here

Here is my code:

LastDailyReturn := 0;
TempFundPriceNew.RESET;
TempFundPriceNew.SETCURRENTKEY("Fund No.",Date);
TempFundPriceNew.SETRANGE(TempFundPriceNew."Fund No.","Temp Fund Price New"."Fund No.");
TempFundPriceNew.SETRANGE(TempFundPriceNew.Date,StartDate,"Temp Fund Price New".Date);
IF TempFundPriceNew.FINDFIRST THEN REPEAT
   LastDailyReturn += "Temp Fund Price New"."Bid Price LCY";
UNTIL TempFundPriceNew.NEXT = 0;

Solution

  • i think that this could be a solution:

    Filter the current table with the range of data that you want;

    Do the FindFirst, in this case should find the 8/1/2021;

    Create a counter, and inside the loop increment it by one;

    Use the function XRec, in order to have the previous record obtained or use the function Until.Next() - 1 in order to go back or still in the loop you can save the previous record value into a dummy variable in order to have it the next cycle of loop;

    Do the difference between each other and insert it inside the table;