Forum Discussion
Spilled Array based on different Time Periods and applied periods - Help Needed
I was going to leave this one to David, but somehow I got embroiled with some interesting results. Firstly, I do not think I have ever worked with a variable pitch timeline before so generating it was a challenge; I am still not sure what is the most economical way of specifying it.
Turning to the expenses, I wrote a function that returns the payment dates for a single expense
COUNTPMTλ = LAMBDA(start, end, interval,
LET(
duration, 1 + DATEDIF(start, end, "m"),
nPmts, duration / interval,
counter, SEQUENCE(1, nPmts, 0),
pmtDate, EOMONTH(start, counter),
TOROW(DROP(FREQUENCY(pmtDate, Start_of_Period),1))
)
)I was planning to assign each payment to a time period and use GROUPBY to accumulate frequent payments over the longer reporting intervals. Then it occurred to me that using the timeline as FREQUENCY bins would achieve the same goal. Hence the function as it is.
My next issue was that there are multiple expense lines. The first thought is to use MAP to run over the various lines. Except, that returns a nested array error (in common with every other worthwhile problem I have ever seen - OK, so that's probably an exaggeration!) That is where I turned to a function MAPλ I had written earlier and posted to GitHub as a Gist.
The formula I finished with was
= ext.MAPλ(Start_Date, End_Date, Applied_Periods, COUNTPMTλ)where ext is a module that I created on this occasion to hold Excel extensions.
cc djclements
- PeterBartholomew1Oct 12, 2024Silver Contributor
The defined name Start_of_Period is hard-wired into the COUNTPMTλ function, where it is used to define the bins of the Excel FREQUENCY function. To avoid that would require Currying so that the worksheet formula becomes
= ext.MAPλ(Start_Date, End_Date, Applied_Periods, COUNTPMTλ(Start_of_Period))and the Lambda function is
COUNTPMTλ = LAMBDA(timeline, LAMBDA(start, end, interval, LET( duration, 1 + DATEDIF(start, end, "m"), nPmts, duration / interval, counter, SEQUENCE(1, nPmts, 0), pmtDate, EOMONTH(start, counter), TOROW(DROP(FREQUENCY(pmtDate, timeline),1)) ) ))The additional LAMBDA means that the function required two sets of arguments, ordered left to right, before it evaluates. The first is provided explicitly in the worksheet formula whilst the second set are provided term at a time by the ext.MAPλ helper function.
- PeterBartholomew1Oct 13, 2024Silver Contributor
Just in case a copy of the modified workbook is of value...