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, 2026Brass 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'.
- Medohh2120Oct 21, 2026Brass Contributor
PeterBartholomew1
Apologies for the late reply. I had account access issues.I would always use REDUCE instead of explicit recursion when possible. The issue is that REDUCE needs the number of iterations in advance through its array parameter. In our case, and in some edge cases, we do not know that number. Those are the only cases where explicit recursion is needed.
Otherwise, REDUCE could waste iterations. It might run 10 recursion depths on an array that is only 5 levels deep, because we do not know its depth. The array depth equals the number of recursions needed, so without knowing the depth, we do not know how many iterations to run. In theory, 64 could be used as the maximum, since Excel’s current nesting limit is 64. I have not tested this yet.
I agree that ARRDEPTH returning a misleading value for ragged arrays unless ISRAGGED was TRUE was not ideal. That is why I renamed the functions to RAGDEPTH and UNIDEPTH. The user chooses the correct function, and the functions do not return false numbers. This also allowed simpler code.
We could use one version that slices down regardless of whether the array is ragged or uniform. This adds calculations, but it should be fast enough. The limitation is that it returns #VALUE! beyond 20 levels. I will keep them separate for now and document that limitation.
I prefer failing loudly with #VALUE! instead of padding the result with another value (">20"). A fallback value inside a larger formula can cause less obvious errors. If the user sees #VALUE!, they should immediately know the depth function failed.
Regarding your FLATTEN function, I tested levels = 0 and found that it runs the full depth. I did not know that behavior existed. I tried -1, but it returned an error. It would still be useful if it left the leaf nodes nested one level deep.