Search code examples
google-sheets-formula

Find closest date of matching date an array in google sheets


I'm looking to find the closest matching earlier date for a given name that also restricts values and adapts the following formula which works on an individual line level to work if it was just pasted in E2.

let(Σ,ifna(filter(B:B,A:A=C2,B:B<=C3,B:B>C3-20)),
    xlookup(C3,Σ,Σ,,-1))

In the table below the values in D:E are used to look up and find corresponding values in A:B returning the nearest date that is not less than 20 days earlierin F:F. How can this be reproduced by simply placing one formula (array, map etc) in E2?

name earlier date name later date output formula text
john 3/25/2023 john 3/25/2023 3/25/2023 =let(Σ,ifna(filter(B:B,A:A=D3,B:B<=E3,B:B>E3-20)),xlookup(E3,Σ,Σ,,-1))
salley 3/26/2023 salley 3/26/2023 3/26/2023 =let(Σ,ifna(filter(B:B,A:A=D4,B:B<=E4,B:B>E4-20)),xlookup(E4,Σ,Σ,,-1))
john 3/29/2023 john 3/27/2023 3/25/2023 =let(Σ,ifna(filter(B:B,A:A=D5,B:B<=E5,B:B>E5-20)),xlookup(E5,Σ,Σ,,-1))
salley 3/30/2023 salley 3/28/2023 3/26/2023 =let(Σ,ifna(filter(B:B,A:A=D6,B:B<=E6,B:B>E6-20)),xlookup(E6,Σ,Σ,,-1))
john 3/31/2023 john 3/29/2023 3/29/2023 =let(Σ,ifna(filter(B:B,A:A=D7,B:B<=E7,B:B>E7-20)),xlookup(E7,Σ,Σ,,-1))

Solution

  • You may try:

    =map(D2:D,E2:E,lambda(d,e,if(d="",,let(Σ,ifna(filter(B:B,A:A=d,B:B<=e,B:B>e-20)),xlookup(e,Σ,Σ,,-1)))))
    

    enter image description here