SCANBYROW

SCANBYROW(initial_value,array,fn)
initial_value
first value for calculation; can be blank, same as SCAN
array
array to calculate over
fn
a 2-parameter LAMBDA, i.e. LAMBDA(acc,val,acc+val)

Performs a SCAN(initial_value,row,fn) on each row of array.

tboulden

Board Regular
Joined
Jan 14, 2021
Messages
73
Office Version
  1. 365
Platform
  1. Windows
Performs a SCAN(initial_value,row,fn) on each row of array. Corresponding SCANBYCOL is also shown.

BYROW/BYCOL are limited in returning a single value per row/column respectively, whereas SCAN returns an array of the same dimensions as the array being SCANned. We can overcome this limitation by thunk-ing the array result: this is essentially wrapping with a LAMBDA with no parameters, i.e. LAMBDA(x). Then to get the thunked x out, we do LAMBDA(x)() = x.

Excel Formula:
=LAMBDA(initial_value,array,fn,
    LET(
        rows_,ROWS(array),
        cols_,COLUMNS(array),
        row_thunks,
            BYROW(array,
                LAMBDA(row_,
                    LAMBDA(SCAN(initial_value,row_,fn))
                )
            ),
        MAKEARRAY(rows_,cols_,
            LAMBDA(i,j,
                INDEX(
                    INDEX(row_thunks,i,1)(),
                    1,
                    j
                )
            )
        )
    )
)

LAMBDA_SCANBY.xlsx
ABCDEFGHIJ
1
21234
35678
49101112
513141516
6
7=SCANBYROW(,$B$2#, LAMBDA(acc,val,acc+val))=SCANBYROW(10,$B$2#, LAMBDA(acc,val,acc+val))
8
91361011131620
10511182615212836
11919304219294052
121327425823375268
13
14=SCANBYCOL(,$B$2#, LAMBDA(acc,val,acc+val))=SCANBYCOL(10,$B$2#, LAMBDA(acc,val,acc+val))
15
16123411121314
1768101216182022
181518212425283134
192832364038424650
Sheet1
Cell Formulas
RangeFormula
B2:E5B2=SEQUENCE(4,4)
B7,G14,B14,G7B7=FORMULATEXT(B9)
B9:E12B9=SCANBYROW(,$B$2#, LAMBDA(acc,val,acc+val))
G9:J12G9=SCANBYROW(10,$B$2#, LAMBDA(acc,val,acc+val))
B16:E19B16=SCANBYCOL(,$B$2#, LAMBDA(acc,val,acc+val))
G16:J19G16=SCANBYCOL(10,$B$2#, LAMBDA(acc,val,acc+val))
Dynamic array formulas.
 
Upvote 1
@Xlambda My mistake. I thought I had it working without the need for ISOMITTED, but clearly that does not work for all situations. Thank you for the peer review. I guess there's nothing for it... the additional IF statement is needed:

Excel Formula:
SCANBYR:
=LAMBDA(init_val,arr,func,LET(rs,CHAR(30),DROP(SCAN(init_val,EXPAND(arr,,COLUMNS(arr)+1,rs),LAMBDA(a,v,IF(v=rs,v,IF(a=rs,IF(ISOMITTED(init_val),v,func(init_val,v)),func(a,v))))),,-1)))

SCANBYC:
=LAMBDA(init_val,arr,func,LET(rs,CHAR(30),DROP(TRANSPOSE(SCAN(init_val,TRANSPOSE(EXPAND(arr,ROWS(arr)+1,,rs)),LAMBDA(a,v,IF(v=rs,v,IF(a=rs,IF(ISOMITTED(init_val),v,func(init_val,v)),func(a,v)))))),-1)))

My intention was to simply share the concept of using EXPAND-DROP as an alternative. Personally, I wouldn't use a generalized lambda function to scan by row, because most of the simple requests I've received could be solved with EXPAND-DROP and a single IF statement (customized for each situation).

I also shared the bisection method because I thought it was awesome. I don't see the "logic flaw" that you mention, because there are no apparent limits. It seems to handle the SEQUENCE(1000000,10) test with relative ease anyways (15 seconds for SCANBiROW on my system). However, I didn't test it extensively to see under which conditions it might fail (if any). And since I was not the original author of this method, I included the link to the source material (credit lori_m).

No harm in sharing ideas, in my opinion. To each their own.

Kind regards.
 

Forum statistics

Threads
1,223,888
Messages
6,175,219
Members
452,619
Latest member
Shiv1198

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top