Index-Match with Sum-Function - google-sheets

I have two tabs Product-Overview and Prices.
On the Prices-tab I would like to add my data and get an overview on the financials tab.
I was trying to use index-match, however if there are several values in the same month in my Prices-tab I am not sure how to sum the up?
I would like to get the following result:
Find below my minimum viable example as google sheet:
Google Sheet
Any suggestions how to do this?

delete column D and use this in D2:
=ARRAYFORMULA(IF(A2:A="";;TEXT(B2:B; "mm/yy")))
then you can do this:
=QUERY(Prices!A2:D; "select A,sum(C) where A is not null group by A pivot D")
and more advanced in O5:
=ARRAYFORMULA(IF(O1:1="";;IF(B5:B="";;
IFNA(VLOOKUP(B5:B; QUERY(Prices!A2:D;
"select A,sum(C) where A is not null group by A pivot D");
MATCH(TO_TEXT(O1:T1); QUERY(QUERY(Prices!A2:D;
"select sum(C) where A is not null group by A pivot D");
"limit 0"; 1); 0)+1; 0))*1)))
of course B5 can be:
=SORT(UNIQUE(Prices!A2:A))
then D5 can be:
=ARRAYFORMULA(IF(B5:B="";;"Product "&REGEXEXTRACT(TO_TEXT(B5:B); ".(\d+)")))
average N5 can be done like:
=ARRAYFORMULA(QUERY(TRANSPOSE(QUERY(TRANSPOSE(O5:100);
"select "&TEXTJOIN(","; 1; IF(B5:B="";;
"avg(Col"&ROW(O5:O)-ROW(O5)+1&")"))&""));
"select Col2"))
generating dates in O1 like:
=ARRAYFORMULA(ARRAY_CONSTRAIN(TRANSPOSE(UNIQUE(TEXT(ROW(INDIRECT(
DATEVALUE("1/11/2020")&":"&
DATEVALUE("1/11/2050"))); "mm/yy"))); 1; COLUMNS(O1:1)-1))
and years in O2 like:
=ARRAYFORMULA(IFNA(20&REGEXEXTRACT(O1:1; "/(\d+)")))

What you are looking for is the sumifs formula:
This will solve your problem, it is not an arrayformula, but it works if you drag it for the cells you want them to go.
=sumifs(
Prices!$C$2:$C;
Prices!$A$2:$A;$B5;
Prices!$D$2:$D;">="&if(isblank(N$1);date(year(O$1);month(O$1)-1;day(O$1));N$1);
Prices!$D$2:$D;"<="&if(isblank(O$1);date(year(N$1);month(N$1)+1;day(N$1));O$1)
)

Related

Using Query to replace PIVOT

Since Query is such a powerful formula, i try to avoid my team members to avoid messing up the pivot. I try to to come out with query to replace my pivot. Based on the raw data Google Sheet Trix, i try to sum column based on the criteria i have described in Grey colour (B2:E2). Appreciate if someone could lay some foundation for me, so i can further tweaks based on the formula you create here.
I created a new tab and entered the following in cell A1:
=query(
{'RAW DATA'!B:B,'RAW DATA'!E:E,'RAW DATA'!G:J},
"select Col1, sum(Col3), sum(Col4), sum(Col5), sum(Col6)
where Col1 is not null and Col2<>'Black and Yellow'
group by Col1"
)
Use a query() with a group by clause, like this:
=query(
'RAW DATA'!B1:J,
"select B, sum(G), sum(H), sum(I), sum(J)
where B is not null and E <> 'Black' and E <> 'Yellow'
group by B",
1
)
The result table will only include weeks where there is at least one data row where E is not Black or Yellow.
See the new Solution sheet in your sample spreadsheet.
Here is your solution. which also included in new tab named Solution Pivot in Query.
=ArrayFormula(QUERY(SPLIT(FLATTEN(TO_TEXT('RAW DATA'!B2:B)&"|"&'RAW DATA'!E2:E&"|"&'RAW DATA'!G1:J1&"|"&'RAW DATA'!G2:J),"|"),"Select Col1, Sum(Col4) where Col2 is not null And Col2 <> 'Yellow' And Col2 <> 'Black' Group by Col1 Pivot Col3 label Col1 'Date'"))

Need to know with formula in Google sheet

I have date in column A, Name in column B and Product sale data in column C. Now I want formula which gives me in return in another table, If that person has sold something on a particular date that sale data entry is shown
Do it with a pivot table so that you will have the answer without any formula.
Try query() with a pivot clause, like this:
=query(A1:C, "select A, B, count(B) where A is not null group by A, B pivot C", 1)
May be it can help too
=Query('Sales '!A1:C, "Select Col1, Col2, Col3 Where Col3 is not null",0)
'Sales ' is data source worksheet
use ,1 if you don't want to include the same headers of of your data source worksheet

Marrying Query and Arrayformula through cell reference in Google Sheets

Trying to understand if it is possible to apply ARRAYFORMULA to situations when QUERY is used in Google Sheets.
For example, I used QUERY for querying and aggregating a set of items, like so:
=QUERY($H$2:$I$17,"select sum(I) where H='"&A2&"' label sum(I) ''",0)
But in order to make that work across the spreadsheet, I will have to drag this formula down. There is also the ARRAYFORMULA thing, which is supposed to help with getting rid of excessive dragging, however it does not seem to work with QUERY, or am I just missing something?
A picture for a quick look:
And a shared file for the longer thinking:
https://docs.google.com/spreadsheets/d/1xOdqeESrFbrBknNYahSeF0ripA5fr2vVFQ-r--lkdA0/edit?usp=sharing
use this formula:
=QUERY(H2:I17, "select H,sum(I) where H is not null group by H label sum(I)''", 0)
and then you can do simple VLOOKUP like:
=ARRAYFORMULA(IFNA(VLOOKUP(A2:A, QUERY(H2:I17,
"select H,sum(I) where H is not null group by H label sum(I)''", 0), 2, 0)))
Here two method alternatively:
first ==>
=arrayformula(sumif(H2:H,"=" & unique(filter(H2:H,H2:H<>"")),I2:I))
second ==>
=arrayformula(
query(
filter({vlookup(H2:H,{A2:A,row(A2:A)},2,false),H2:I},H2:H<>"")
,"Select sum(Col3) group by Col1 label Sum(Col3) ''"
)
)

Translating each result row of a Google Sheets QUERY operation via VLOOKUP

I have the multi-column results of a QUERY in Google Sheets, where I want to translate the strings in one column based on a lookup table in another named range. I can accomplish this indirectly with an additional VLOOKUP call, but I'd like to do it all in one go, with no intermediary steps. See this sheet for an example of my format.
The initial query I make looks something like this:
=QUERY(votes, "SELECT B, SUM(C) GROUP BY B LABEL B 'Option', SUM(C) 'Votes'")
I can then translate each row with something like this in a new column...
=VLOOKUP(A2, options, 2, 0)
... and then just pick out the columns I need:
={C2:C4,B2:B4}
How can I combine all this? I think I need to do this with an ARRAYFORMULA, but this doesn't seem to be it:
=ARRAYFORMULA(VLOOKUP(options, QUERY(votes, "SELECT B, SUM(C) GROUP BY B LABEL B 'Option', SUM(C) 'Votes'"), 2, FALSE))
=ARRAYFORMULA({VLOOKUP(INDEX(
QUERY(votes, "select B,sum(C) group by B LABEL sum(C)''"),,1), options, 2, 0),
QUERY(votes, "select sum(C) group by B LABEL sum(C)''")})
=ARRAYFORMULA(QUERY({IFERROR(VLOOKUP(Votes!B2:B, options, 2, 0)), Votes!C2:C},
"select Col1,sum(Col2) where Col1 is not null group by Col1 label sum(Col2)''", 0))

sum same condition in multiple columns to get a total

I've been trying to sum the same condition in different columns (and different sheets) to get a total.
For example, I have a list of items in 3 different columns with 3 different sums.
See here:
I solved this with an SQL query within Google sheets but I'm looking for a more simple/ elegant solution.
This is what I have:
=UNIQUE({QUERY(A2:I8; "SELECT SUM(C) where B LIKE 'bananas'");
QUERY(A2:I8; "SELECT SUM(F) where E LIKE 'bananas'");
QUERY(A2:I8; "SELECT SUM(I) where H LIKE 'bananas'")})
And the result looks like this:
Does someone know a shorter version that fits in one cell?
=SUM(QUERY({A:C;D:F;G:I}; "where Col2 = 'bananas'"; 0))
=QUERY({A:C;D:F;G:I}; "select sum(Col3) where Col2 contains 'banana' label sum(Col3)''")
=SUM(FILTER({C:C;F:F;I:I}; {B:B;E:E;H:H}="bananas"))

Resources