Basically I have 4 checkboxes and I want to sum up to 4 columns based on those, with the formula query:
It's a bit ugly what I di and I was wondering if there was a more elegant way of doing it, especially if I want to add more checkboxes or even a multiplier to those sums. Thanks in advance for your reply.
example:
IF(M4,
IF(M5,
IF(M6,
IF(M7,
query(A3:E10,"Select A, (SUM(B) + SUM(C) + SUM(D) + SUM(E)) where A is not null group by A label (SUM(B) + SUM(C) + SUM(D) + SUM(E)) 'Q1-4'", 1),
query(A3:E10,"Select A, (SUM(B) + SUM(C) + SUM(D)) where A is not null group by A label (SUM(B) + SUM(C) + SUM(D)) 'Q1-3'", 1)),
IF(M7,
query(A3:E10,"Select A, (SUM(B) + SUM(C) + SUM(E)) where A is not null group by A label (SUM(B) + SUM(C) + SUM(E)) 'Q1 Q2 Q4'", 1),
query(A3:E10,"Select A, (SUM(B) + SUM(C)) where A is not null group by A label (SUM(B) + SUM(C)) 'Q1 Q2'", 1))),
IF(M6,
IF(M7,
query(A3:E10,"Select A, (SUM(B) + SUM(D) + SUM(E)) where A is not null group by A label (SUM(B) + SUM(D) + SUM(E)) 'Q1 Q3 Q4'", 1),
query(A3:E10,"Select A, (SUM(B) + SUM(D)) where A is not null group by A label (SUM(B) + SUM(D)) 'Q1 Q3'", 1)),
IF(M7,
query(A3:E10,"Select A, (SUM(B) + SUM(E)) where A is not null group by A label (SUM(B) + SUM(E)) 'Q1 Q4'", 1),
query(A3:E10,"Select A, (SUM(B)) where A is not null group by A label (SUM(B)) 'Q1'", 1)))),
IF(M5,
IF(M6,
IF(M7,
query(A3:E10,"Select A, (SUM(C) + SUM(D) + SUM(E)) where A is not null group by A label (SUM(C) + SUM(D) + SUM(E)) 'Q2-4'", 1),
query(A3:E10,"Select A, (SUM(C) + SUM(D)) where A is not null group by A label (SUM(C) + SUM(D)) 'Q2 Q3'", 1)),
IF(M7,
query(A3:E10,"Select A, (SUM(C) + SUM(E)) where A is not null group by A label (SUM(C) + SUM(E)) 'Q2 Q4'", 1),
query(A3:E10,"Select A, (SUM(C)) where A is not null group by A label (SUM(C)) 'Q2'", 1))),
IF(M6,
IF(M7,
query(A3:E10,"Select A, (SUM(D) + SUM(E)) where A is not null group by A label (SUM(D) + SUM(E)) 'Q3 Q4'", 1),
query(A3:E10,"Select A, (SUM(D)) where A is not null group by A label (SUM(D)) 'Q3'", 1)),
IF(M7,
query(A3:E10,"Select A, (SUM(E)) where A is not null group by A label (SUM(E)) 'Q4'", 1)))))
here is a link to a sample sheet: Link to a sample sheet
Best Answer
here's a way to achieve this.
The problem with your formula is that your condition is outside the query.
Right now it's something like this :
What I can propose is this :
First, I added some valuable information inside an array with some "settings"
Then i tried to construct a sql statement when only one box is
true
:Output is this
Then when multiples boxes are checked :
Now the final
query
is :Working example here