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))
Related
I have been struggling with the Google Sheets query for several hours and maybe getting confused how to combine HLookup and VLookup (or any other function) in a way that can find the first and last occurrence of a value in a sheet based on the date header above it.
Here is an example sheet for reference which is very clear, but I will try explain verbally as well ... https://docs.google.com/spreadsheets/d/1rBVM7EtW3IREundWs_f2ftic-h4fEB97u4k4sZyIFNY/edit#gid=0
Given that I have a 2d range of cafeteria locations serving food on certain day (so the Y-axis headers of the table are cateteria locations and the X-axis headers are dates and the value is the name of the food served that day such as "Pizza") ... I want to have another table below that has a lookup for the first and last date that the food was offered. In my reference sheet I denoted that by Yellow highlight.
It seems like something that should be doable in a spreadsheet tool; unless it is impossible and I am not realizing it. Is such an operation possible?
delete range B10:C and use:
=INDEX(IFNA(VLOOKUP(A10:A, QUERY(SPLIT(FLATTEN(B1:E1&"×"&B2:E8), "×"),
"select Col2,min(Col1),max(Col1) group by Col2", ), {2,3}, 0)))
See if this helps
=query(ArrayFormula(split(flatten(text(B1:E1, "yyyy-mm-dd")&"~"&B2:E5), "~")), "Select Col2, min(Col1), max(Col1) where Col2 <> '' group by Col2 label Col2 'Food', min(Col1) 'First Offered', max(Col1) 'Last Offered' format min(Col1) 'yyyy-mm-dd', max(Col1) 'yyyy-mm-dd'", 0)
Change range to suit.
I have 2 forms and I have their results on the same sheet. I used the following formula
=Query({importrange(...);importrange(...)}, "SELECT * where Col1 is not null")
I want to have all the results from 1 form and the results depending on the date from the other form on the same sheet.
I have tried the following formula for July
=Query({importrange(...);importrange(...)}, "WHERE month(Col1)=5")
but it shows ONLY the results with July in the date from both forms.
How can I combine the results?
EDIT 2
(following OP's concern)
...Looks like it works only when there are entries with July in the date in the second spreadsheet. I need all the entries in the first spreadsheet to be visualized at any time
We can easily fix that by wrapping the whole formula with the IFERROR function
=IFERROR({QUERY(IMPORTRANGE("xxxxxx","form1!A1:E"), "where Col1 is not null");
QUERY(IMPORTRANGE("xxxxxx","form2!A1:E"), "where Col1 is not null and month(todate(Col1))=6",0)},
QUERY(IMPORTRANGE("xxxxxx","form1!A1:E"), "where Col1 is not null"))
Just make sure that Col1 (the Timestamp) in your results sheet is formatted as Date time
This final formula says:
Import data from the 1st sheet and under it append data from the 2nd sheet based on a certain month.
If that month is missing (which results to an error), import data from just the 1st sheet.
EDIT
(following OP's clarification)
...but my results are not on the same spreadsheets. I'd like to keep them on different spreadsheets.
Since there are different spreadsheets involved you must use the IMPORTRANGE function (as you very correctly already do).
Then the formula would be:
={QUERY(IMPORTRANGE("xxxxxx","form1!A1:E"), "where Col1 is not null");
QUERY(IMPORTRANGE("xxxxxx","form2!A1:E"), "where Col1 is not null and month(todate(Col1))=6",0)}
(As before please adjust ranges to your needs. Just make sure you keep the same number of columns)
NOTES:
1. Though July is the 7th month, we use month(todate(Col1))=6. That is because in "query language" January is month 0 and not 1.
2. In our second part we use as headers 0. This way we get to avoid returning them for the second query.
3. Since the imports come from forms we keep open ranges so when new answers are submitted, then get imported as well.
If all your sheets are in the same spreadsheet, you do not need IMPORTRANGE.
Using INDEX -which is much "lighter"- your formula would be:
={QUERY({form1!A2:O29}, "where Col1 is not null");
QUERY({form2!A2:O29}, "WHERE month(Col1)=6")}
Now your form2 results will be placed under the ones from form1
Having an additional query, you can have them both ordered by the timestamp in column 1
=QUERY({QUERY({form1!A2:O29}, "where Col1 is not null");
QUERY({form2!A2:O29}, "WHERE month(Col1)=7")},"order by Col1")
(Please adjust all sheet names and ranges to your needs)
I have a Google Sheet (Test Sheet 2) with two sheets in it, Sheet 1 and Sheet 2. Sheet 2 is where all of the data is and I need to get the sums of the counts of that column based on three criteria into sheet 1 column C. The name, week, year and count need to match up. I used the formula
=arrayformula(iferror(vlookup(A2:A&2020&B2:B, {Sheet2!A2:A&Sheet2!B2:B&Sheet2!C2:C, Sheet2!D2:D}, 2, FALSE)))
but that only works for unique rows. In the example sheet I am providing, the formula works well for Bill, Lisa, Katie and Jon because they all have one value for 'count' from Sheet2 when the parameters of name, week and year match up. But Mike has two rows matching the criteria. This formula returns the first match which is 3. The other value is 4 so I would like the count in Sheet1 to show 7 instead of 3. I need it to add them up.
I also tried to use sumifs but the columns are two different sizes so that didn't work.
Any idea? I did try to combine sumif with the above formula but that did not work either.
Link to Test Sheet
Solution with ARRAYFORMULA and SUMIF
If you need to use ARRAYFORMULA to improve the performances you can use this tweak of the SUMIF statement: basically you can concatenate the conditions to make them create a single AND condition.
This would be a possible solution for the formula in your comment:
=ARRAYFORMULA(SUMIF(Sheet2!A2:A&Sheet2!B2:B,A2:A&2020,Sheet2!D2:D))
Solution with SUMIFS
If you are looking for a solution with the SUMIFS formula you can use this:
SUMIFS('sum_range', 'criteria_range', condition, ['criteria_range_2', condition_2])
In your case this will translate to:
=SUMIFS(Sheet2!D2:D, Sheet2!A2:A, A2, Sheet2!B2:B, 2020)
In this case the ranges dimensions won't affect the formula execution.
Just drag this formula for the Sheet1 table column and you will get the results. The drawback is that you cannot use ARRAYFORMULA with SUMIFS. Performance wise, if you have a lot of rows in the Sheet1 I suggest using the ARRAYFORMULA solution, since this will trigger a lot of formula calls instead of just one.
Try this query()
=query(Sheet2!A:D, "Select A, C, sum(D) where B = 2020 group by A, C label C 'Week', sum(D) 'Count'", 1)
UPDATED:
If you really need to use vlookup in arrayformula() you can always ise the query (that deals with the summing) as the lookup range. In the spreadsheet I used
=ArrayFormula(if(len(A2:A), iferror(vlookup(A2:A&year(D2:D)&E2:E, query({Sheet2!A:A&Sheet2!B:B&Sheet2!C:C, Sheet2!D:D}, "Select Col1, sum(Col2) where Col1 <>'' group by Col1", 1), 2, 0)),) )
and see if that helps?
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'"))
I have a sheet with
Date|Amount Date|Amount Date|Amount ...
columns and from that I would like to get Month-Year:Total amount if it can be done with some Google spreadsheet functions/script.
I already have tried with many functions including ArrayFormula, Query, Text, etc. But They might require a range of data while I have broken range and didn't find any functions helpful.
This is how I would like to have:
Can it be done?
=ARRAYFORMULA(QUERY({TEXT({A:A;C:C;E:E;G:G}, "mmm - yyyy"), {B:B;D:D;F:F;H:H}},
"select Col1,sum(Col2) where Col2 is not null group by Col1 label sum(Col2)''", 0))