Forum Discussion
Gas (Petrol) Prices and how they fluctuate…
- Aug 11, 2024
Thanks. The formula
=IF(AND(A2<=A1, A2<A3), LET(r, (A3:A$73<=A2)*ROW(A3:A$73), m, MIN(IF(r>0, r)), IF(m=0, "", m-ROW(A2))), "")
returns the number of days from the last day before an increase to the first date after that with the same or a lower value.
Do not panic! I do not really expect you to adopt this style of programming Excel, but, in case you or some future reader of the thread are interested, I have taken the solution as bit further by introducing a Lambda function.
All that appears on the worksheet is
= DaysToReturnλ(regular)
= DaysToReturnλ(midGrade)
= DaysToReturnλ(premium)
= DaysToReturnλ(diesel)
in cells P2:S2. I have created the chart for diesel prices as well because more seems to be going on there.
For completeness sake the formula, as shown within the advanced formula editor is
/*
"= DaysToReturnλ(grade)
returns period before a value returns to a prior local minumum value"
*/
DaysToReturnλ
= LAMBDA(grade,
LET(
seq, SEQUENCE(COUNT(grade)),
minimum, LocalMinsλ(grade),
location, IF(minimum <> "", seq),
MAP(
location,
minimum,
LAMBDA(k, m,
IF(
k,
LET(
valuesToSearch, DROP(grade, k),
nextOccurrence, XMATCH(m, valuesToSearch, -1),
IFERROR(nextOccurrence, "")
),
""
)
)
)
)
);
/*
"= LocalMinsλ(values)
retains local minimum values whist replacing other values by blank"
*/
LocalMinsλ
= LAMBDA(values,
LET(
prior, DROP(VSTACK(@(+values), values), -1),
next, DROP(VSTACK(values, 0), 1),
minima, IF((values <= prior) * (values < next), values, ""),
minima
)
);
As you see, just as others work to make their Excel formulas more concise, I work to expand them, adding explanations and separating out areas of distinct functionality.