Search code examples
arraysexcelexcel-formulaforeach

For-each loop capability with dynamic arrays without VBA?


I've been trying to figure this out for hours and have tried lots of ways to get this to work (e.g., Let, Offset, Countifs, Lambda, helper columns, etc.), but can't crack this nut yet.

Here's a sample of the spreadsheet. NOTE: this is greatly simplified. The actual spreadsheet has many class start dates, many different events, and very different class structures.

enter image description here

Each team has multiple classes with different start dates. Each team also has a completely different class structure (different events, different timelines). Tstart=number of days from class start date the event occurs. Duration=number of days the event lasts. The "Class start dates" and "Class structure" sections are given, but the entire "Schedule" section needs to be calculated, and it needs to be a running list as shown.

The process order would be:

If "Class start date" isn't blank, then calculate the class schedule based on that team's class structure. (This part I have, but I'm sure there are better ways to do it than what I have).

Then, check one cell below the previous "Class start date" and repeat the above with the output being placed immediately below the previous output. This is the iteration part I can't figure out.

Dynamic arrays haven't played nice because each team schedule has a different number of rows based on having a different number of events.

Haven't been able to figure out how to use Offset or other functions to iterate within a single formula.

Would sure appreciate any help!!!

Here's the given data in markdown table format.

Team Class Class start date Team Event Tstart Duration
Yellow 1 1/15/2024 Yellow Paperwork -10 2
Blue 1 1/20/2024 Yellow Sign in 0 1
Orange 1 1/30/2024 Yellow Interviews 2 2
Yellow 2 2/15/2024 Blue Paperwork -8 2
Blue Sign in 0 1
Blue Interviews 4 2
Blue Farewell 6 1
Orange Paperwork -6 1
Orange Sign in 0 1
Orange Meeting 3 1
Orange Interviews 5 1

Solution

  • Dynamic Arrays' Loop Capability

    • When you get tired of figuring out how to do it with other functions (I did!), the DROP/REDUCE/VSTACK combo comes to the rescue.
    • It might seem mindboggling at the moment but once you get a hang of it, you'll see that many questions can easily be answered with it.
    • The issue with it is that it is rather slow on large datasets (due to the repeated stacking) hence it is avoided whenever possible.
    =LET(cdata,A2:C5,edata,E2:H12,
        DROP(REDUCE("",SEQUENCE(ROWS(cdata)),LAMBDA(rr,r,LET(
            ef,FILTER(edata,CHOOSECOLS(edata,1)=INDEX(cdata,r,1)),
            a,TAKE(ef,,2),
            b,IF(SEQUENCE(ROWS(ef)),INDEX(cdata,r,2)),
            c,INDEX(cdata,r,3)+CHOOSECOLS(ef,3),
            d,c+CHOOSECOLS(ef,4)-1,
            VSTACK(rr,HSTACK(a,b,c,d))))),1))
    

    enter image description here

    Edit (Switched Event and Class Columns)

    =LET(cdata,A2:C5,edata,E2:H12,
        DROP(REDUCE("",SEQUENCE(ROWS(cdata)),LAMBDA(rr,r,LET(
            ef,FILTER(edata,CHOOSECOLS(edata,1)=INDEX(cdata,r,1)),
            a,CHOOSECOLS(ef,1),
            b,IF(SEQUENCE(ROWS(ef)),INDEX(cdata,r,2)),
            c,CHOOSECOLS(ef,2),
            d,INDEX(cdata,r,3)+CHOOSECOLS(ef,3),
            e,d+CHOOSECOLS(ef,4)-1,
            VSTACK(rr,HSTACK(a,b,c,d,e))))),1))
    

    enter image description here