Why would the following snippet in an expression rule not be able to correctly evaluate iterations past the first two?
with( local!datelisting: {"01/01/2020","01/02/2020","01/03/2020","01/04/2020"}, local!BeginBalanceArray: {100.45,292,0,0}, local!RunningTotal: 0, local!answerRecs: { a!forEach(items: local!datelisting, expression: with ( local!RunningTotal: local!RunningTotal + local!BeginBalanceArray[fv!index], if ( fv!index > 1, (local!BeginBalanceArray[fv!index-1] + local!RunningTotal), local!BeginBalanceArray[fv!index] ) ) ) }, local!answerRecs)
Testing the above gives a list in which the first two iterations seem to evaluate, but it 'forgets' running total after the second iteration:
Also, instead of hardcoding for this simple example, I had tried the array functions updatearray() within the foreach and also append() with the 'BeginBalanceArray' up there initialized to only the first balance, but the array could not be modified in that case. Is there a way to split this out among other expressions, etc? Also tried different combinations of 'load' and 'localvariables', etc. but no dice.
Discussion posts and replies are publicly visible
I believe your code is working as expected. If your actual output should be 100.45, 392.45, 392.45, 392.45 (which I also assume should be) then please try the below code
a!localVariables( local!datelisting: { "01/01/2020", "01/02/2020", "01/03/2020", "01/04/2020" }, local!BeginBalanceArray: { 100.45, 292, 0, 0 }, a!flatten( a!forEach( items: local!datelisting, expression: { sum( index( local!BeginBalanceArray, 1 + enumerate( fv!index ) ) ) } ) ) )
Thanks, this gets me going down the right path. I needed a way to update data like in excel rows, where the last cell of previous row ends up being like the beginning value in the next row. And... I didn't understand the flatten method.
So, if imitation is the sincerest form of 'flattenry', I will carry on with your approach, thanks again!
My bad. Ideally you wouldn't even need the flatten function here. The below piece of code should wok.
a!localVariables( local!datelisting: { "01/01/2020", "01/02/2020", "01/03/2020", "01/04/2020" }, local!BeginBalanceArray: { 100.45, 292, 0, 0 }, a!forEach( items: local!datelisting, expression: sum( index( local!BeginBalanceArray, 1 + enumerate( fv!index ) ) ) ) )