Google sheet formula to find missing number based on matching criteria - google-sheets

With the data in the attached google sheet, Col F (IN/OUT) will have either IN or OUT and Col J (Bag Numbers) will have numbers seperated by Comma.
Based on the sample data, for Vendor 4-Sunil, Bag numbers 2,3,4,5 where sent OUT on 8/9/21. On 17/9/21, 2,3,4,5 where received IN. So there is nothing Pending with Vendor 4-Sunil.
On the other hand with Vendor 5-Rajan, Bag numbers 13,14,15,16 where sent OUT on 14/9/21. On 25/9/21 Bag Numbers 13,15 where received IN. So the pending bags yet to be received IN from Vendor 5-Rajan is 14,16.
Is there a way to build a formula in Google sheet to indicate the number of pending Bags based on IN or OUT criteria for a given Vendor?
Thank you.
Sample Google sheet
https://docs.google.com/spreadsheets/d/10XP3k4HLteGHgOEdd0YIZR-_0p2IiHog5R7luIM5NeM/edit?usp=sharing

To display Bag numbers out, try:
=arrayformula(query({
query({A:J},"select Col5 where Col5 !='' and Col6 contains 'OUT' order by Col5",1),
regexreplace(regexreplace(regexreplace(query({A:J},"select Col10 where Col5 !='' and Col6 contains 'OUT' order by Col5",1),iferror(vlookup(query({A:J},"select Col5 where Col5 !='' and Col6 contains 'OUT' order by Col5",1),{query({A:I,iferror(regexreplace(J:J,",","|"),J:J)},"select Col5, Col10 where Col5 !='' and Col6 contains 'IN' order by Col5",1)},2,0)),""),",+",","),"^,+|,+$",)
},"where Col2 != '' label Col2 'Remaining out' ",1))
If you want a more complex solution running from a single cell that handles multiple OUT and IN for the same Vendor, then try:
=arrayformula({"Vendor","Items still out";regexreplace(query({
query(regexreplace(split(flatten(regexreplace(trim(query(transpose(query(if(E2:E<>"",substitute({E2:E&char(9998)&F2:F&char(9998),J2:J,J2:J}," ",char(9999)),),"select Col1,Max(Col2) where Col1 !='' group by Col1 pivot Col3",)),"",9^9))," ",",")),char(9998)),"^,+|,+$",),"select Col1 where Col2 contains 'OUT' order by Col1",0),
iferror(regexreplace(regexreplace(query(regexreplace(split(flatten(regexreplace(trim(query(transpose(query(if(E2:E<>"",substitute({E2:E&char(9998)&F2:F&char(9998),J2:J,J2:J}," ",char(9999)),),"select Col1,Max(Col2) where Col1 !='' group by Col1 pivot Col3",)),"",9^9))," ",",")),char(9998)),"^,+|,+$",),"select Col3 where Col2 contains 'OUT' order by Col1",0),iferror(vlookup(
query(regexreplace(split(flatten(regexreplace(trim(query(transpose(query(if(E2:E<>"",substitute({E2:E&char(9998)&F2:F&char(9998),J2:J,J2:J}," ",char(9999)),),"select Col1,Max(Col2) where Col1 !='' group by Col1 pivot Col3",)),"",9^9))," ",",")),char(9998)),"^,+|,+$",),"select Col1 where Col2 contains 'OUT' order by Col1",0),
{regexreplace(query(regexreplace(split(flatten(regexreplace(trim(query(transpose(query(if(E2:E<>"",substitute({E2:E&char(9998)&F2:F&char(9998),J2:J,J2:J}," ",char(9999)),),"select Col1,Max(Col2) where Col1 !='' group by Col1 pivot Col3",)),"",9^9))," ",",")),char(9998)),"^,+|,+$",),"where Col2 contains 'IN' order by Col1",0),",","|")}
,3,0),),),"^,+|,+$",),)
},"where Col2 !='' ",0),char(9999)," ")})

Related

Query multiple columns of data in Google Sheets and count number occurrences

I am trying to query two columns of data in Google Sheets and count how many times the values occur.
I have made a Google Sheets formula which works but only returns data in the first column.
=QUERY(QUERY({D2:D,E2:E}, "select Col1, count(Col1) group by Col1"), "select Col1, Col2 order by Col2 desc, Col1 limit 45", 0)
How can I query and count data from both columns together?
DESIRED RESULT
2.38 / 2
5.38 / 2
...
I have made a Google Sheet showing an example. This can be copied by going to File-Make a Copy
https://docs.google.com/spreadsheets/d/1S5fE43JPVgUhZFVx-rjowkCy8N6p_ziIhIWe4MweiXQ/edit?usp=sharing
If your formula gives the correct output, you may need to change your comma with semicolon. That way all your data will be grabbed as a single column:
=QUERY(QUERY({D2:D;E2:E}, "select Col1, count(Col1) group by Col1"), "select Col1, Col2 order by Col2 desc, Col1 limit 45", 0)
Please try:
=QUERY({D2:D;E2:E},"Select Col1, COUNT(Col1) WHERE Col1 IS NOT NULL GROUP BY Col1 ORDER BY COUNT(Col1) DESC LABEL COUNT(Col1)''")

Query Formula using Google Sheets - count data from multiple columns and return results in a single column

I am trying to calculate a count of how many times a course is listed in different columns and return the count in a list of all the courses found in those columns.
I have a spreadsheet that shows the start of my formula...
query(UNIQUE({C2:C21; F2:F21;I2:I21}),"select C,F,I, count(C), count(F), count(I) where B is not null group by C,F,I")
Clear cells A23, A24 and B23 and then try in A23
=query({C2:C21; F2:F21;I2:I21},"select Col1, count(Col1) where Col1 <> '' group by Col1 label Col1 'Fall Courses', Count(Col1) 'Count'")
and see if that helps?
If it does, you can repeat the same logic for 'winter' and 'spring'.
Alternatively, you can also try this single formula
=query(ArrayFormula(split(transpose(split(textjoin("~", true, regexreplace(C1:K1, "(Winter|Spring|Fall)", "_$1")&"_"&C2:K21), "~")), "_")), "Select Col3, Count(Col3) where Col3 <>'' group by Col3 pivot Col2", 0)

is there any functions in google sheets to resolve the below problem?

I want to filter the value in F (F>100) and group values in F according to Values in E, find the total for the group and multiply it with value in B as per the group (ie, A,B,C etc.,) and the final totals of the multiplied values for all groups
sum(A)*20+sum(B)*30+sum(C)*15 and so on
enter image description here
See if this works
=index(query({E:F, IFERROR(F:F*VLOOKUP(E:E, A:B, 2, 0))}, "Select Col1, sum(Col3) where Col2 > 100 group by Col1 label sum(Col3)''", 0))
If Col A will always have A,B,C,D,E in that order:
=arrayformula(if(A:A<>"",{A:A,QUERY({E:F},"select sum(Col2) where Col2 >100 group by Col1 label sum(Col2) '' ",0)*B:B},))
If not:
=arrayformula(if(A:A<>"",QUERY({E:F},"select sum(Col2) where Col2 >100 group by Col1 label sum(Col2) '' ",0)*query({A:B},"select Col2 where Col1 is not null order by Col1",0),))

Arrange query output by sheet name

thanks in advance.
Is there a way to arrange my multiple sheets query output by sheets name? so there is a separation between the two when they stack on top of each other?
=QUERY({orange!A2:I24;'apple'!A2:I26},"select Col1,Col2 where Col1 !='' ",1)
cheers to all.
={query({orange!A2:I24},"select Col1,Col2, 'orange' where Col1 !='' ",1);
query({'apple'!A2:I26},"select Col1,Col2, 'apple' where Col1 !='' ",1)}
The only way is to hardcode sheet names. There's no formula to get sheet names automatically.
Unless you have some specific data marker to differentiate between sheets, I don't think it's possible. But you may well use two queries like,
=ARRAYFORMULA({QUERY({orange!A2:I24},"select Col1,Col2 where Col1 !='' ",1);QUERY({'apple'!A2:I26},"select Col1,Col2 where Col1 !='' ",1)})
If you have a specific data marker,
=QUERY({orange!A2:I24;'apple'!A2:I26},"select Col1,Col2 where Col1 !='' order by Col3",1)
Where Col3, i.e., C column should be say,1 in orange sheet and 2 in Apple sheet.

Google Sheets Query Language, return 0

I've been having troubling getting the query function to return 0 when the queried data isn't found.
Given the following data...
With each sheet being a different player, I need to count the number of games each played and the outcome of those games.
Some players may be in the same game as each other so the gameIDs are given as a unique identifier so those games are only counted once, that part I have working.
What I'm trying to do now is record the number of wins team A has in each room.
You can see my problem in the above screenshot.
Here is the sample sheet that has the above data.
This is the formula used for the right most example
=QUERY({QUERY(UNIQUE(QUERY({Sheet1!A2:C;Sheet2!A2:C;Sheet3!A2:C},"select Col1, Col2 where Col2 <> '' and Col3 contains 'A'")),"select Col1, 1");QUERY(QUERY({Sheet1!A2:C;Sheet2!A2:C;Sheet3!A2:C},"select Col1, Col2 where Col2 = '' and Col1 <> '' and Col3 contains 'A'"),"select Col1, 1")},"select Col1, sum(Col2) where Col1 <> '' group by Col1 label sum(Col2) 'TeamA Wins', Col1 'Room'")
Anyone able to help?
You may add dummy data into the formula:
=QUERY(data;{QUERY(UNIQUE(QUERY({Sheet1!A2:C;Sheet2!A2:C;Sheet3!A2:C},"select Col1, Col2 where Col2 <> '' and Col3 contains 'A'")),"select Col1, 1");QUERY(QUERY({Sheet1!A2:C;Sheet2!A2:C;Sheet3!A2:C},"select Col1, Col2 where Col2 = '' and Col1 <> '' and Col3 contains 'A'"),"select Col1, 1")},"select Col1, sum(Col2) where Col1 <> '' group by Col1 label sum(Col2) 'TeamA Wins', Col1 'Room'")
^^^^
data is a named range, it looks like this:
To make it:
paste the data to separate sheet, as shown in picture above.
select range A1:B6
go to Data → Named range → call it "data"
enter the formula and it should work fine now.
Sample file with working formula

Resources