Forum Discussion

boukasa's avatar
boukasa
Brass Contributor
Feb 25, 2023
Solved

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

  • JosWoolley's avatar
    JosWoolley
    Iron Contributor

    boukasa

    Another

    =LET(
        ζ, XMATCH(A2:A9, A2:A9),
        XMATCH(ζ, UNIQUE(ζ))
    )

     Regards

  • Patrick2788's avatar
    Patrick2788
    Silver Contributor

    boukasa 

    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)
    • JosWoolley's avatar
      JosWoolley
      Iron Contributor
      Cool! The parameter passed to LAMBDA can also be interpreted as a range reference where appropriate.

Resources