简体   繁体   中英

I need ARRAYFORMULA() to use conditions in another row to decide what to calculate

Here is my formula in G5 =ARRAYFORMULA(IF(AND(J5:J17=true,H5:H17=false),E5:E17*F5:F17*1.075,IF(AND(J5:J17=true,H5:H17=TRUE),0,IF(and(J5:J17=false,H5:H17=false),E5:E17*F5:F17,IF(and(J5:J17=false,H5:H17=true),0)))))

This works if Cols H,I, and J are all false, however once I start setting anything in H,I,or J to true, the formula stops working.

在此处输入图像描述

在此处输入图像描述

Thanks for any help, Im really stumped on this one.

I think you can simplify your formula all the way down to:

=arrayformula(E5:E17*F5:F17*not(H5:H17)*if(J5:J17,1.075,1))

A few pointers:

  • What you were trying to achieve boils down to: 'on each row, give me the value of the cell in colE * the value of the cell in colF, multiplied by 1.075 if the cell in colJ is ticked, unless the cell in colH is ticked, in which case give me 0.
  • You don't need to evaluate tickboxes against TRUE/FALSE as they contain a TRUE/FALSE themselves (so the =TRUEs & =FALSEs in your formula are redundant)
  • AND/OR in arrays don't work the way you might initially want them to as they are aggregating functions (ie they accept arrays as input by design) and wrapping them in ARRAYFORMULA to try to make them evaluate the input arrays row-by-row or column-by-column doesn't work. Historically the answer to this problem has been to use Boolean algebra (where AND is * & OR is +, FALSE=0 and TRUE<>0) as I've done here which normally makes for the most compact representation, but more recently it's also been possible to use MAP to process arrays element-by-element (in which case AND/OR can still be used) as per one of the other answers
  • You only really need to use IF statements if you want to return values other than TRUE/FALSE (1/0) as a result of a logical evaluation (like your colJ where you want TRUE=1.075 & FALSE=1); otherwise you can omit them and just use Booleans.

use:

 =ARRAYFORMULA(IF((J5:J17=true)*(H5:H17=false), E5:E17*F5:F17*1.075,
  IF((J5:J17=true)*(H5:H17=TRUE), 0,
  IF((J5:J17=false)*(H5:H17=false), E5:E17*F5:F17,
  IF((J5:J17=false)*(H5:H17=true), 0)))))

You can try to do this with MAP:

=MAP(J5:J17,H5:H17,E5:E17,F5:F17,LAMBDA(j,h,e,f,
IF(AND(j=true,h=false),e*f*1.075,IF(AND(j=true,h=TRUE),0,IF(and(j=false,h=false),e*f,IF(and(j=false,h=true),0))))))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM