SOLVED

Excel Spill Row Split

%3CLINGO-SUB%20id%3D%22lingo-sub-2464730%22%20slang%3D%22en-US%22%3EExcel%20Spill%20Row%20Split%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2464730%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20wondering%20if%20there%20is%20away%20to%20Split%20the%20Spill%20that%20comes%20from%20the%20Following%20ARRAY.%3C%2FP%3E%3CP%3E%3DSORT(FILTER(FILTER(TableHLP%2CTableHLP%5BDistrict%5D%3D%24A%247)%2C%7B1%2C1%2C0%2C0%2C0%2C0%2C0%2C0%7D)%2C1)%3C%2FP%3E%3CP%3EIt%20produces%20a%20list%20of%20names%20and%20services%20that%20looks%20like%20something%20like%20this.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EName%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20Services%3C%2FP%3E%3CP%3EJohn%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20ABC%3C%2FP%3E%3CP%3EJohn%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20DEF%3C%2FP%3E%3CP%3EJohn%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20GHI%3C%2FP%3E%3CP%3Egreg%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BDEF%3C%2FP%3E%3CP%3Egreg%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BGHI%3C%2FP%3E%3CP%3Ecindy%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20ABC%3C%2FP%3E%3CP%3ECindy%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20GHI%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThere%20is%20about%20100%20Records.%20I%20would%20like%20to%20be%20able%20to%20split%20this%20Data%2030%20records%20on%20sheet1%2C%2030%20on%20sheet2%20and%2030%20on%20sheet3.%20Is%20there%20a%20way%20to%20split%20the%20spill%20by%20providing%20a%20minimum%20and%20maximum%20range%3F%20Second%20question%20is%20there%20away%20to%20add%20a%20Space%20after%20each%20person.%20and%20remove%20duplicate%20names%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EExample%3A%3C%2FP%3E%3CP%3EName%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20Services%3C%2FP%3E%3CP%3EJohn%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20ABC%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20DEF%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20GHI%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Egreg%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BDEF%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20GHI%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ecindy%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20ABC%3C%2FP%3E%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3BGHI%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20tried%20a%20bunch%20of%20different%20things%20and%20have%20hit%20a%20wall.%20Any%20help%20would%20be%20greatly%20appreciated.%3C%2FP%3E%3CP%3EI%20am%20open%20to%20VBA%20or%20macros%20if%20the%20solution%20needs%20to%20go%20there.%3C%2FP%3E%3CP%3EThanks%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2464730%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2464815%22%20slang%3D%22en-US%22%3ERe%3A%20Excel%20Spill%20Row%20Split%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2464815%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1076230%22%20target%3D%22_blank%22%3E%40dshawSLDC%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOn%20each%20sheet%20you%20may%20use%20formulas%20like%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-excel%22%3E%3CCODE%3E%3DLET(k%2C%20SEQUENCE(ROWS(array))%2C%0A%20%20%20%20%20r%2C%20IF(%20(k%20%26gt%3B%3D%20minN)*(k%20%26lt%3B%3D%20maxN)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20INDEX(array%2Ck)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22remove%22)%2C%0A%20%20%20%20%20FILTER(r%2C%20r%20%26lt%3B%26gt%3B%20%22remove%22))%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E
Contributor

I am wondering if there is away to Split the Spill that comes from the Following ARRAY.

=SORT(FILTER(FILTER(TableHLP,TableHLP[District]=$A$7),{1,1,0,0,0,0,0,0}),1)

It produces a list of names and services that looks like something like this.

 

Name        Services

John            ABC

John            DEF

John            GHI

greg             DEF

greg             GHI

cindy            ABC

Cindy            GHI

 

There is about 100 Records. I would like to be able to split this Data 30 records on sheet1, 30 on sheet2 and 30 on sheet3. Is there a way to split the spill by providing a minimum and maximum range? Second question is there away to add a Space after each person. and remove duplicate names?

 

Example:

Name        Services

John            ABC

                    DEF

                    GHI

 

greg             DEF

                    GHI

 

cindy            ABC

                     GHI

 

I have tried a bunch of different things and have hit a wall. Any help would be greatly appreciated.

I am open to VBA or macros if the solution needs to go there.

Thanks,

 

 

28 Replies
best response confirmed by dshawSLDC (Contributor)
Solution

@dshawSLDC 

On each sheet you may use formulas like

=LET(k, SEQUENCE(ROWS(array)),
     r, IF( (k >= minN)*(k <= maxN),
            INDEX(array,k),
            "remove"),
     FILTER(r, r <> "remove"))
This works for splitting across multiple sheets but the array is only showing 1 column. How do i get it to show all column's. everything i try hasn't worked. This is the formula i have been using.

=LET(k, SEQUENCE(ROWS(SORT(FILTER(FILTER(TableHLP,TableHLP[District]='Invoice (2)'!$A$7),{1,1,0,1,1,1,1,1}),1))),
r, IF( (k >= 1)*(k <= 30),
INDEX(SORT(FILTER(FILTER(TableHLP,TableHLP[District]='Invoice (2)'!$A$7),{1,1,0,1,1,1,1,1}),1),k),
"remove"),
FILTER(r, r <> "remove"))

Thanks again for you help.

@dshawSLDC 

Formula could be like

=LET(
array, SORT(FILTER(FILTER(TableHLP,TableHLP[District]=$A$7),{1,1,0,1,1,1,1,1}),1),
k,     SEQUENCE(ROWS(array)),
m,     SEQUENCE(,COLUMNS(array)),
r,     IF(k<5,INDEX(array,k,m),"remove"),
FILTER(r, INDEX(r, 0, 1) <> "remove")
)

Creating an array we already filtered columns.  After that we check each element of it one by one returning it's value or "remove" depends on each row we check.

 

In general if return some columns using INDEX that will be like

=INDEX(Table1,SEQUENCE(ROWS(Table1)),{1,3,4})

 

Please check sample attached.

@Sergei Baklan 

This is the Formula I ended up with trying to produce the end product I need. I will also attach and example file that shows what i have and what Im trying to obtain. Thanks again for your help.

 

 

=LET(
mtbl,TableHLP,mtbd,TableHLP[District],dis,B1,
array,SORT(FILTER(mtbl,mtbd=dis),1),
sf,FILTER(array,{0,0,0,0,1,0,0,0}),af,FILTER(array,{0,0,0,0,0,1,0,0}),rf,FILTER(array,{0,0,0,1,0,0,0,0}),tf,FILTER(array,{0,0,0,0,0,0,1,0}),
min,1,max,35,
k,SEQUENCE(ROWS(array)),ksf,SEQUENCE(ROWS(sf)),kaf,SEQUENCE(ROWS(af)),krf,SEQUENCE(ROWS(rf)),ktf,SEQUENCE(ROWS(tf)),
r,IF((k>=min)*(k<=max),INDEX(array,k),""),rsf,IF((ksf>=min)*(ksf<=max),INDEX(sf,ksf),""),raf,IF((kaf>=min)*(kaf<=max),INDEX(af,kaf),""),
rrf,IF((krf>=min)*(krf<=max),INDEX(rf,krf),""),rtf,IF((ktf>=min)*(ktf<=max),INDEX(tf,ktf),""),
report,FILTER(r,r<>""),reportser,FILTER(rsf,rsf<>""),reportatt,FILTER(raf,raf<>""),reportrat,FILTER(rrf,rrf<>""),reporttot,FILTER(rtf,rtf<>""),
stcnt,SORT(UNIQUE(report)),snct,COUNTA(stcnt),

rep,CHOOSE({1,2,3,4,5,6,7},report,reportser,reportatt,"",reportrat,"",reporttot),rep)

 

 

@dshawSLDC 

From my point of view that's bit overcomplicated, same result gives

=LET(
array, FILTER(TableHLP,TableHLP[District]=$B$1),
k,     SEQUENCE(ROWS(array)),
minK, 1, maxK, 30,
col,   {1,5,6,0,4,0,7,8},
res,   IF( (k>=minK)*(k<=maxK),
       IF(col=0, "", INDEX(array,k,col)),
       "remove"),
FILTER(res, INDEX(res, 0, 1) <> "remove"))

please check in attached file.

 

Not sure right now how to empty repeated names, will play with it some later.

@dshawSLDC 

If skip repeated names

=LET(
array, SORT(FILTER(TableHLP,TableHLP[District]=$B$1)),
col,   {1,5,6,0,4,0,7,8},
headers, IF(col=0, "", INDEX(TableHLP[#Headers], 1, col)),
n,      ROWS(array),
k,      SEQUENCE(n+1,,0,1),
minK, 1, maxK, 35,
res,    IF(k=0, headers,
        IF( (k>=minK)*(k<=maxK),
        IF(col=0, "",
           IF( (INDEX(array,k-1,col)=INDEX(array,k,col))*
               ((col=1)+(col=8))*
               (k<>minK),
                "", INDEX(array,k,col) )  ),
         "remove")),
FILTER(res, (INDEX(res, 0, 1) <> "remove")+(INDEX(res, 0, 1) = "Name"))
)

which gives

image.png

@Sergei Baklan 

Wow that formula is way cleaner than mine. Question is there away to drop the name total down to the end line of each Name? Also can we put a blank row in after each name? Thanks again for all your help. I really appreciate it.

@dshawSLDC 

Name Total to down could be

=LET(
array,  SORT(FILTER(TableHLP,TableHLP[District]=$B$1)),
col,    {1,5,6,0,4,0,7,8},
headers, IF(col=0, "", INDEX(TableHLP[#Headers], 1, col)),
n,       ROWS(array),
k,       SEQUENCE(n+2,,0,1),
minK, 1, maxK, MIN(35, n),
v,       INDEX(array,k,col),
vPrev,   INDEX(array,k-1,col),
vNext,   INDEX(array,k+1,col),
res,    IF(k=0, headers,
           IF( (k>=minK)*(k<=maxK),
               IF(col=0, "",
               IF( (vPrev=v)*(col=1)* (k<>minK),
                     "",
                    IF( k<maxK,
                    IF( (vNext=v)*(col=8), "",  v ),
                    v )
                )   ),
               "remove")),
FILTER(res, (INDEX(res, 0, 1) <> "remove")+(INDEX(res, 0, 1) = "Name"))
)

result is

image.png

With empty row is more complex, we need to calculate number of names in given block and correctly work with bounds. Maybe next time. 

As workaround you may apply conditional formatting to separate blocks with names, as variant

image.png

@Sergei Baklan 

Thanks for your assistance again. I updated the code to count names to provide the min and max that way is doesn't cut off services. The only thing left is to add the row spacing. trying to use the same count function to do this. this is the code Im trying to use. 

afu,UNIQUE(af),
cc,IF((af>=mins)*(af<=maxs),INDEX(afu,af),"remove"),ccr,FILTER(cc,(INDEX(cc,0,1)<>"remove")),

Im trying to take afu and add those to ccr to get the extra spacing.

 

The following is the updated code with the counting.

=LET(
array, SORT(FILTER(TableHLP,TableHLP[District]=$A$7)),array1,SORT(FILTER(FILTER(TableHLP,TableHLP[District]=$A$7),{1,0,0,0,0,0,0,0})),
col,    {1,5,6,0,4,0,7,8},
mins,1,maxs,8,
suc, UNIQUE(array1),
af,MATCH(array1,suc,0),
cn,IF((af>=mins)*(af<=maxs),INDEX(suc,af),"remove"),cnt,FILTER(cn,(INDEX(cn,0,1)<>"remove")),
c, ROWS(cnt),

afu,UNIQUE(af),
cc,IF((af>=mins)*(af<=maxs),INDEX(afu,af),"remove"),ccr,FILTER(cc,(INDEX(cc,0,1)<>"remove")),

n,       ROWS(array),
k,       SEQUENCE(n+2,,0,1),
minK, 1, maxK, MIN(c, n),
v,       INDEX(array,k,col),
vPrev,   INDEX(array,k-1,col),
vNext,   INDEX(array,k+1,col),
res,    IF( (k>=minK)*(k<=maxK),
               IF(col=0, "",
               IF( (vPrev=v)*(col=1)* (k<>minK),
                     "",
                    IF( k<maxK,
                    IF( (vNext=v)*(col=8), "",  v ),
                    v )
                )   ),
               "remove"),
Final,FILTER(res, (INDEX(res, 0, 1) <> "remove")+(INDEX(res, 0, 1) = "Name")),
Final
)

 I also attached the example with the updated code.

Thanks again.

@dshawSLDC 

I did that on mockup with the table (one block)

image.png

formula is

=LET(
  array, Table1,
  columnsToShow, {1,2,4},
  columnNameTotal, 4,
  arrayRows, ROWS(array),
  names,        INDEX(array, SEQUENCE(arrayRows),1),
  distinct,     UNIQUE(names),
  distinctRows, ROWS(distinct),
  startPos,     INDEX( XMATCH(distinct, names),
                       SEQUENCE(,distinctRows-1,2)) +
                SEQUENCE(,distinctRows-1,0),
  enumPos,      SEQUENCE( COLUMNS(startPos),,1,0),
  k,            SEQUENCE(arrayRows+distinctRows-1),
  i,            k - MMULT(--(k>startPos), enumPos ),
  emptyPos,     MMULT(--(k=startPos), enumPos ),
  v,            INDEX(array, i, columnsToShow),
  vPrev,        INDEX(array,  i-1, columnsToShow),
  vNext,        INDEX(array, i+1, columnsToShow),
  result, IF(
             emptyPos, "",
             IF( (vPrev=v)*(columnsToShow=1)*(i<>1), "",
             IF( i < arrayRows,
             IF( (vNext=v)*(columnsToShow=columnNameTotal), "",  v), v)
             ) ),
 result )

@Sergei Baklan 

Thank you very much for all your help. I took what you had there and added the ability to control how many names appear on 1 sheet. I'm sure there is a more eloquent way of doing this. take a look and let me know what you think. Again thank you.

=LET(
array, SORT(FILTER(TableHLP,TableHLP[District]=$A$7)),array1,SORT(FILTER(FILTER(TableHLP,TableHLP[District]=$A$7),{1,0,0,0,0,0,0,0})),
columnsToShow,    {1,5,6,0,4,0,7,8},
columnNameTotal, 4,
lmaxs,1,
mins,1,maxs,8,
suc, UNIQUE(array1),
af,MATCH(array1,suc,0),
cn,IF((af>=mins)*(af<=maxs),INDEX(suc,af),"remove"),cnt,FILTER(cn,(INDEX(cn,0,1)<>"remove")),
c, ROWS(cnt),
cnm,IF((af>=mins)*(af<=lmaxs),INDEX(suc,af),"remove"),cntm,FILTER(cnm,(INDEX(cnm,0,1)<>"remove")),
cm, ROWS(cntm),
arrayR, ROWS(array),
cntseq, SEQUENCE(arrayR,,0,1),
minK,IF(lmaxs=1,mins,cm+1), maxK, MIN(c, arrayR),
removeArray,IF((cntseq>=minK)*(cntseq<=maxK),INDEX(array,cntseq,{1,2,3,4,5,6,7,8}),"remove"),
trunkArray,FILTER(removeArray,(INDEX(removeArray,0,1)<>"remove")+(INDEX(removeArray,0,1)= "Name")),

  arrayRows, ROWS(trunkArray),
  names, INDEX(trunkArray, SEQUENCE(arrayRows),1),
  distinct,  UNIQUE(names),
  distinctRows, ROWS(distinct),
  startPos, INDEX( XMATCH(distinct, names),
                               SEQUENCE(,distinctRows-1,2)) +
                  SEQUENCE(,distinctRows-1,0),
  enumPos, SEQUENCE( COLUMNS(startPos),,1,0),
  k, SEQUENCE(arrayRows+distinctRows-1),
 i, k - MMULT(--(k>startPos), enumPos ),
 emptyPos, MMULT(--(k=startPos), enumPos ),
 v, INDEX(trunkArray, i, columnsToShow),
 vPrev, INDEX(trunkArray,  i-1, columnsToShow),
 vNext, INDEX(trunkArray, i+1, columnsToShow),
 res, IF(
             emptyPos, "",
             IF( (vPrev=v)*(columnsToShow=1)*(i<>1), "",
             IF( i < arrayRows,
             IF( (vNext=v)*(columnsToShow=columnNameTotal), "",  v), v)
              ) ),
res)

@Sergei Baklan 

I did find an error in my code. 

columnsToShow,    {1,5,6,0,4,0,7,8},

I the 0 will return the name. Im trying to figure out how to prevent that. 

@dshawSLDC 

I Think I got it. Check out the Formula and let me know what you think.

=LET(
array, SORT(FILTER(TableHLP,TableHLP[District]=$A$7)),array1,SORT(FILTER(FILTER(TableHLP,TableHLP[District]=$A$7),{1,0,0,0,0,0,0,0})),
columnsToShow,    {1,5,6,0,4,0,7,8},
columnNameTotal, 8,
lmaxs,1,
mins,1,maxs,8,
suc, UNIQUE(array1),
af,MATCH(array1,suc,0),
cn,IF((af>=mins)*(af<=maxs),INDEX(suc,af),"remove"),cnt,FILTER(cn,(INDEX(cn,0,1)<>"remove")),
c, ROWS(cnt),
cnm,IF((af>=mins)*(af<=lmaxs),INDEX(suc,af),"remove"),cntm,FILTER(cnm,(INDEX(cnm,0,1)<>"remove")),
cm, ROWS(cntm),
arrayR, ROWS(array),
cntseq, SEQUENCE(arrayR,,0,1),
minK,IF(lmaxs=1,mins,cm+1), maxK, MIN(c, arrayR),
removeArray,IF((cntseq>=minK)*(cntseq<=maxK),INDEX(array,cntseq,{1,2,3,4,5,6,7,8}),"remove"),
trunkArray,FILTER(removeArray,(INDEX(removeArray,0,1)<>"remove")+(INDEX(removeArray,0,1)= "Name")),

  arrayRows, ROWS(trunkArray),
  names, INDEX(trunkArray, SEQUENCE(arrayRows),1),
  distinct,  UNIQUE(names),
  distinctRows, ROWS(distinct),
  startPos, INDEX( XMATCH(distinct, names),
                               SEQUENCE(,distinctRows-1,2)) +
                  SEQUENCE(,distinctRows-1,0),
  enumPos, SEQUENCE( COLUMNS(startPos),,1,0),
  k, SEQUENCE(arrayRows+distinctRows-1),
 i, k - MMULT(--(k>startPos), enumPos ),
 emptyPos, MMULT(--(k=startPos), enumPos ),
 v, IF(columnsToShow=0,"",INDEX(trunkArray, i, columnsToShow)),
 vPrev, INDEX(trunkArray,  i-1, columnsToShow),
 vNext, INDEX(trunkArray, i+1, columnsToShow),
 res, IF(
             emptyPos, "",
             IF( (vPrev=v)*(columnsToShow=1)*(i<>1), "",
             IF( i < arrayRows,
             IF( (vNext=v)*(columnsToShow=columnNameTotal), "",  v), v)
              ) ),
res)

@Sergei Baklan 

Well I think it is finished. Thank you for all your help. I learned a lot. Let me know what you think. I know its not as elegant as your code and could probably be cleaned up. Let me know if there is anything that could be done simpler. Thanks again.

=LET(
array, SORT(FILTER(TableHLP,TableHLP[District]=$B$1)),array1,SORT(FILTER(FILTER(TableHLP,TableHLP[District]=$B$1),{1,0,0,0,0,0,0,0})),
columnsToShow,    {1,5,6,0,4,0,7,8},
columnNameTotal, 8,
lmaxs,1,
mins,1,maxs,3,
suc, UNIQUE(array1),
af,MATCH(array1,suc,0),
cn,IF((af>=mins)*(af<=maxs),INDEX(suc,af),"remove"),cnt,FILTER(cn,(INDEX(cn,0,1)<>"remove")),
c, ROWS(cnt),
cnm,IF((af>=mins)*(af<=lmaxs),INDEX(suc,af),"remove"),cntm,FILTER(cnm,(INDEX(cnm,0,1)<>"remove")),
cm, ROWS(cntm),
arrayR, ROWS(array),
cntseq, SEQUENCE(arrayR,,0,1),
minK,IF(lmaxs=1,mins,cm+1), maxK, MIN(c, arrayR),
removeArray,IF((cntseq>=minK)*(cntseq<=maxK),INDEX(array,cntseq,{1,2,3,4,5,6,7,8}),"remove"),
trunkArray,FILTER(removeArray,(INDEX(removeArray,0,1)<>"remove")+(INDEX(removeArray,0,1)= "Name")),

  arrayRows, ROWS(trunkArray),
  names, INDEX(trunkArray, SEQUENCE(arrayRows),1),
  distinct,  UNIQUE(names),
  distinctRows, ROWS(distinct),
  startPos, INDEX( XMATCH(distinct, names),
                               SEQUENCE(,distinctRows-1,2)) +
                  SEQUENCE(,distinctRows-1,0),
  enumPos, SEQUENCE( COLUMNS(startPos),,1,0),
  k, SEQUENCE(arrayRows+distinctRows-1),
 i, k - MMULT(--(k>startPos), enumPos ),
 emptyPos, MMULT(--(k=startPos), enumPos ),
 v, IF(columnsToShow=0,"",INDEX(trunkArray, i, columnsToShow)),
 vPrev, INDEX(trunkArray,  i-1, columnsToShow),
 vNext, INDEX(trunkArray, i+1, columnsToShow),
 res, IF(
             emptyPos, "",
             IF( (vPrev=v)*(columnsToShow=1)*(i<>1), "",
             IF( i < arrayRows,
             IF( (vNext=v)*(columnsToShow=columnNameTotal), "",  v), v)
              ) ),
res)

 

@dshawSLDC 

Looks great. Perhaps the code could be bit optimized, but that's on another iterations. What I'd do now - apply at least minimum of formatting to make function easier to read, and use more descriptive name. Perhaps except indexes. Otherwise in few month you forget what is what and it takes more time to analyze the code.

 

Comments here if can be used when indirectly. I'm not fun of comments, if use very few of them is enough and they shall answer on question why we do that, not what we do here. Descriptive names is enough for that.

 

Quite small cosmetic changes are in attached file, spill at very bottom.

Thanks, Again I am very appreciative of your time and willingness to help.

@dshawSLDC , you are welcome, that was interesting task

@Sergei Baklan 

Hey I found an error. If you only have 1 name in a district you get a #CALC! error " empty arrays are not supported". any thoughts on how to fix this?

@dshawSLDC 

Perhaps to wrap by IFERROR(), but don't know what exactly. If you don't solve will try to play tomorrow.