Forum Discussion
boukasa
Feb 25, 2023Brass Contributor
Increment a column only when a corresponding column changes, in a Lambda
I am trying to write a lambda that takes a sorted array an produces a corresponding numbering column that increments when the value in the source array changes. Like this:
param result
1 1
3 2
3 2
7 3
10 4
11 5
11 5
14 6
I have a number of problems like this that are easy to do in a sheet with a formula, but I need to accomplish them in a Lambda, preferably using native array formulas/sequences rather than iterating.
How do I solve this particular problem, and is there are general way to think about this class of problems?
7 Replies
- JosWoolleyIron Contributor
- boukasaBrass ContributorThis is beautiful, thank you.
- OliverScheurichGold Contributor
=SCAN(0,A2:A9,LAMBDA(i,a,IF(OFFSET(a,-1,0)=a,i,i+1)))Another alternative could be this formula.
- Patrick2788Silver Contributor
Here's a solution with MAP:
Dynamic named item 'param' =LET(p, Sheet1!$D$2:$D$1000, nonblank, COUNTA(p), TAKE(p, nonblank)) Lambda 'Locate'. To be used with MAP. Each element in the array will be matched within a unique list of the array. =LAMBDA(e,XMATCH(e, UNIQUE(param))) Sheet level formula: =MAP(param,Locate) - OliverScheurichGold Contributor
- JosWoolleyIron ContributorCool! The parameter passed to LAMBDA can also be interpreted as a range reference where appropriate.
- FikturFoxBrass Contributor