Forum Discussion
Excel Nested Arrays: Find the Nesting Depth of Uniform and Ragged Arrays ARRDEPTH() & ISRAGGED()
I lack your experience of what to expect of other languages (my Fortran experience from many years ago is hardly relevant). I think the list of keyword/entity pairs that I applied your functions to is inherently ragged so returning 1 did not help me much, since it characterises the keyword rather than the entity depths. I guess it was MAXDEPTH that I had in mind, as one of my requirements was to know how many times to apply FLATTEN. My concern was that returning a value of 1 was positively misleading.
- Medohh2120Oct 11, 2026Tin Contributor
I agree, it sounds unhelpful. I actually tried changing it to MAXDEPTH, but then hit this:
The function breaks if the nesting level is more than 20 in the non-linear search, probably due to Excel’s auto-recursion limits. I can’t confirm it, since Excel recursion limits have always been ambiguous to me.
What I do know: as long as depth is 20 or less, it works regardless of array size.
Excel has been returning unexpected errors lately, sometimes #VALUE! inside IFERROR, or volatility behavior from non-volatile functions. That makes me less confident that this is purely a recursion limit; it could be related to an Excel bug or unstable calculation behavior. But the linear approach works up to 64, (Excel’s maximum nesting depth)
/* Name: MakeNestArr Description: Creates a uniform-nested array given size and element depth. Example: = MakeNestArr(2,1,3) -> {{{0}};{{0}}} [element_depth]: Controls how many nested-array levels are created. Omitted defaults to 2. */ MakeNestArr = LAMBDA(rows, columns, [element_depth], MAKEARRAY( rows, columns, LAMBDA([r], [c], REDUCE( 0, SEQUENCE(IFOMITTED(element_depth, 2) - 1), LAMBDA(acc, [nxt], {acc}) ) ) ) );Actual test:
=ARRDEPTH(MakeNestArray(100,100,20),TRUE) // works =ARRDEPTH(MakeNestArray(1,1,21),TRUE) // retunrs #VALUE! =ARRDEPTH(MakeNestArray(1,1,64)) // worksIf we make MAXDEPTH the default, it will always break for arrays deeper than 20. I know 20-level-deep arrays are unrealistic in practice, but do you still agree with changing it to MAXDEPTH?
As for your scenario of:
how many times to apply FLATTEN
I’m assuming you want to completely flatten any array. I haven’t tested much, but I think FLATTEN internally uses recursion and stops once all arrays are flat. So prematurely setting [levels] to 64 on a 2-level array shouldn’t hurt performance much:
=BENCHMARK(LAMBDA(FLATTEN(MakeNestArray(50,50,21),,64))) // took 140ms =BENCHMARK(LAMBDA(FLATTEN(MakeNestArray(50,50,21),,21))) // took 130msOn my side the difference was almost negligible. Perhaps you could add something like this instead:
FLATTENALL = LAMBDA(array, [pad_with], FLATTEN(array, pad_with, 64));- PeterBartholomew1Oct 11, 2026Silver Contributor
I used to know the limit on the number of parameters Excel will accept in a formula but, sadly, I have forgotten it since REDUCE appeared, and I largely stopped using explicit recursion. I remember that it sounded insanely high, but in a recursive formula, every level gives rise to a further set of parameters, so it is easy to hit the limit. I remember Microsoft doubled the limit to make recursive formulas more viable.
Clearly building a function MAXDEPTH knowing it will error for some valid data is unappealing. One possibility might be to terminate the recursion at 16 (say) and return ">16" in place of a definitive result.
In response to your suggested function FLATTENALL, I would observe that FLATTEN had a levels parameter added shortly before being released for beta testing, so with levels=1 the function flattens a single level whereas levels=0 runs full depth (see attached workbook). There might be a case for levels=-1 meaning 'leave the leaf nodes nested 1 deep for the next process step'.