EagleEye
EagleEye

Reputation: 510

How can I use Array Formula with multiple SUMIFs?

I am a beginner with google sheets formulas, I created a formula which is not very efficient nevertheless it works and gives correct output, here is the formula:

=if($C$2:$C = "Check-Out",
 SUM((SUMIFS($D$2:$D,$C$2:$C,"Check-In",$B$2:$B,B2,$A$2:$A,"<"&A2)+
      SUMIFS($D$2:D,$C$2:$C,"Request Extra Key",$B$2:$B,B2,$A$2:$A,"<"&A2))-
(SUM(SUMIFS($D$2:$D,$C$2:$C,"Check-Out",$B$2:$B,B2,$A$2:$A,"<"&A2)+ 
     SUMIFS($D$2:$D,$C$2:$C,"Return Extra Key",$B$2:$B,B2,$A$2:$A,"<"&A2)))),D2)

I want to copy this formula automatically to the last nonempty row, and I used ArrayFormula to copy this to last row:

=ARRAYFORMULA(if($C$2:$C = "Check-Out" ,SUM((SUMIFS($D$2:$D,$C$2:$C,
  "Check-In",$B$2:$B,B2:B,$A$2:$A,"<"&A2:A)+SUMIFS($D$2:$D,$C$2:$C,
  "Request Extra Key",$B$2:$B,B2:B,$A$2:$A,"<"&A2:A))-
   (SUM(SUMIFS($D$2:$D,$C$2:$C,"Check-Out",$B$2:$B,B2:B,$A$2:$A," 
  <"&A2:A)+SUMIFS($D$2:$D,$C$2:$C,"Return Extra Key",$B$2:$B,B2:B,$A$2:$A,"
   <"&A2:A)))),$D$2:$D))

it does copy the formula to the last row but the output values are not correct, here is the sheet, Column E contains correct values by dragging down the formula. I want to get same values by using ArrayFormula. Any help would be much appreciated. Thank you

Upvotes: 0

Views: 76

Answers (1)

Musharaf Dhanji
Musharaf Dhanji

Reputation: 11

From what I'm understanding you're trying to keep track of how many keys are dispensed and whether it is permitted to dispense an additional key or not (based on rules).

For this I would take a different approach.

I would use a separate Tab (KeyCount) for calculations and only return the OK or NOT OK message back to the KeyLog Tab.

In the KeyCount Tab I would list all the Keys and the number of times they were added to the system, subtracted by check ins etc.

KeyCount Tab

| Key ID | Add Key | Check-In | Check-Out | Return Extra | Count |

For the Key ID Column I would list all the unique keys: =unique(KeyLog!B2:B)

For Add Key I would sum all the entries that list that unique key: =arrayformula(sumif(KeyLog!B2B:B,KeyCount!A2:A,KeyLog!D2:D))

For Check-In I would do the same as above but make it a negative by surrounding the formula with -( ). This way this will be a negative number regardless of the sum.

Do the same as above for the other Triggers.

Finally I would add a Count column which simply uses an Array to add and subtract the different triggers in each row to tell you the current balance of keys for each unique key.

This is the figure you use in your KeyLog to decide if it's OK or NOT OK.

BONUS: You can set up a table in another tab which decides if its ok or not ok. For example if number of keys should not exceed 2, then have your array formula in KeyLog look up this table table and return the OK or NOT OK Message. This way if you change policies and allow extra keys you can easily change the rules in this table without messing with the formulas in other tabs. You can also add a longer explanation message such as "Not enough keys" or "Number of allowed keys exceeded" to this table which can show in the KeyLog table.

I hope this helps you solve your problem. Let me know if you need further explaining.

Happy coding!

Upvotes: 1

Related Questions