Trying to count rows matching multiple criteria - google-sheets

Using Zapier, when someone fills out my Wufoo application form, a line is created with some of the information, including Date Created as well as the locations selected from a checkbox field. On my second sheet, I want to be able to have a total of how many applications have come in just today for each location. I figured the easiest way to do this would be a CountIFs function, but I'm probably wrong.
https://docs.google.com/spreadsheets/d/1mIw_O6KT2QCyKeKmZ4aJ1EHzOkZ4xj49ZyjEMOCwUxA/edit#gid=0
Here is a copy of what I'm building if you'd like to experiment. I'm using =countifs(Sheet1!E2:T1000,"Buford,GA (Atlanta)",Sheet1!E2:T1000,"="&TODAY()) to try to find this, but always get 0 as the result.

try this:
You needed to specify the columns, in your case: E2:E to look up for the name, and S2:S for the date.
=countifs(Sheet1!E2:E,"Buford, GA (Atlanta)",Sheet1!S2:S,"="&TODAY())
You can also use Partial Match in order to use your headers in Sheet2 as the first criteria in your countifs:
=countifs(Sheet1!E2:E,"*"&B$1&"*",Sheet1!S2:S,"="&TODAY())
Then you had a problem of formatting in Sheet1 within the Date's Column (S): your second date in cell S3 was formatted as Date Time, as oppose to your first date in S2 which was formatted as Date only.
Note: The above suggestions are to resolve your personal attempt, otherwise they are other ways of achieving what you're looking for.
UDPATE (ALTERNATIVE SOLUTION):
you could use the following QUERY which summarize everything within a table and sort it in descending order:
= {"Names","Count";
ARRAYFORMULA(
SORT(
TRANSPOSE(
SUBSTITUTE(
QUERY(Applications!D1:T,
"Select Count(D),Count(E), Count(F), Count(G), Count(H), Count(I), Count(J),
Count(K), Count(L), Count(M), Count(N), Count(O), Count(P), Count(Q), Count(R), Count(S) where T = date '"&TEXT(today(),"yyyy-mm-dd")&"'
"),
"count", "")
),
2, 0)
)
}

Related

Using `ARRAYFORMULA` with `FILTER` in Google Sheets

I have some entries against days of the year:
I want to state the average of these entries for every week (hence the week helper column in Column A).
Normally to do this I would use FILTER:
AVERAGE(FILTER(C:C, A:A = A1 ) ))
Where A1 is the week number against which I would like to place the average (assume that this is on another tab and correctly referencing the data).
This works for a single cell, and if I drag the formula down manually. However, I want it to update automatically, but I cannot use ARRAYFORMULA with FILTER. I am not sure of the solution: the other questions on SO can be solved more easily as they do not require multiple rows to be returned. I do, so I cannot use VLOOKUP, which would solve the problem.
in a new cell, on a brand new tab, try this assuming the tab name with the data is "Sheet1":
=QUERY('Sheet1'!A:C,"select A,AVG(C) where A<>'' group by A")
Here is a good resource on QUERY().

Multiple search criteria in google sheets

I am trying to make an automated attendance sheet
I have 2 google sheets,
the first one is the responses from a google form that has the name of the students and the date they attended, so it will have duplicated name and duplicated dates.
The second sheet have the names of the students on the left and the dates on the top.
I am trying to automate the second sheet to put "P" under the date that the student was present and "A" when his name is not in the first sheet with that date.
Best i could do was adding an extra column with the letter "p" in the first sheet and using dget to search for the name and date and output the "p" from the extra column, which only worked for one of them for some reason.
=DGET('ATTENDANCE DATE !B:D, "AT", {"NAME", "DATE"; $H$4,12})
I tried to use query also but no luck.
=QUERY('ATTENDANCE DATE'!B7:D,"
SELECT D
WHERE B MATCHES'"&$H9&"' AND C MATCHES '"&I$2&"'
")
Sorry if my question was confusing.
A good solution is to use 4 formulas to do exactly what you like. Each formula has a function:
B1 formula: generates the headers for all the dates with data.
B2 formula: generates the sub-header with the day of the week for each date.
A3 formula: gets all the names.
B3 formula: gets the attendance values for all users. This is the most complexs one.
Here is how it looks:
A
B
1
< fromula 1 >
2
name
< formula 2 >
3
< formula 3 >
< formula 4 >
Before starting there a few things to note
Questions and more information
Please, if at any point you don't understand something, let me know (I'd like this to be a nice resource on how to do formulas).
Also, at the end I left links to all the formulas I use, so you can see what they exactly do.
Locale
I'm using the English locale. this means that I'm using commas , to separate arguments (instead of ;) and array literals (instead of \). if you have function formatting errors, look into it, as this could be the issue.
Sheet names
I've changed the Sheet's names as they are very long and made the formulas harder to follow. Fell free to replace the names on the formulas back to the original name. Here is how I named them:
ATTENDANCE RESPONSES FROM GOOGLE FORM ⟶ Att
LATE/ABSENT RESPONSES FROM GOOGLE FORM ⟶ Late
Formula format
Almost all formulas require "ARRAYFORMULA" to show their full effects. I won't be adding it everywhere as it could get confusing. If you'd like to see what a formula part (doesn't have an equal sign =) does, go to a sheet and do:
=arrayformula(
<paste formula part>
)
Also, parts that are in <some name> are not literal, and represent the code named in between the brackets.
Formula 1
It can be split into 2 formulas:
Get the ordered unique dates
Add a Reason column for each date
Get the dates
The first thing you can use is UNIQUE to get only the unique ones and SORT to sort them. You also need to get them from both sheets as there could be a day that everyone is absent or another where everyone came. SORT(UNIQUE({Att!B2:B; Late!B2:B})) does most of the trick but you also get empty cells. because of that we add a filter. So together:
SORT(UNIQUE(FILTER({Att!B2:B; Late!B2:B}, {Att!B2:B; Late!B2:B}<>"")))
Adding Reason
The problem of it is that the number of column is not fixed (it grows over time). A good workaround is to concatenate the date with a separator and Reason and then split it again. This only works for columns and generates a 2 column, result. Then it can be moved into a single column by using FLATTEN.
FLATTEN(SPLIT(
<previous part>&"␟Reason",
"␟"
))
I'm using ␟ (Symbol For Unit Separator) as the separator as it indicates exactly what it is and is very-very unlikely to be included in the sheet.
If you use that you'll see that the date is shown in numbers. To Change that we'll format the date before concatenating and splitting:
FLATTEN(SPLIT(
TEXT(
<previous part>,
"dd/mmm/yyyy"
)&"␟Reason",
"␟"
))
Now we need to make it a row instead of a column. There is a function that does that: TRANSPOSE.
Complete formula 1
=ARRAYFORMULA(
TRANSPOSE(FLATTEN(SPLIT(
TEXT(
SORT(UNIQUE(FILTER({Att!B2:B; Late!B2:B}, {Att!B2:B; Late!B2:B}<>""))),
"dd/mmm/yyyy"
)&"␟Reason",
"␟"
)))
)
Formula 2
To add the day of the week we need to format the date with TEST and the format ddd, which is the short-version of the name of the day of the week (Mon, Tue, etc).
TEXT(B1:1, "ddd")
Note though that if the value formatted is already text, it will pass it. Because of that, we need to only do this for the columns with dates. One way that I found is to get the even-numbered columns. TO do that, it's a combination of COLUMN (get the number of the column), MOD (get the module), and IF:
IF(MOD(COLUMN(B1:1),2)=0, <formatted text>, "")
This does what we want but we now get "Sun" on columns that there is nothing. The reason is that empty cells are being interpreted as zeros. Because of that we need to add another condition: the cell is not empty.
IF((MOD(COLUMN(B1:1),2)=0)*(B1:1<>""), <formatted text>, "")
To do the logical and I'm using the product because the formula AND would return a single value ("eats" the passed array).
Complete
=ARRAYFORMULA(
IF(
(MOD(COLUMN(B1:1),2)=0)*(B1:1<>""),
TEXT(B1:1, "dddd"),
""
)
)
Formula 3
The third formula is the simplest and should be self-explanatory:
=ARRAYFORMULA(
SORT(UNIQUE(Att!A2:A))
)
Formula 4
This is the final formula. This formula is based on using VLOOKUP to know the value for each person and date.
Making a table to VLOOKUP into
The way of doing that is to generate a key by joining both values into a single text value and set the other values on the other columns. To prevent problems we add a separator to make sure that there are no combination that will be equal. Here is how the table to lookup into looks like:
< name >␟< date >
< status >
< reason >
< name >␟< date >
< status >
< reason >
< name >␟< date >
< status >
< reason >
⋮
⋮
⋮
The key for the first sheet is:
Att!$A$2:$A&"␟"&Att!$B$2:$B
To add the other 2 columns (Present and an empty one) we use a similar trick to Formula 1: we add a separator to split it later on. Because we already are using ␟ for the key, we need another one. In the same block there is another meant for this cases: ␞ (Symbol For Record Separator).
Att!$A$2:$A&"␟"&Att!$B$2:$B&"␞"&"Present"&"␞"&""
or joining the literal text:
Att!$A$2:$A&"␟"&Att!$B$2:$B&"␞Present␞"
This need to be filtered, as there are empty values, which we don't want. We'll use FILTER to do exactly that:
FILTER(Att!$A$2:$A&"␟"&Att!$B$2:$B&"␞Present␞", Att!$A$2:$A<>"", Att!$B$2:$B<>"")
For the second sheet, we do something similar but including the other columns:
FILTER(Late!$A$2:$A&"␟"&Late!$B$2:$B&"␞"&Late!$C$2:$C&"␞"&Late!$D$2:$D, Late!$A$2:$A<>"", Late!$B$2:$B<>"", Late!$C$2:$C<>"")
Note that I've added more conditionals.
This needs to be vertically joined. This can be done with an array literal:
{
<Attr formula>;
<Late formula>
}
Then we need to split the rows to expand into the multiple columns:
SPLIT(
{
FILTER(Att!$A$2:$A&"␟"&Att!$B$2:$B&"␞Present␞", Att!$A$2:$A<>"", Att!$B$2:$B<>"");
FILTER(Late!$A$2:$A&"␟"&Late!$B$2:$B&"␞"&Late!$C$2:$C&"␞"&Late!$D$2:$D, Late!$A$2:$A<>"", Late!$B$2:$B<>"", Late!$C$2:$C<>"")
},
"␞"
)
Using VLOOKUP
Now that we have where to lookup into, we can do it like so:
VLOOKUP(
A3:A&"␟"&C1:1,
<lookup table>,
2,
false
)
Note that the key that we are looking up is the one we generate. Also, this will get the values only below the dates (will fail otherwise).
Adding the reason
Since we know that the cells which are for the reason fail (since <name>␟Reason shouldn't exist), we can use IFERROR to detect it:
IFERROR(
<vlookup status>,
<vlookup reason>
)
The formula for reason is almost identical to the one for status. The only changes are that we lookup into the third column (instead of the second) and we look one to the left:
VLOOKUP(
A3:A&"␟"&OFFSET(C1:1, 0, -1),
<lookup table>,
3,
false
),
Using OFFSET instead of a range ensures that they have the same size.
Final error management
This formula fails when the key doesn't have name or date (which is outside the table or there that entry missing). For that case we add another IFERROR:
IFERROR(
<formula>,
""
)
Complete formula
=ARRAYFORMULA(
IFERROR(
IFERROR(
VLOOKUP(
A3:A&"␟"&C1:1,
SPLIT(
{
FILTER(Att!$A$2:$A&"␟"&Att!$B$2:$B&"␞Present␞", Att!$A$2:$A<>"", Att!$B$2:$B<>"");
FILTER(Late!$A$2:$A&"␟"&Late!$B$2:$B&"␞"&Late!$C$2:$C&"␞"&Late!$D$2:$D, Late!$A$2:$A<>"", Late!$B$2:$B<>"", Late!$C$2:$C<>"")
},
"␞"
),
2,
false
),
VLOOKUP(
A3:A&"␟"&OFFSET(C1:1, 0, -1),
SPLIT(
{
FILTER(Att!$A$2:$A&"␟"&Att!$B$2:$B&"␞Present␞", Att!$A$2:$A<>"", Att!$B$2:$B<>"");
FILTER(Late!$A$2:$A&"␟"&Late!$B$2:$B&"␞"&Late!$C$2:$C&"␞"&Late!$D$2:$D, Late!$A$2:$A<>"", Late!$B$2:$B<>"", Late!$C$2:$C<>"")
},
"␞"
),
3,
false
)
),
""
)
)
Final touches
The final result is something like this.
After that you can simply add formats to your taste. You can also add conditionals ones to more easily see the result.
References
MOD (Google Editors Help)
SPLIT (Google Editors Help)
TEXT (Google Editors Help)
IF (Google Editors Help)
IFERROR (Google Editors Help)
FILTER (Google Editors Help)
UNIQUE (Google Editors Help)
SORT (Google Editors Help)
TRANSPOSE (Google Editors Help)
FLATTEN (Google Editors Help)
ARRAYFORMULA (Google Editors Help)
VLOOKUP (Google Editors Help)
OFFSET (Google Editors Help)
I've completed a not-so-neat solution for you, starting on Row10 in the 'AUTO ATTENDANCE' sheet. It's divided into 4 parts:
The formula in cell D10 auto-populates Row10 with dates and empty cells in between:
=SPLIT(JOIN("|REASON|",SORT(UNIQUE({'ATTENDANCE RESPONSES FROM GOOGLE FORM'!$B$2:$B;'LATE/ABSENT RESPONSES FROM GOOGLE FORM'!$B$2:$B}))),"|")
Row 11 gets the day of the week from row 10 (if the cell above it contains a date:
=IF(ISDATE(D10),TEXT(D10,"dddd"),)
Cell C12 gets all unique names from both response sheets (auto-populates the name column):
=SORT(UNIQUE({'ATTENDANCE RESPONSES FROM GOOGLE FORM'!$A$2:$A;'LATE/ABSENT RESPONSES FROM GOOGLE FORM'!$A$11:$A}))
Cell D12 onwards gets the form responses and does the auto-attendance:
=IF($C12<>"", IF(ISDATE(D$1), IF(IFERROR(QUERY('ATTENDANCE RESPONSES FROM GOOGLE FORM'!$A$2:$B,"select A where A = '"&$C12&"' AND B = datetime '"&TEXT(D$1, "yyyy-mm-dd hh:mm:ss")&"'"), "noresult")=$C12, "PRESENT", IFERROR(QUERY('LATE/ABSENT RESPONSES FROM GOOGLE FORM'!$A$2:$D, "select C where A = '"&$C12&"' and B = datetime '"&TEXT(D$1, "yyyy-mm-d hh:mm:ss")&"'"), "NO RESPONSE")), IFERROR(QUERY('LATE/ABSENT RESPONSES FROM GOOGLE FORM'!$A$2:$D, "select D where A = '"&$C12&"' and B = datetime '"&TEXT(C$1, "yyyy-mm-d hh:mm:ss")&"'"), )), )
The cells with yellow background contain formulae, the ones with green background do not contain formulae but will be auto-populated as the forms get more responses. The single cell with red background (C11), you'll have to write manually ;) Hope this solves your issue!

How to import multiple column values with one condition?

I am having problems in getting the values. I need to get the values of July 10, 2020 to July 25, 2020 under column TL "June Troy". I have tried to do query with importrange and filter with importrange. But I cannot get it right. Please help.
If I understand your question, the following query should work for you:
=QUERY(ARRAYFORMULA(TO_TEXT({importrange("https://docs.google.com/spreadsheets/d/1CQkhI5dZoIUfoKF1aQ8lm1Y8rmOOZapaoYBJw8BJTSE/edit?usp=sharing","Attendance!A1:BC99")})),
"select Col7,Col8,Col9,Col10,Col11,Col12,Col13,Col14,Col15,Col16,Col17,Col18,Col19,Col20,Col21,Col22,Col23,Col24,Col25 where Col7 = 'June Troy' ",1)
Note that since your test data has June Troy on every row, this ends up selecting every row.
More importantly, your "value" columns have mixed data types, both numeric and string values, and QUERY ignores the minority data types and returns blanks for those values. So I included the TO_TEXT function to convert individual cells to text before passing them to the QUERY. And to make the TO_TEXT act on every cell in the range, it is wrapped in an ARRAYFORMULA.
Let us know if this works for you.
UPDATED: To correct formula. Sorry about that.
This is an answer to your second question, which should perhaps be a separate from the first part, since it is a different issue. But yes, you should be able to connect two IMPORTRANGE queries. Consider this formula:
={
QUERY(ARRAYFORMULA(TO_TEXT({importrange("https://docs.google.com/spreadsheets/d/1CQkhI5dZoIUfoKF1aQ8lm1Y8rmOOZapaoYBJw8BJTSE/edit?usp=sharing","Attendance!A1:BC99")})),
"select Col7,Col8,Col9,Col10,Col11,Col12,Col13,Col14,Col15
where Col7 = 'June Troy' ",1),
QUERY(ARRAYFORMULA(TO_TEXT({importrange("https://docs.google.com/spreadsheets/d/1CQkhI5dZoIUfoKF1aQ8lm1Y8rmOOZapaoYBJw8BJTSE/edit?usp=sharing","Attendance!A1:BC99")})),
"select Col16,Col17,Col18,Col19,Col20,Col21,Col22,Col23,Col24,Col25
where Col7 = 'June Troy' ",1)
}
Basically, you would have two very similar queries. In my example, I point them both at the same sheet, but you can point to a different link, for one of the queries.
They are wrapped in braces, "{...}", to form a new array. And most importantly, the first query has a comma, ",", after it, to force the result of the second query to be in adjacent columns, on the same rows. If you separate the two queries with a semi-colon, ";", the result of the second query would be added as rows underneath the first query, not in columns beside it.
HOWEVER, I think this causes an error if the two queries don't both return the same number of rows. So that will depend on your data. But since you are getting related columns, I'm assuming they should return the same number of rows. If not, share the data from your two sample sheets, and what the desired outcome should look like.

Google Sheets Query returning results that don't exist in the source list

I am creating a budget tracker within Google Sheets. I am having trouble with Google Query Language returning results that do not exist in the source data
I have a Google Form into which expenditure can be entered, which populates an associated Sheet in my Google Drive. My Master budget tracker also sits within my Google Drive. I use IMPORTRANGE to pull the data into the Master from the Responses spreadsheet, and then Query to separate this out into different 'expenditure' categories on sheets within the workbook (one for each month). February worked perfectly - all expenditure was found and summed correctly and then the February sheet was duplicated for each month of the year, the formulae or look up terms updated. But March is acting strangely - it is returning a value for March that doesn't exist - and not only does the summed value not exist but the word 'March' also does not exist either, so shouldn't be matching. I have tweaked the code, tried refreshing and rewriting the formula into the cell to force a refresh, re-imported the range from the external spreadsheet, I have tried various parentheses placements (I'm not a SQL or Google Query Language expert, so am feeling my way a bit) as I thought it was something to do with the AND/OR clauses not being 'collated', but none has produced even a different result, it's always the same false value being returned
The query code is as follows:
=query(spend, "select sum(B) where H = '"&$H$1&"' and E='Leisure' or E='Tickets' or E='Parking' or E='Other' label sum(B) ''", -1)
'spend' is the range containing the data imported from the Responses spreadsheet, which includes several post form completion formulae coding the row with a day and a month. Right now, there are only values coded as 'February' in H - nothing else. Cell H1 contains the month name (written in, not formula). This formula works perfectly within the 'February' sheet, and if I update cell H1 to read 'February' in the March sheet it shows the accurate values for February, however, if I enter 'March' in H1 I am getting the odd outcome
I am expecting a £0 result for March, but instead, I am getting a value of £19.46. As previously described - the source list 'spend' only contains values coded as February in H, and the value £19.46 does not appear singularly in the list (and doesn't appear to be made by any values when 'summed'). I am at a loss as to what is happening, and no answers seem to address the appearance of mystery values, so I hope I'm not repeating old ground - please do correct me if I am, and many thanks in advance for any guidance
you picked 19.46 because even if H1 wasn't found in Query, Query continued to evaluate for AND and OR statements and sum up a bunch of nonsense
=IFERROR(IF(QUERY(spend,
"select count(H)
where H='"&$H$1&"'
label count(H)''", 0)>0,
QUERY(spend, "select sum(B)
where (H='"&$H$1&"')
AND (E='Leisure')
OR (E='Tickets')
OR (E='Parking')
OR (E='Other')
label sum(B)''", -1), ), )
I think you might just want to try this:
=query(spend, "select sum(B) where H = '"&$H$1&"' AND (E='Leisure' OR E='Tickets' OR E='Parking' OR E='Other') label sum(B) ''", -1)
EDIT: This will cause an error since it will return an empty query, so try this instead.
=iferror(query(spend, "select sum(B) where H = '"&$H$1&"' AND (E='Leisure' OR E='Tickets' OR E='Parking' OR E='Other') label sum(B) ''", -1),0)

using both QUERY and FILTER together in a single statement?

I hope someone can help me; I am building some spreadsheets to help with time-tracking. I have a list of tasks, with columns for criteria including date, hours spent, category of work, and client.
I want to filter this data by month, so for example I would like to know how long I spent in a single month on correspondence. This means I need to select all the rows where category = 'correspondence' and where the dates are all from one specified month. At the moment, I am having to use a query which outputs to an intermediary table, and then run a filter function on that table in order to output to my final table. Here are my two functions:
=QUERY( 'Task List'!A4:F , "select A, B, E, F where C = 'Correspondence'" )
that gives me the first table, with just the rows where the category is "Correspondence". Then, on that table, I have to run the next function:
=filter(J4:M,J4:J>=date(2015,4,1),J4:J<=date(2015,4,31))
To get only the rows from this month of April. If possible I would like to remove the intermediary table (which serves no other purpose and just clutters my sheet).
Is it possible to combine these statements and do the process in one step?
Thanks.
That is indeed possible.
Since you didn't specify in which column the dates are to be found (in the 'raw' data), I assumed for this example that dates are in col F. The easiest way would be to use the MONTH() function. However, when used in query(), this function considers January as month 0. That's why I added the +1. See if this works ?
=QUERY( 'Task List'!A4:F , "select A, B, E, F where C = 'Correspondence' and month(F)+1 =4 ")
I came to this question needing to filter by weeknum() and year() as well as query by contains(). It can be helpful to combine the query and filter functions for similar but more dynamic date and text matching needs. If for example the OP had needed to show this data by week, that is not available in the Google Query Language.
The filter function does not have the contains function so you are limited to exact match text or using Reg-Ex. The Query Lanuague does not have the Weeknum functions.
Combining Filter and Query can be useful in scenario similar to this question but with a dynamic timeline (no hard set month or date such as rolling timeline) and where the text your matching is not exact (when you need to use a contains function from query language).
Here is an example for combining filter and query in Google sheets.
=(sum(Filter(QUERY(FB!$A:$Z, "select Q where B contains 'Apple'"), Weeknum (QUERY(FB!$A:$Z, "select E where B contains 'Apple'")) = Weeknum($A8))))
In this example I queried Facebook ads data export for any posts which contained the word 'Apple' in their title, and where Weeknum() matched the ongoing weeks on my sheet, in order to pull weekly data from multiple sources into one table to build reports, with minimal updating required as the timeline runs on.
It selects Q(spend) Where B(title) contains Apple, and Weeknum(E) matches week number on current row of sheet(A8). I have found this useful many times. Query + Filter Example Sheet Here.
If OP wanted to pull this info dynamically as the months went on if A column contained months in order the formula could be pulled along and would automatically pull data from query data filtered by matching month month.
=(sum(Filter(QUERY( 'Task List'!A:Z , "select A, B, E, F, J where C contains 'Correspondence'" ), Month(QUERY( 'Task List'!A4:F , "select J where C contains 'Correspondence'" )) = Month('$A2'))))

Resources