Need to know with formula in Google sheet - google-sheets

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

Related

How to join data from different sheets using Query in Google Sheets

I am trying to make a simple cash book.
As shown in the picture, from the cash-In and Cash-Out sheets i need to consolidate and display in the monitor sheet according to the selected party.
It should be sorted by date.
sheet link: https://docs.google.com/spreadsheets/d/1BwtcDIJv_CiZ-7Ae8qgLr4Azttnn7pBIOoxxMxdi2fk/edit?usp=sharing
Try this query-
=QUERY({QUERY('Cash In'!A2:C,"select A, C,0 where B='" & B1 & "'");
QUERY('Cash Out'!A2:C,"select A, 0, C where B='" & B1 & "'")},
"where Col1 is not null order by Col1 label Col1 'Date', Col2 'Cash In', Col3 'Cash Out'")
See sheet harun24hr.
This formula will solve your problem. First goto Monitor sheet then delete A3,B3 and C3 cells. Then put this formula on A3 cell.
=QUERY(UNIQUE({query('Cash In'!A1:C, "select A, C, 'Cash In' WHERE A is not null AND B ='"&$B$1&"'", 0);query('Cash Out'!A1:C, "select A, C, 'Cash Out' WHERE A is not null AND B ='"&$B$1&"'",0)}), "SELECT Col1, SUM(Col2) WHERE Col1 is not null group by Col1 PIVOT Col3 order by Col1 Label Col1 'Date'")
Get Cash In values query('Cash In'!A1:C, "select A, C, 'Cash In' WHERE A is not null AND B ='"&$B$1&"'", 0) and add a virtual column with Cash In with selected filter on B1 cell at Monitor sheet
Get Cash Out values query('Cash Out'!A1:C, "select A, C, 'Cash Out' WHERE A is not null AND B ='"&$B$1&"'",0)} and add a virtual column with Cash Out with selected filter on B1 cell at Monitor sheet
Then merge two data in a single result. (Vertically merge) UNIQUE({data_frame1, data_frame2})
In last step transpose PIVOT and sort by date column
Result:

Google Query SELECT statement concatenated with a NESTED IF result

Is it possible to return a Nested IF result from a CELL that will be concatenated to the SELECT statement in the QUERY function?
For example, I am trying to return the result for the following Nested IF function into the Query Function:
https://docs.google.com/spreadsheets/d/15i1E8AZHORRmPlu1VQqFRN1_7-aUyAz-hlYMOUtIlY4/edit?usp=sharing
Appreciate it, if anyone could take a look.
Regards
JVA
its done like this:
=QUERY(TESTDATA!A1:D16, "SELECT A, D, SUM(C) WHERE 1=1 "&
IF(AND(M3="NAME",N3="Customer"), " GROUP BY A, D PIVOT B",
IF(AND(N3 = "Customer"," AND A = '"&M3&"' GROUP BY A, D PIVOT B"),
" AND A = '"&M3&"' GROUP BY A, D PIVOT B",
" AND A = '"&M3&"'
AND D = '"&N3&"' GROUP BY A, D PIVOT B")), 1)
Sometimes, it's easier to FILTER the results before applying QUERY:
=ArrayFormula(QUERY(FILTER(A1:D16, A1:A16=M3, D1:D16=N3), "SELECT Col1, Col4, SUM(Col3) GROUP BY Col1, Col4 PIVOT Col2 LABEL Col1 'Name', Col4 'Customer'",0))
As you can see, this requires using Colx notation instead of letters to indicate columns in the SELECT clause; but this is actually (in my opinion) more versatile, since you don't have to rewrite the QUERY if you ever insert columns before the existing source data.
You'll also notice that I needed to LABEL the first two columns, since FILTER will have FILTERed out the headers. (In fact, for this reason, the ranges in the formula could just as easily have begun with row 2, e.g., A2:A16, etc.)
Finally, at least in your sample spreadsheet, you didn't need the sheet name to reference the source ranges, since the result is in the same sheet.

Combine first column of n sheets to one colume

So in one spreadsheet file we have n sheets.
Sheet1
Names
a
b
Sheet2
Names
b
c
d
and so on.. assume n sheets with n values in the first column
Then there's one super sheet.
Super sheet
Supersheet
Supersheet
Supersheet
Supersheet
Sheet1
Sheet2
Sheet3
...
Names
Count
a
1
b
2
c
1
d
1
Assume A1:1 is a list of all of the names of the sheets we want to fetch names from. We want the names pasted into A3:A.
My first thought was to do
=unique(filter({INDIRECT(A1&"!A3:A");INDIRECT(B1&"!A3:A");INDIRECT(C1&"!A3:A")},{INDIRECT(A1&"!A3:A");INDIRECT(B1&"!A3:A");INDIRECT(C1&"!A3:A")}<>""))
and expand, but that doesn't take care of N sheets.
So what I thought I could do was
=ARRAYFORMULA(INDIRECT(A2:C2&"!A2:A3"))
but that only takes care of sheet1. INDIRECT doesn't work with arrayformula
So what can I do?
you can either use some script or put your sheets in array {} and pre-program it:
=QUERY({INDIRECT(Sheet1!A1&"!A3:A");
INDIRECT(Sheet2!A1&"!A3:A")},
"select Col1,count(Col1)
where Col1 is not null
group by Col1
label count(Col1)''")
since this is one column future sheets can be added this way:
=QUERY({INDIRECT(Sheet1!A1&"!A3:A");
INDIRECT(Sheet2!A1&"!A3:A");
IFERROR(INDIRECT(Sheet3!A1&"!A3:A"));
IFERROR(INDIRECT(Sheet4!A1&"!A3:A"))},
"select Col1,count(Col1)
where Col1 is not null
group by Col1
label count(Col1)''")
IMPORTRANGE would be:
=QUERY({IMPORTRANGE("id", Sheet1!A1&"!A3:A");
IMPORTRANGE("id", Sheet2!A1&"!A3:A");
IFERROR(IMPORTRANGE("id", Sheet3!A1&"!A3:A"));
IFERROR(IMPORTRANGE("id", Sheet4!A1&"!A3:A"))},
"select Col1,count(Col1)
where Col1 is not null
group by Col1
label count(Col1)''")
ofc, every importrange formula needs to be authorized separately

Index-Match with Sum-Function

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)
)

Formula to fill a two dimensional grid with computed value from a table

Here is a problem to solve with a Google Sheets formula.
I have a big table (sheet “data” with headers on the first row) with those columns:
A, product reference
B: customer
C to F, KPI1 to KPI4
On another sheet, a grid of product references (A2:A) by customer (B1:1).
Now I need to fill each cell from the grid with the concatenation of KPIs (data!C24&"|"&data!D24&"|"&data!E24&"|"&data!F24)
Could you workout a single formula to fill all the cells?
Here is a sample spreadsheet with the data and grid sheet:
https://docs.google.com/spreadsheets/d/1iA_kw4kKw99Qk69X4tST9U-QN2SeG2EN3KEeyG6AtHs/edit?usp=sharing
I have worked out a formula which does the job, though with very poor performance on large dataset:
=ARRAYFORMULA(
IFNA(
VLOOKUP(
$B3:$B&"|"&C$2:$2,
ARRAYFORMULA(
{data!A2:A&"|"&data!B2:B,data!C2:C&"|"&data!D2:D&"|"&data!E2:E&"|"&data!F2:F}
),2,0
),""
)
)
Solution
Use an ArrayFormula on a Query with Pivot:
=ARRAYFORMULA(
QUERY(
{data!A2:A, data!B2:B, data!C2:C&"|"&data!D2:D&"|"&data!E2:E&"|"&data!F2:F},
"select Col1,max(Col3) where Col1 is not null group by Col1 pivot Col2",0
)
)
a shorter version of previous answer (no need for pre-sorting coz pivot will sort it on its own):
=ARRAYFORMULA(QUERY(
{data!A2:A, data!B2:B, data!C2:C&"|"&data!D2:D&"|"&data!E2:E&"|"&data!F2:F},
"select Col1,max(Col3) where Col1 is not null group by Col1 pivot Col2", 0))
Try this on the first cell of your grid:
=ArrayFormula(query(sort({data!A:A,data!B:B,transpose(substitute(query(transpose(data!C:F),,4)," ","|"))},1,true,2,true),"select Col1, max(Col3) where Col1 is not null group by Col1 pivot Col2",0))

Resources