本帖最后由 edwin11891 于 2025-3-5 20:51 编辑
不用辅助列,合并辅助列过程拉个超长火车,先保证结果准确。如果分组方案有调整,修改nGrp参数10即可。如果全部为一个组,nGrp修改为总行数,如示例中为794。
- =LET(sData,A2:D795,sDD,D2:D795,nGrp,10,t,MAP(sDD,LAMBDA(x,MATCH(x,SORT(UNIQUE(sDD),,-1),))),src,HSTACK(SORTBY(sData,t,1),SORT(t)),sgp,GROUPBY(TAKE(src,,-1),TAKE(src,,-1),COUNT,0,0),fx,LAMBDA(arr,nr,res,LET(n,IFERROR(XMATCH(nGrp,SCAN(,arr,SUM),1),nr-ROWS(res)+1),IF(COUNT(arr),fx(DROP(arr,n),nr,VSTACK(res,REPTARRAY(TAKE(res,-1)+1,n))),res))),st,DROP(fx(TAKE(sgp,,-1),ROWS(TAKE(sgp,,-1)),0),1),s,FILTER(st,st<>""),Re,IF(ROWS(s)<ROWS(TAKE(sgp,,-1)),VSTACK(s,REPTARRAY(MAX(st)+1,ROWS(TAKE(sgp,,-1))-ROWS(s))),s),sct,DROP(REDUCE("",UNIQUE(Re),LAMBDA(x,y,VSTACK(x,LET(Sa,FILTER(TAKE(sgp,,1),Re=y),Sb,FILTER(CHOOSECOLS(src,4),(TAKE(src,,-1)>=MIN(Sa))*(TAKE(src,,-1)<=MAX(Sa))),Sc,MAP(Sb,LAMBDA(z,(SUM(N(Sb<z))+SUM(N(Sb=z))/2)/ROWS(Sb))),Sc)))),1),Res,XLOOKUP(TAKE(sData,,1),TAKE(src,,1),sct,"",0),Res)
复制代码 |