Sum Values Meeting Criteria that Match Row Data and Dynamic Date Columns - google-sheets

I have scoured the forum and found nothing similar to my situation; would love some guidance.
I would like to sum values across multiple rows where the data match two criteria: row label reference and date headers from multiple tables in one sheet. Here is the data structure:
I would like to Add all values matching "Criteria #1" for each month so that I get:
I have tried sumif, sum(filter), various combinations of index/match, and to no avail. I can provide more info or context if needed to get your formula suggestions.

try:
=ARRAYFORMULA(QUERY(SPLIT(TRANSPOSE(SPLIT(QUERY(TRANSPOSE(QUERY(TRANSPOSE({
IF((B3:B5 ="criteria #1")*(ISNUMBER(C3:E5)), "♠"&B3:B5 &"♦"&C2:E2&"♦"&C3:E5 &"♦", );
IF((B8:B12="criteria #1")*(ISNUMBER(C8:E12)), "♠"&B8:B12&"♦"&C7:E7&"♦"&C8:E12&"♦", )})
,,9^9)),,9^9), "♠")), "♦"),
"select Col1,sum(Col3) group by Col1 pivot Col2 label Col1'label'"))

Related

Efficient way to collate/aggregate specific data in Google Sheets

I'm looking for an efficient way to gather and aggregate some date in Google Sheets. I've been looking at the query function, pivot tables, and Index + Match formulas, but so far I've not found a way that brings me to the result I'm looking for. I have a set of data which looks more or less as follows.
The fields with an X represent irrelevant data which I don't want to show up in my end result. They only serve to illustrate that there are columns of data that I don't want in between the columns of data that I do want. The data in those columns is of varying types and of varying values per type, they are not actually fields with an "X" in it. Only the fields with numbers are of interest along with the related names at the top and left of those. The intent is to create a list that looks more or less like this.
I've highlighted those yellow fields because that data has been aggregated. For example, in the original file field D3 shows a relation between Laura and Pete with the number 1, and field L3 also shows a relation between Laura and Pete, so the number in that field is to be added to the number in the other field resulting in an aggregated total of 2 for that particular combination.
I would really appreciate any suggestions that can help me get to an elegant and efficient solution for this. The only solutions I can come up with would involve multiple "in-between" sheets and there just has to be a better way.
UPDATE:
Solved by applying the solution in player0's answer. I just had to switch around the order of Col1 and Col2 in the formula to get the table sorted the way I needed it. Formula looks like below now. Many thanks to both player0 and Erik Tyler for their efforts.
=INDEX(QUERY(SPLIT(FLATTEN(A2:A&"×"&D1:N1&"×"&D2:N), "×"),
"select Col2,Col1,sum(Col3)
where Col2 is not null
and Col3 is not null
group by Col2,Col1
label sum(Col3)''", ))
try:
=INDEX(QUERY(SPLIT(FLATTEN(A2:A&"×"&D1:N1&"×"&D2:N), "×"),
"where Col3 is not null and Col2 is not null", ))
update:
=INDEX(QUERY(SPLIT(FLATTEN(A2:A&"×"&D1:N1&"×"&D2:N), "×"),
"select Col1,Col2,sum(Col3)
where Col3 is not null
and Col2 is not null
group by Col1,Col2
label sum(Col3)''", ))
Given your current data set (which only appears to extend to Col N), place the following somewhere to the right of Col N:
=ArrayFormula(SPLIT(TRANSPOSE(QUERY(TRANSPOSE(QUERY(SPLIT(QUERY(FLATTEN(FILTER(IF(NOT(ISNUMBER(D2:N)),,D1:N1&"~ "&A2:A&"|"&D2:N),A2:A<>"")),"Select * WHERE Col1 Is Not Null"),"|"),"Select Col1, SUM(Col2) GROUP BY Col1 LABEL SUM(Col2) ''")&"~ "),,2)),"~ ",0,1))
It would be better if this were placed in a different sheet from the original data. Supposing that your original data sheet is named Sheet1, place the following version of the above formula into a new sheet:
=ArrayFormula(SPLIT(TRANSPOSE(QUERY(TRANSPOSE(QUERY(SPLIT(QUERY(FLATTEN(FILTER(IF(NOT(ISNUMBER(INDIRECT("Sheet1!D2:"&ROWS(Sheet1!A:A)))),,Sheet1!D1:1&"~ "&Sheet1!A2:A&"|"&INDIRECT("Sheet1!D2:"&ROWS(Sheet1!A2:A))),Sheet1!A2:A<>"")),"Select * WHERE Col1 Is Not Null"),"|"),"Select Col1, SUM(Col2) GROUP BY Col1 LABEL SUM(Col2) ''")&"~ "),,2)),"~ ",0,1))
This separate-sheet approach and formula allows for the original data to extend indefinitely past Col N.

Google sheets - getting percentages of yes, no, maybe responses into a pivot table

I have some data. One of the columns (Column D) has data such as "yes", "no", 'Credit", "n/a", "half".
At the top of that column, I have created a function which allows me to get percentage of "yes" responses, but not as a percentage of all responses, but just as a percentage of the responses that are "Yes", "No" and "Half"
Formula below
=iferror((COUNTIF(C3:C, "yes") / (COUNTIF(C3:C, "no") + COUNTIF(C3:C, "half") + COUNTIF(C3:C, "yes"))))
Below that, starting at row 3, is the data.
What I want to do is put data this into a pivot table. One of my other Columns is date, and I want to be able to group the data by month (actually by week, but I have already worked that out).
I am looking for my grand total value to be summarised using the formula above, which doesn't exist as one of the standard summary options.
I am interested to hear your ideas on the best way to solve this.
Thanks
Edit: here is a link to a sheet with the basics of what I am trying to do. Thanks for your help with this.
https://docs.google.com/spreadsheets/d/1ew4pPv4qtJCZJCaUgg_I7VdL5F00t-PFhIlg2bs8sh0/edit?usp=sharing
One "extended" solution is
get all yes per month in L3
=query({arrayformula(EOMONTH(B$3:B,0)),C3:C},"select Col1, count(Col2) where Col2='yes' group by Col1 label count(Col2) 'Exercice-yes' ")
get all yen/no/half per month in N3
=query({arrayformula(EOMONTH(B$3:B,0)),C3:C},"select Col1, count(Col2) where Col2='yes' or Col2='no' or Col2='half' group by Col1 label count(Col2) 'Exercice-yes|no|half' ")
get all unique monthes in G3
=arrayformula(unique(if(B3:B="","",eomonth(B3:B,0))))
and finally find the %
=arrayformula(iferror(VLOOKUP(G3:G,L:M,2,0)/VLOOKUP(G3:G,N:O,2,0)))

How can I separate a column into multiple columns based on values?

I have searched on a lot of pages but I cannot find a solution to my problem except in reverse order. I have simplified what I do, but I have a query that comes looking for information in my data sheet. Here there are 3 columns, the date, the amount and the source.
I would like, with a query function, to be able to make different columns which counts the information of column C based on the values of its cells per month, like this
I'm okay with the start of the formula
=QUERY(A2:C,"select month(A)+1, sum(B), count(C) where A is not null group by month(A)+1")
But as soon as I try a little different things by putting 2 query together in an arrayformula, obviously the row count doesn't match as some minus are 0 for some sources.
Do you have a solution for what I'm trying to do? Thank you in advance :)
Solution:
It's not possible in Google Query Language to have a single query statement that has one result grouped by one column and another result grouped by another.
The first two columns can be like this:
=QUERY(A2:C,"select month(A)+1, sum(B) where A is not null group by month(A)+1 label month(A)+1 'Month', sum(B) 'Amount'")
To create the column labels for the succeeding columns, use in the first row, in my example, I1:
=TRANSPOSE(UNIQUE(C2:C))
Then from cell I2, enter this:
=COUNTIFS(arrayformula(month($A$2:$A)),$G2,$C$2:$C,I$1)
Then drag horizontally and vertically to apply to the entire table.
Results:
try:
=INDEX({
QUERY({MONTH(A2:A), B2:C},
"select Col1,sum(Col2) where Col2 is not null group by Col1 label Col1'month',sum(Col2)'amount'"),
QUERY({MONTH(A2:A), B2:C, C2:C},
"select count(Col3) where Col2 is not null group by Col1 pivot Col4")})

Sum based on multiple row + header criteria

I have a problem with summing up my values from a data set, that's structured like this:
The goal is to sum the revenues separated by company and split by month, so the result is output in this way
I have tried it with some =sumifs + index/match and =sumproduct solutions, but can't seem to make it work.
Here's the sample file:
https://docs.google.com/spreadsheets/d/16xOoPCHDtcSRRojCkwcBorUc5dstgkXFPR6M_d5uY2U/edit#gid=0
On the "revenues" tab, in cell B4, try using the formula:
=SUMIFS(indirect(address(1,match(A4,Overview!$3:$3,0)-1,,,"Overview")&":"&address(1000,match(A4,Overview!$3:$3,0)-1)),Overview!A1:A1000,">="&B$2,Overview!A1:A1000,"<="&B$3)
To break it down, this bit helps figure out which revenue column to use by matching the name of the company and then taking the column before that:
match(A4,Overview!$3:$3,0)-1
This bit creates an address "Overview!$G$1":
address(1,match(A4,Overview!$3:$3,0)-1,,,"Overview")
This bit creates the 2nd part of the address i.e.":$G$1000":
"&":"&address(1000,match(A4,Overview!$3:$3,0)-1)
And the rest is a SUMIFS where it sums the revenue column for dates after the 1st of the month and before the last date of the month.
Be careful: your data is for 2020 and your summary table is using dates in 2021.
Reference:
SUMIFS
ADDRESS
MATCH
INDIRECT
use in A4:
=ARRAYFORMULA(QUERY(QUERY({SPLIT(FLATTEN(IF(
FILTER(Overview!G7:1000, MOD(COLUMN(Overview!G7:1000)+2, 3)=0)="",,
TEXT(Overview!A7:A25, "m")&"×"&
FILTER(Overview!G7:1000, MOD(COLUMN(Overview!G7:1000)+2, 3)=0)&"×"&
FILTER(Overview!G3:3, MOD(COLUMN(Overview!G3:3)+1, 3)=0))), "×");
SEQUENCE(12), SEQUENCE(12, 2,,)},
"select Col3,sum(Col2)
where Col1 is not null
group by Col3
pivot Col1", 0),
"offset 2", 0))

Pivot table with double rows or going from wide to long in Google Sheets

How can I go from wide to long in Google Sheets based on two different columns or create a pivot table where I specify two different columns from the original matrix as rows?
Please see example for intended effect:
you can do it like this all in one go:
=ARRAYFORMULA(QUERY({
A1:B, TEXT(C1:C, "hh:mm");
A2:A, D2:D, TEXT(C2:C, "hh:mm")},
"select Col2,max(Col1)
where Col2 is not null
group by Col2
pivot Col3", 1))
Here's a fairly simple approach. I don't think you can use a pivot table because the values have to be summaries of a numeric value, or at least counts, not a string value.
To get the times:
=transpose(C2:C)
To get the lecturers (fairly big assumption that there are no lecturers that work only as assistants but this can be changed later):
=unique(B:B)
If there are additional lecturers working only as assistants:
=unique({B:B;D2:D})
Then to get the topic corresponding to a particular lecturer or assistant:
=ArrayFormula(IFERROR(vlookup(filter(F2:F,F2:F<>"")&filter(G1:1,G1:1<>""),{B2:B&C2:C,A2:A},2,false))&
IFERROR(vlookup(filter(F2:F,F2:F<>"")&filter(G1:1,G1:1<>""),{D2:D&C2:C,A2:A},2,false)))

Resources