Forum Discussion
pbakseli
Nov 25, 2025Copper Contributor
How to sum chain values from N-number cells?
Hello, I have data of people by age, but 100 different values on a line chart would look... bad. Hence the need to group ages in sets of 5 or 10 on a separe line in chain, but what is the functio...
Patrick2788
Nov 29, 2025Silver Contributor
How about a generalized Lambda to handle this task?
I've included a workbook with GroupbyBinλ. All you would need to do is move your data to this workbook and run the function. I've included a few examples to get started.
Edit: it looks like this forum is still not accepting attachments. Here is the workbook:
GroupbyBinλ Demo.xlsx
For those interested, here's the function:
// Author: Patrick H.
// Date: 11/29/2025
// [Note: part of a larger module to include GroupbyDateλ, Histogramλ, Streakλ,
// Modeλ, and more!]
//--- GroupbyBinλ ---
//
// Description:
// Aggregate one or more value arrays into fixed-width bins
// derived from a numeric field.
//
// Parameters:
// array - Numeric array (1D) used to define bin boundaries.
// values - Array(s) to be aggregated (1D or 2D).
// bin_width - Positive integer specifying bin width (≥1).
// function - ETA-compatible function or single-parameter LAMBDA.
//
// Optional Parameters:
// [field_header] - TRUE if input arrays include headers.
// [chart_prep] - TRUE to return a concise chart-ready layout
// (bin start/end joined into one column).
//
// Notes:
// • If bin_width = 1, behaves like GROUPBY (no binning).
// • If bin_width ≥ (max-min), collapses to a single bin.
// • Chart mode produces {"Bin","Aggregate"} output for plotting.
GroupbyBinλ=
LAMBDA(
array,values,bin_width,function,[field_header],[chart_prep],
LET(
//Dimensions
i, ROWS(array), j, COLUMNS(array),
i_,ROWS(values), j_, COLUMNS(values),
//Header check
Headers?, NOT(ISOMITTED(field_header)),
//Halting scenarios
Is2D?, AND(i>1,j>1),
ShapeMismatch?, i<>i_,
InvalidBin?, OR(ISTEXT(bin_width),bin_width<1),
InvalidFn?, ISERROR(OR(BYROW(SEQUENCE(3,3,1,0),function))),
NonNumeric?, NOT(AND(ISNUMBER(IF(Headers?,DROP(array,1),array)))),
//Logic gate
IF(Is2D?, "#1D-ARRAY!",
IF(ShapeMismatch?, "#DIMENSIONS!",
IF(NonNumeric?, #NUM!,
IF(InvalidBin?, "#BIN-WIDTH!",
IF(InvalidFn?, "#FUNCTION!",
//Proceed
LET(
NoChart?, ISOMITTED(chart_prep),
arr, IF(Headers?,DROP(array,1),array),
val, IF(Headers?,DROP(values,1),values),
//Assign headers
row_header, HSTACK("Bin Start "&IF(Headers?,TAKE(array,1),"")&" Value","Bin End"),
val_header, EXPAND(IF(Headers?,TAKE(values,1),"Total"),,j_,""),
header, HSTACK(row_header,val_header),
//Bin arrays
min, MIN(arr),
start, FLOOR(min,bin_width),
max, MAX(arr),
end, CEILING(max,bin_width),
k, (end-start)/bin_width,
bin, SEQUENCE(k+1,,start,bin_width),
bin_start, DROP(bin,-1),
bin_end, DROP(bin,1),
//Bin assignments
i, XMATCH(arr,bin_start,-1),
row_start, INDEX(bin_start,i),
row_end, INDEX(bin_end,i),
row_fields, HSTACK(row_start,row_end),
//Aggregation
agg, GROUPBY(row_fields,val,function,,0),
//Chart prep
a, CHOOSECOLS(agg,1)&" - <"&CHOOSECOLS(agg,2),
b, DROP(agg,,2),
chart_labels, {"Bin","Aggregate"},
//Return modes
data, VSTACK(header,agg),
chart, VSTACK(chart_labels,HSTACK(a,b)),
delivery, IF(NoChart?,data,chart),
delivery
))))))));