Google-sheets – Summing Columns Based off Different Criteria in a group by in a Query Function in Google Sheets

google sheetsgoogle-sheets-query

I would like to know how to sum a column based on different criteria in a group by in google sheets query function.
Here's the data
And
Here's what I want

Currently, I have a separate query for each summed column, and I need to copy down the row whenever the customer changes and the service providers change.
Is there some way to put this into one formula so I don't need to always be copying and pasting? If one could put a query function into an array formula, that would be useful, but sadly, that is not possible. Group by allows summing of 1 column, so if I could just have multiple sums, that would be very useful.
Does anyone know the best way to do this?
Thanks,

Best Answer

Brandon, I have added three sheets to your sample spreadsheet.

"Erik Notes" has notes (as found below).

"Erik Help" holds four formulas: in A2, B2, C2 and D2. Each "runs" its own column.

In A2:

=ArrayFormula({"Name";SORT(UNIQUE(FILTER(data!D2:D,data!E2:E=A1)))})

In B2:

=ArrayFormula({"Direct";IF(A3:A="",,IFERROR(VLOOKUP(A3:A,QUERY({data!C:F},"Select Col2, SUM(Col4) Where Col1 Contains 'Direct' AND Col3 = '"&A1&"' Group By Col2"),2,FALSE)))})

In C2:

=ArrayFormula({"Consult";IF(A3:A="",,IFERROR(VLOOKUP(A3:A,QUERY({data!C:F},"Select Col2, SUM(Col4) Where Col1 Contains 'Consult' AND Col3 = '"&A1&"' Group By Col2"),2,FALSE)))})

In D2:

=ArrayFormula({"Number of Contracts";IF(A3:A="",,IFERROR(VLOOKUP(A3:A,QUERY(UNIQUE({data!D2:E,data!B2:B}),"Select Col1, COUNT(Col3) Where Col2 = '"&A1&"' Group By Col1"),2,FALSE)))})

The A2 formula creates the header "Names" followed by an alphabetized list of all names from the "data" sheet.

The formulas in B2 and C2 are the same in structure, differing only in the header names and what is being searched for (i.e., "Direct" or "Consult"). Every non-blank entry in A2:A (i.e., each name) is run through a VLOOKUP of a QUERY made up of only names, amounts and services from "data" where the service type contains "Direct" (or "Consult" for the C2 version) and the customer matches whatever is chosen in cell A1. Since the "Group By" is based on name only, there will only be one matching result (or none) per person.

The D2 formula creates the header followed by a similar VLOOKUP of a QUERY. This QUERY finds only UNIQUE sets of data!D2:E followed by the names. Again, the elements of the QUERY have a "Group By" based on the name, so there will be one COUNT (or none) of unique entries per name for the VLOOKUP to find.


"Erik Help 2" combines all of the above formulas into one mega-formula, in A2, if you prefer that. This one formula runs everything in A2:D:

=ArrayFormula({"Name","Direct","Consult","Number of Contracts";SORT(UNIQUE(FILTER(data!D2:D,data!E2:E=A1))),IFERROR(VLOOKUP(SORT(UNIQUE(FILTER(data!D2:D,data!E2:E=A1))),QUERY({data!C:F},"Select Col2, SUM(Col4) Where Col1 Contains 'Direct' AND Col3 = '"&A1&"' Group By Col2"),2,FALSE)),IFERROR(VLOOKUP(SORT(UNIQUE(FILTER(data!D2:D,data!E2:E=A1))),QUERY({data!C:F},"Select Col2, SUM(Col4) Where Col1 Contains 'Consult' AND Col3 = '"&A1&"' Group By Col2"),2,FALSE)),IFERROR(VLOOKUP(SORT(UNIQUE(FILTER(data!D2:D,data!E2:E=A1))),QUERY(UNIQUE({data!D2:E,data!B2:B}),"Select Col1, COUNT(Col3) Where Col2 = '"&A1&"' Group By Col1"),2,FALSE))})

However, you may find the separate formulas easier to understand and edit if the need arises.