Expression rule needs to keep running total among items, is foreach causing me issue?

Certified Associate Developer

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:  

  • List of Number (Floating Point) - 4 items
      • 100.45(Number (Decimal))
        • 392.45(Number (Decimal))
          • 292(Number (Decimal))
            • 0(Number (Decimal))

          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

          Parents
          • 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
                        )
                      )
                    )
                  }
                )
              )
            )

          • 0
            Certified Associate Developer
            in reply to Siddharth

            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!

          Reply Children
          • 0
            A Score Level 1
            in reply to ericc0002

            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
                    )
                  )
                )
              )
            )