Forum Discussion

Gary1234's avatar
Gary1234
Copper Contributor
Dec 29, 2023
Solved

Sum array by group (quaters, years other) requires extra argument.

I want to group (sum) a dynamic array. The excel sheet attached has 2 lambda functions. The lambda in cell I5 correctly groups the dynamic array. The lambda in cell H5 does not. What is wrong with the lambda in cell H5? I have to provide the sequence as an additional argument instead of defining in the lambda function. What am I doing wrong or is this a bug?

 

Below is the spreadsheet. I will attempt to attach also.

MonthsFirst Month  Count    
241/1/2024  3    
       Not workingWorking
SequenceDateAmount MinMax TotalTotal
11/1/2024$9.73 11 $9.73$9.73
22/1/2024$48.75 12 $9.73$58.48
33/1/2024$22.63 13 $9.73$81.11
44/1/2024$49.99 24 $48.75$121.37
55/1/2024$33.92 35 $22.63$106.54
66/1/2024$2.88 46 $49.99$86.79
77/1/2024$92.52 57 $33.92$129.32
88/1/2024$65.84 68 $2.88$161.24
99/1/2024$98.41 79 $92.52$256.77
1010/1/2024$17.60 810 $65.84$181.85
1111/1/2024$69.09 911 $98.41$185.10
1212/1/2024$32.65 1012 $17.60$119.34
131/1/2025$32.14 1113 $69.09$133.88
142/1/2025$31.93 1214 $32.65$96.72
153/1/2025$80.12 1315 $32.14$144.19
164/1/2025$60.28 1416 $31.93$172.33
175/1/2025$10.58 1517 $80.12$150.98
186/1/2025$30.34 1618 $60.28$101.20
197/1/2025$51.31 1719 $10.58$92.23
208/1/2025$5.46 1820 $30.34$87.11
219/1/2025$43.61 1921 $51.31$100.38
2210/1/2025$7.27 2022 $5.46$56.34
2311/1/2025$89.27 2123 $43.61$140.15
2412/1/2025$78.49 2224 $7.27$175.03

 

The working lambda function in cell I5 is:

 

The non-working lambda function in cell H5 is:

 

Thanks in advance.

Gary

 

  • Gary1234 While I can't provide a complete explanation, it seems that BYROW behaves differently when used on an array than it does with a range. Specifically, it appears the issue is caused by passing the entire row parameter i to each subsequent calculation, even though the array only contains a single column. The solution in this case is to define the iMax variable first as INDEX(i, 1), then use iMax instead of i for the remaining calculations:

     

    =LAMBDA(Amount,Date,Count,
       LET(
          Seq, SEQUENCE(ROWS(Amount)),
          BYROW(Seq, LAMBDA(i, LET(iMax, INDEX(i, 1), iMin, IF((iMax - Count + 1) > 0, iMax - Count + 1, 1), SUM(INDEX(Amount, SEQUENCE(iMax - iMin + 1, 1, iMin))))))
       )
    )(C5:C28, B5#, E2)

     

    The function, as presented, could also be rewritten and simplified as follows:

     

    =LAMBDA(Amount,Count,
    BYROW(SEQUENCE(ROWS(Amount)), LAMBDA(r, LET(
       n, INDEX(r, 1),
       m, MAX(n-Count+1, 1),
       SUM(INDEX(Amount, SEQUENCE(n-m+1,, m)))))))(C5:C28, E2)

     

    Having said that, the function does not actually sum the data by group. Rather, it's summing the previous n rows in the range. If this is the expected result, great. If not, then another approach is needed. Cheers!

3 Replies

  • Gary1234's avatar
    Gary1234
    Copper Contributor

    I am posting the solution to get a 'rolling' sum of an array for others that may search for it later.

     

    The solution uses the excel function scan to define a named function (lambda) named Rolling Sum.

    The function is defined as:

    LAMBDA(Values,Reset,
    SCAN(0, SEQUENCE(ROWS(Values)),
    LAMBDA(a,s, IF(INDEX(Reset, s, 1), 0, a) + INDEX(Values, s, 1)))
    )

     

    The 2 parameters are dynamic arrays (or named ranges) of a single column. The first is the array containing the numbers to compute the rolling sum. The second parameter is an array that specifies when the rolling sum is to be reset.  I have attached the updated example.

     

    Thanks for the help 

    G

  • djclements's avatar
    djclements
    Silver Contributor

    Gary1234 While I can't provide a complete explanation, it seems that BYROW behaves differently when used on an array than it does with a range. Specifically, it appears the issue is caused by passing the entire row parameter i to each subsequent calculation, even though the array only contains a single column. The solution in this case is to define the iMax variable first as INDEX(i, 1), then use iMax instead of i for the remaining calculations:

     

    =LAMBDA(Amount,Date,Count,
       LET(
          Seq, SEQUENCE(ROWS(Amount)),
          BYROW(Seq, LAMBDA(i, LET(iMax, INDEX(i, 1), iMin, IF((iMax - Count + 1) > 0, iMax - Count + 1, 1), SUM(INDEX(Amount, SEQUENCE(iMax - iMin + 1, 1, iMin))))))
       )
    )(C5:C28, B5#, E2)

     

    The function, as presented, could also be rewritten and simplified as follows:

     

    =LAMBDA(Amount,Count,
    BYROW(SEQUENCE(ROWS(Amount)), LAMBDA(r, LET(
       n, INDEX(r, 1),
       m, MAX(n-Count+1, 1),
       SUM(INDEX(Amount, SEQUENCE(n-m+1,, m)))))))(C5:C28, E2)

     

    Having said that, the function does not actually sum the data by group. Rather, it's summing the previous n rows in the range. If this is the expected result, great. If not, then another approach is needed. Cheers!

    • Gary1234's avatar
      Gary1234
      Copper Contributor
      djclements

      Thank you for finding the problem (and for the simplified solution. I had tried the MAX function but had a problem. I think that was due to the error you found. Thanks again.

      I was looking for the previous n rows. That is what I defined as the group. Thanks for making sure.

      Gary