Search code examples
google-sheetsfilterlambdagoogle-sheets-formulagoogle-query-language

search within a list of dates and find the next closest future date


Question:

Given a list of pay periods and sales dates. For each sales date, determine the next pay day.

Pay periods (given) Sale date (given) Pay day (To be generated)
June 4 June 4 June 4
June 17 June 4 June 4
June 30 June 4 June 4
July 15 June 5 June 17
June 5 June 17
June 12 June 17
June 16 June 17
June 18 June 30
June 22 June 30
June 24 June 30
June 28 June 30
June 30 June 30
July 1 July 15
July 7 July 15
July 8 July 15
July 9 July 15
July 10 July 15

Google Sheets: Yellow = given data, Blue = answer to be generated


Clostest I've gotten:

=arrayformula(filter($A$2:$A,abs($A$2:$A-B2)=min(abs($A$2:$A-B2))))

This formula is incorrect as for some of the sales date, the pay day is earlier. The sale date must always come before the pay day because in real life, you're only paid after you make a sale, not before.


Solution

  • try:

    =BYROW(B2:B, LAMBDA(x, SINGLE(FILTER(A2:A, A2:A>=x))))
    

    enter image description here

    or from another tab:

    =BYROW(B2:B, LAMBDA(x, SINGLE(FILTER('New tab'!A2:A, 'New tab'!A2:A>=x))))