Subtotals with ArrayFormula()

别说谁变了你拦得住时间么 提交于 2021-01-29 08:54:04

问题


I've created a simple table and trying to split data with subtotals.

  • A indicates the subtotal lines.
  • B contains the rows number for previous subtotal. This is just extra field to simplify formulas.
  • C Contains some amounts.
  • D Contains subtotals of amounts between previous and current subtotal line.

The subtotal formula has the following view:

=ArrayFormula(
    IF($A2:$A; MMULT(
        ($B2:$B < TRANSPOSE(ROW($A2:$A))) * (TRANSPOSE(ROW($A2:$A)) < ROW($A2:$A)); 
        IF(ISNUMBER(C2:C); C2:C; 0)
    ); )
)

The problem is that the formula is extrimely slow. Is there a way to make it faster?

Example file:

https://docs.google.com/spreadsheets/d/1HPGeLZfar2s6pIQMVdQ8mIPzNdw2ESqKAwZfo4IicnA/edit?usp=sharing


回答1:


You could also try this much simpler formula:

=ArrayFormula(
   if(B3:B="","",
         sumif(row(B3:B),"<="&row(B3:B),C3:C)-
         sumif(row(B3:B),"<="&B3:B,C3:C)
   )
)




回答2:


Yes there is

The easier is to remove the blank rows below the data range.

One that might require maintenance, replace open reference like $A2:$A by closed references, i.e. $A2:$A100

One that incresase the formula complexity an volatility, put each open reference inside ARRAY_CONSTRAIN but it's easier to maintain in case that new data rows were added




回答3:


use the "necessary" range:

=ARRAYFORMULA(IFERROR(IF(A2:A; MMULT((
 INDIRECT("B2:B"&MAX(IF(B2:B="";;  ROW(B2:B)))) < TRANSPOSE(ROW(
 INDIRECT("A2:A"&MAX(IF(A2:A=TRUE; ROW(A2:A); )))))) * (TRANSPOSE(ROW(
 INDIRECT("A2:A"&MAX(IF(A2:A=TRUE; ROW(A2:A); ))))) < ROW(
 INDIRECT("A2:A"&MAX(IF(A2:A=TRUE; ROW(A2:A); ))))); IF(ISNUMBER(
 INDIRECT("C2:C"&MAX(IF(C2:C="";;  ROW(C2:C)+1)))); 
 INDIRECT("C2:C"&MAX(IF(C2:C="";;  ROW(C2:C)+1))); 0)); )))

this should be way faster...



来源:https://stackoverflow.com/questions/62645002/subtotals-with-arrayformula

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!