Skip to content

Instantly share code, notes, and snippets.

@ncalm
Created November 24, 2023 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ncalm/7a413296baa61ddeec8ecca57f533bc7 to your computer and use it in GitHub Desktop.
Save ncalm/7a413296baa61ddeec8ecca57f533bc7 to your computer and use it in GitHub Desktop.
Example use of SCAN with GROUPBY to solve gaps and islands problem in Excel
=LET(
test_data, {
45232, 1;
45233, 1;
45234, 2;
45235, 2;
45236, 1;
45237, 1;
45238, 2;
45239, 2;
45240, 3;
45241, 4
},
d, test_data,
g, GROUPBY(
SCAN(
,
SEQUENCE(ROWS(d)),
LAMBDA(a, b, IF(INDEX(d, b, 2) <> INDEX(d, b - 1, 2), a + 1, a))
),
TAKE(d, , 1),
HSTACK(MIN, MAX)
),
VSTACK({"island_id", "MinDate", "MaxDate"}, DROP(g, 1))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment