Skip to content

Instantly share code, notes, and snippets.

@ncalm
Created October 2, 2024 00:31
Show Gist options
  • Save ncalm/fc6c66803c91f20c2a76e30975b079a8 to your computer and use it in GitHub Desktop.
Save ncalm/fc6c66803c91f20c2a76e30975b079a8 to your computer and use it in GitHub Desktop.
These Excel LAMBDA functions mimic the DataFrame.resample method from the Pandas library
FIRST = LAMBDA(arr, INDEX(arr, 1, 1));
LAST = LAMBDA(arr, INDEX(arr, 1, 1));
RESAMPLE = LAMBDA(rule,
LAMBDA(datetime_index, values, functions,
LET(
week_day, WEEKDAY(datetime_index, 2),
date_year, YEAR(datetime_index),
group_function, SWITCH(
LOWER(rule),
"w", LAMBDA(date, date + 7 - week_day),
"m", LAMBDA(date, MAP(date, LAMBDA(x, EOMONTH(x, 0)))),
"q", LAMBDA(date, MAP(date, LAMBDA(x, EOMONTH(DATE(YEAR(x), 3*CEILING(MONTH(x)/3, 1), 1), 0)))),
"y", LAMBDA(date, DATE(date_year, 12, 31)),
"b", LAMBDA(date, IF(BYROW(week_day, LAMBDA(r, OR(r=6, r=7))), date + (8 - week_day), date)),
"sa", LAMBDA(date, IF(MONTH(date) <= 6, DATE(date_year, 6, 30), DATE(date_year, 12, 31))),
"bw", LAMBDA(date,
LET(
first_date, MIN(date),
first_monday, first_date - WEEKDAY(first_date, 2) + 1,
MAP(date, LAMBDA(x, first_monday + 14*INT((x - first_monday) / 14)))
)
),
LAMBDA(arr, arr)
),
group_by, GROUPBY(group_function(datetime_index), values, functions,,0),
header, IFERROR(CHOOSEROWS(group_by, 2),INDEX(datetime_index,1,1)),
VSTACK(header, DROP(group_by,2))
)
)
);
RESAMPLE.WEEK = RESAMPLE("w");
RESAMPLE.MONTH = RESAMPLE("m");
RESAMPLE.QUARTER = RESAMPLE("q");
RESAMPLE.YEAR = RESAMPLE("y");
RESAMPLE.BUSINESSDAY = RESAMPLE("b");
RESAMPLE.SEMIANNUAL = RESAMPLE("sa");
RESAMPLE.BIWEEKLY = RESAMPLE("bw");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment