Forum Discussion
Unpivot Monthly Data with a Formula
My current feeling is that both thunks and MAKEARRAY need to be approached with a certain degree of caution. In the present instance, I decided to use concatenated strings to hold information relating to an item within the crosstab. The code to the point of getting a list of packed records is the same whether I go on to use HSTACK or MAKEARRAY
= LET(
packed, nBravo&","&dates&"|"&rBravo,
mask, LEFT(dates,1)<>"Q",
filtered, FILTER(packed,mask),
list, TOCOL(filtered),
Unpackλ(list)
)
Using HSTACK
UnpackHSλ = LAMBDA(list,
LET(
dimensions, TEXTBEFORE(list, "|"),
d₁, TEXTBEFORE(dimensions, ","),
d₂, TEXTAFTER(dimensions, ","),
val, VALUE(TEXTAFTER(list, "|")),
HSTACK(d₁, d₂, val)
)
);
and using MAKEARRAY
UnpackMAλ = LAMBDA(list,
MAKEARRAY(ROWS(list),3,
LAMBDA(r, f,
LET(
item, INDEX(list, r),
SWITCH(f,
1,TEXTBEFORE(TEXTBEFORE(item, "|"), ","),
2,TEXTAFTER(TEXTBEFORE(item, "|"), ","),
3,VALUE(TEXTAFTER(item, "|"))
)
)
)
)
);
(I do not do 'concise'!)
A key point of interest is that the solution with HSTACK took 10ms to refresh whereas MAKEARRAY took 690ms. That is a massive difference.
Thank you for sharing. The more examples I see with MAKEARRAY, it seems to confirm the function is not be used for re-shaping data. Its best use is in generating random data (I'd argue there are other simpler options available to doing the same task).
Re: calculation times - I have a working knowledge of VBA but would much rather do a task at the sheet level if possible (Especially with the wealth of new functions being released). The way I was told to write code was to minimize 'touching the sheet' (e.g. write data to cells rather than copy/paste, avoid .select, etc.). I'd like to understand where MAKEARRAY's calculation time is coming from. My guess is the use of INDEX and having to 'touch the sheet' more than the HSTACK solution.
- mtarlerMay 23, 2022Silver ContributorI agree that INDEX should take many more cycles than HSTACK. Basically you are evaluating/executing on every element while HSTACK presumably acts on the entire array(s). I presume you could 'test' this somewhat by comparing a 100,000x2 grid compared to a 2x100,000 grid since the former should show significant improvement using HSTACK but MAY (depending on the coding) show much less improvement on the latter.