Google Sheet Formula to sum based on conditions - google-sheets-query

The following formula is designed to sum column C when column B contains a specific string:
=sum(query(A:C, " select C where B contains '2018-09-10' "))
https://i.stack.imgur.com/zjiJS.png
It looks straightforward but it doesn't work in the Sheet:
https://docs.google.com/spreadsheets/d/1qpQiwxHOUzoMTqHnn-m-nsnvl63YJkVIGgqpXzOZgzU/edit?usp=sharing
Am I missing something? Others have posted similar threads but this problem looks specific to this particular use case.

months late but saw this searching for a similar problem, the problem comes from that what sheets stores with a date is different than what you see if you change it to:
=sum(query(A:C, " select C where B = date '2018-09-10' "))
it should work, alternatively, you can just select the column and choose Format -> Number -> Plain Text and your formula will work but then you won't be able to treat it as a date elsewhere in the sheet

By Cell Reference:
=sum(query(A:C, " select C where B = date '"&TEXT(B3,"yyyy-mm-dd")&"'",1))

Related

Countif Formula to exclude Duplicates

I sought help regarding this once, but I failed to outline my problem.
This time I am happy to share the sheet with dummy data in hope it explains my problem a bit better: Link to the sheet
My issue is the following:
In column E I am counting the number of opportunities for a rep (listed in column A). The data I am considering is in a separate sheet named "Pipeline".
I do this with the countif formula and I use additional criteria to filter on date as well. My dates for february are in B4 and G4, because I only want to see opportunities in February.
My formula looks like this:
=countIFS(Pipeline!$A:$A,$A7,Pipeline!$F:$F,">="&$B$4,Pipeline!$F:$F,"<="&$G$4)
This works perfectly fine. However, sometimes I have two opportunities in my pipeline sheet with the same name (these are split opportunities). If an opportunity has the same name it should be counted only once. I can't seem to find an easy way to update my countif formula.
In the dummy sheet I shared above, you can see that Peter has two "New - CC Tech" opportunities. I want this to count as one opportunity. Everything I googled so far suggests using rather complex formulas, which is not so easy as I have multiple criteria in the formula that I need to filter my results (such as name of the rep and dates). Please feel free to suggest a solution within the sheet above and play around with it.
I really appreciate the help!
Try this ('unique' based on A,B and F)
=query(unique({Pipeline!A:B,Pipeline!F:F}),"select count(Col1) where Col1='"&A7&"' and Col3>=DATE'"&TEXT($B$4,"yyyy-MM-dd")&"' and Col3<=DATE'"&TEXT($G$4,"yyyy-MM-dd")&"' label count(Col1) '' ")
or, if you consider that the date could be different between two lines ('unique' based only on A and B, the date could be different but within the limits)
=query(unique({Pipeline!A$2:B,arrayformula((Pipeline!F$2:F>=$B$4)*(Pipeline!F$2:F<=$G$4))}),"select count(Col1) where Col1='"&A7&"' and Col3>0 label count(Col1) '' ")
In this second formula, we construct a matrix with A, B and 0/1 (which is the result of the question: is F within limits), then we apply unique and we query when Col3 is equal to 1 and Col1 the name we are looking for

Matches where an occurrence of X is Y but the corresponding Z is missing

I am trying to create a formula (or script) that is able to find all the occurrences of a plate number and return 1 if 'BOUGHT' has been found, but 'SOLD' is missing for that plate number.
The dataset that looks like this:
Column A is a list of plate numbers and B is the type ('BOUGHT' or 'SOLD').
I need to find the plate numbers for which there is a 'BOUGHT' but no 'SOLD'.
I tried using the formula below (and many others):
=ARRAYFORMULA(IFERROR(IF(
((VLOOKUP(A2:A,A:B,2,0)="VÉTEL")
+
(VLOOKUP(A2:A,A:B,2,0)="ELADÁS"))<>2
,1,0)))
Since nothing seems to be working, any help would be highly appreciated.
I created a sample worksheet, which can be found here.
Try below formula-
=ArrayFormula(IF(COUNTIFS(A2:A,A2:A,B2:B,"BOUGHT")>0,IF(COUNTIFS(A2:A,A2:A,B2:B,"BOUGHT")*(COUNTIFS(A2:A,A2:A,B2:B,"SOLD"))=0,1,""),""))
See your sheet.

Is there a way in google sheets to select a list of cells as an array to be used as a parameter in a connected sheet?

I have a new google sheet set up to query my database via a connected sheet.
The query returns a list of our shops and their sales per year. Each shop has an ID.
I am able to set Cell A1 in another, reference sheet, to be a parameter in the query. This way the connected query only returns results for that particular store ID.
When using this, I really want to put an IN function into my query. The connected query would then look something like.
SELECT * FROM shops where shops.id in (#RANGE)
And #RANGE would be A2:A as an array.
I've had success naming each cell as a new parameter and then:
SELECT * FROM shops where shops.id in (#REFERENCE1, #REFERENCE2)
Is there a more elegant solution?
Maybe a little late, but the easiest way I found was to convert to regex.
select (#POSTCODES) as test, postcode
from `postcode.au_towns`
where regexp_contains(#postcodes,safe_cast(postcode as string))
Where #POSTCODES is a gsheet string using a formula like join("|",UNIQUE(Sheet1!D2:D)).
Just make sure to remove the extra "|" generated using something like
left(B2,len(B2)-1)
This might work for you.
=SUBSTITUTE(QUERY(FILTER(D3:D,D3:D<>"",E3:E),"WHERE Col1 <> ''",9^99)," ","|")
This filters a column of store IDs based on which ones have been selected, and coverts that into a text string similar to the query you have been using. Producing something like "A1|A3|A7".
The query then just points to that result for the contains criteria.
Note that if your range of store IDs to report on is built in some other fashion, you just need to point to its range, instead of using the filter I have.
See a sample sheet here. This also shows a merged example of the two formulas, to produce the report all from one formula.
https://docs.google.com/spreadsheets/d/11uMa7CNcTXBnnpWGSIC_WvGSa-P2GTLQ2T7GvTgY4oM/edit?usp=sharing
Let us know if this helps you.
=QUERY(IMPORTRANGE("Google_Sheet_ID_Can_Be_Find_In_URL", "Sheet_Name!Range(you want to query)"),"SELECT * ")
or
=QUERY(IMPORTRANGE("Google_Sheet_ID_Can_Be_Find_In_URL", "Sheet_Name!A2:A"),"SELECT * ")
or
=QUERY(IMPORTRANGE("Google_Sheet_ID_Can_Be_Find_In_URL", "Sheet_Name!Range"),"SELECT * WHERE Col2='shops.id'")")
IMPORTRANGE() method import data from another worksheet. In the parameter, you type google sheet id from the url with quotes, type the desired sheet name end with ! Then you type the range from that sheet you want to query. When you wrap it with the outer QUERY() method, you can query the data from that range such as A2:A by selecting specific columns including the column with the range or * from that sheet name
When you're using IMPORTRANGE() method, it's going to return an array. The selected columns have to label in numeric like "SELECT Col 1, Col 2, Col 3"

Format Excel, column to get sum of hh:mm:ss when they are listed that way

I need to get the total duration time in hours:minutes:seconds when the list of time is all in one column. The seconds need to convert into minutes and seconds when they exceed 60 and the minutes into minutes and hours. I am getting the original data from a CVS format so it must all go into one column. I need to use a formula that will give me the sum for each person's individual times.
For example: a column like 01:22:03 added to 00:58:57 must come out to 02:21:00. I have tried putting the format into custom time h:mm:ss but it does not work. I have far too many to add on a weekly basis to do it by hand and I know someone has an answer for this new Excel user.
use the timevalue() function to convert a string of hh:mm:ss into a serial number that can be summed and displayed as a hh:mm:ss number.
Add all the cells with AutoSum.
All you need do is apply an appropriate format to its result:
Select the cell containing the total
From the Format menu, choose the Cells command
From the Category list, select Custom
In the Type box, input the format [h]:mm
(note the use of square brackets).
If you want to show seconds in your total, input [h]:mm:ss
(this format is listed in Excel's Custom formats)
Click OK.
all the best
If they are all stored as actual times, you should just be able to use a custom format of [hh]:mm:ss (this allows the hour total to go above 24.
If this isn't working then it is likely they are likely stored as text in which case you will need to split them and recombine them either through formulas or using text to columns.
If it is stored as a proper date =isnumber(your cell) should return TRUE, if not it is text.
If you are copying and pasting the data in make sure the cells aren't formatted as text before you do as it could cause issues.
If your format is always with this structure (00:00:00) you can use:
01:22:03(A1) 00:58:57(B1) 0 (C1) 2 (D1) 21(E1) 0 (F1) 02:21:00(G1)
05:22:04(A2) 00:50:40(B2) 0 (C2) 6 (D2) 12(E2) 44(F2) 06:12:44(G2)
11:22:05(A3) 00:08:59(B3) 0 (C3) 11(D3) 31(E3) 4(F3) 11:31:04(G3)
D = horas; E = minutes; F = Seconds
Formulas used:
C1 =(NSHORA(EXTRAE(A2;1;2);EXTRAE(A2;4;2);EXTRAE(A2;7;2)))+NSHORA(EXTRAE(B2;1;2);EXTRAE(B2;4;2);EXTRAE(B2;7;2))
d1 =HORA(C1)
e1 =MINUTO(C1)
f1 =SEGUNDO(C1)
We could finally get in a different column the result with your format if necessary with this one:
=CONCATENAR(SI(LARGO(D2)<2;CONCATENAR(0;D2);D2);":";SI(LARGO(E2)<2;CONCATENAR(0;E2);E2);":";SI(LARGO(F2)<2;CONCATENAR(0;F2);F2))
Select range C1:G1 and copy it to get what you want. After it you could also hide columns c, d, e and f
Since I'm programmer, I use VBA for everything :)
here is little VBA (Macro) for that.
There isn't test for input format so code will break if column do not contains fields like 01:12:13
'' SUM time in Column
'' Choose Column,StartRow,EndRow
'' result will be placed at Column:EndRow + 1
'' Column=1 1=A,2=B, ...
Dim w As Worksheet
Set w = ActiveWorkbook.Worksheets(1) ''choose first worksheet
Dim d As Double
Dim Column, StartRow, EndRow As Integer
Column = 1 ''this is A Column
StartRow = 1
EndRow = 21
For i = StartRow To EndRow
d = d + CDate(w.Cells(i, Column)) * 24 * 60 * 60
Next
''convert seconds to days
d = d / 86400.0
''display result in EndRow+1
w.Cells(EndRow + 1, Column) = Format(d, "hh:mm:ss")
Use the sum product function. I am no wiz I just googled the crap out of it. What happens is that the duration is converted into text so you have to convert the text to value. So for eg
=sumproduct((a2:a50)*1) then to get the duration you have to format the cells to duration and it'll get the number.
This is the site that helped me to figure it out
https://a4accounting.com.au/how-to-sum-text-numbers-in-excel/

including a conditional statement in a =QUERY function

How can I add a conditional statement (similar to an 'if elseif' in .php) to the query listed below so that it will return columns D and E from Sheet2 if column C in the responses sheet == 1st, or columns F and G from Sheet2 if column C in the responses sheet == 2nd? It must still only return column data from the appropriate row (based on the query).
=QUERY(Sheet2!$A$1:K; CONCATENATE("SELECT B, C WHERE A = ", responses!B1), 0)
I looked into IF(OR), but I don't believe that will work because final project will have more than two possibilities (there will actually be eight, 1st through 8th class period).
Here is a link to the Google Spreadsheet I'm working with
Any suggestions?
Thank you for your time and assistance,
Todd
High School Teacher
That cant be done with query. All rows must select the same columns.
Use a formula post-query (like an arrayformula)