Using SEARCH function to count occurrence of multiple values - google-sheets

So I've Two lists in Google sheets. one is a (relatively short) list of names, let's say a rooster of employees. The second list is (rather a long) list of shifts, which notes the employees who were present.
for example:
List A - (rooster):
___________________
Mike
Linda
Carrie
Dave
List B - (Import_shift_data):
____________________________
Mike, John
Dave, Linda, Mike
Carrie
Dave, John
Linda
Mike
Dave, Carrie, John, Mike
My goal is to count the presence of each employee.
Now, here are the tricky parts:
List B updates every day, and each cell contains more than one name.
List A also updates, as some employees join the team and other leave.
Each shift could by a day shift, or a night shift (listed in another column next to List B) and I need to count them separately.
The Day/night column is in a parallel column next to shift column, and has one of two values, "Day" or "Night"
So my notion was to create an array formula, who can expand or shrink based on the number of values in List A. The problems is, I Can't yield and results from using the whole {list A} as the first argument in the SEARCH function.
I've tried the foloowing:
=Arrayformula(IF(INDIRECT("A2"):INDIRECT(CONCATENATE("A",MAX(Arrayformula(IF(isblank($A:$A),"",Row($A:$A)))))) = 0,"",COUNTIFs('Import_shift_data'!$P:$P,INDIRECT("A2"):INDIRECT(CONCATENATE("A",MAX(Arrayformula(IF(isblank($A:$A),"",Row($A:$A)))))),'Import_shift_data'!$M:$M,"Night")))
.
But this formula only works for a shift with a single employee.
I also wrote this one:
=Countifs(Arrayformula(ISNUMBER(SEARCH(A2,'Import_shift_data'!$P:$P))),"true",'Import_shift_data'!$M:$M,"Night")
which works fine, but I need to manually drag it up or down every time List A (The rooster) is updated.
So my end game is to have two arrays, one that counts night shifts for each employee, and one who counts day shifts. those arrays should automatically shrink or expand by the size of the rooster. (List A)
Note: If relevant, I may also note that the names in {List A} may contain more than one word, in case there are two employees with the same first name.
A copy of the spreadsheet:
https://drive.google.com/open?id=1HRDAy9-T_rflFpzanZq0fmHpV0jTZg6Rc4vHyOu-1HI

day shift:
=ARRAYFORMULA(QUERY(TRIM(TRANSPOSE(SPLIT(TEXTJOIN(", ", 1, B2:B), ","))),
"select Col1,count(Col1) group by Col1 label count(Col1)''", 0))
night shift:
=ARRAYFORMULA(QUERY(TRIM(TRANSPOSE(SPLIT(TEXTJOIN(", ", 1, C2:C), ","))),
"select Col1,count(Col1) group by Col1 label count(Col1)''", 0))

I Think I've found the Solution, I've used player0's idea of rearranging the data vector and split non-single shifts into single cells.
so basically it goes:
=Arrayformula(CountiF(Transpose(SPlit(Textjoin(" , ",TRUE,QUERY('Import_shift_data'!A:P, "select P where M = 'Night' ", 1))," , ",False)),INDIRECT("A2"):INDIRECT(CONCATENATE("A",MAX(Arrayformula(IF(isblank($A:$A),"",Row($A:$A))))))))
Thanks player0 !

Related

Stack multiple queried Importranges with a single word between the two sets of imported data?

I have a master sheet that contains all of the data for other sheets. Column A in the master sheet is the name for who's sheet it will import to. So if the name is "John" then every row that has "John" in column A from the master sheet will be imported into John's sheet. I have the person's name in cell G1 of their own respective sheet and use the following formula.
=ARRAYFORMULA(IFNA(QUERY(IMPORTRANGE("LINK_TO_SHEET","Assignments!A2:Q"),"SELECT Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 WHERE Col1='"&G1&"'",0),""))
Master sheet looks like this:
Assigned To
Name
State
City
John
Blake G.
Arizona
Phoenix
Andy
Chase C.
Arizona
Phoenix
John
Amy B.
New Mexico
Santa Fe
John
Bill S.
Texas
Austin
John's sheet will look like this:
Name
State
City
Blake G.
Arizona
Phoenix
Amy B.
New Mexico
Santa Fe
Bill
Texas
Austin
I want to separate the data based on what is in Col17 and put a single word between the two data sets. Col17 has either a Y or a N so I can just add AND Col17='N' to the existing formula. I double up the formula, but changed the last condition to be Col17='Y' so that I can pull the two data sets separately and on top of each other.
={ARRAYFORMULA(IFNA(QUERY(IMPORTRANGE("LINK_TO_SHEET","Assignments!A2:Q"),"SELECT Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 WHERE Col1='"&G1&"' AND Col17='N'",0),""));ARRAYFORMULA(IFNA(QUERY(IMPORTRANGE("LINK_TO_SHEET","Assignments!A2:Q"),"SELECT Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 WHERE Col1='"&G1&"' AND Col17='Y'",0),""))}
What I want to do is add a single word between the two imported sets of data. It should like kinda like this:
Name
State
City
Blake G.
Arizona
Phoenix
Amy B.
New Mexico
Santa Fe
Inactive
Bill
Texas
Austin
I added the word inactive so the middle looks like ""));"Inactive";ARRAY between the two formulas but because of how Query works I get an error saying the array lengths for each formula do not match. Is there alternative to help make this happen or will I need to figure out a workaround with something that isn't Query?
SUGGESTION
Feel free to comment below if ever your question has been misunderstood.
In my understanding, here's your process:
The first formula will list all data that has N value in Col17 .
The second formula will list all data that has Y value in Col17.
This second formula should also have an Inactive title header.
Merge the two formulas separated by the word "Inactive".
If I got it correctly, you could use the label clause to add "Inactive" in your second Query function as seen on this tweaked formula:
={ARRAYFORMULA(IFNA(QUERY(IMPORTRANGE("SHEET_URL","A1:Q"),"SELECT Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 WHERE Col1 = '"&G1&"' AND Col17 = 'Y'"),0));ARRAYFORMULA(IFNA(QUERY(IMPORTRANGE("SHEET_URL","A1:Q"),"SELECT Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 WHERE Col1 = '"&G1&"' AND Col17 = 'N' label Col2 'Inactive'"),0))}
Demo
Sample result.
E.g. If there's new data added that should be under "Inactive".
Main Sheet
John's Sheet
#SputnikDrunk2 is a very good workaround. Just in case you decide to stack different types of ranges that are not queries, you should consider that they need to have the same amount of columns. So if you add a new row it should have the 12 columns you have from your Queries. One way would be like this:
={ARRAYFORMULA(IFNA(QUERY(IMPORTRANGE("LINK_TO_SHEET","Assignments!A2:Q"),"SELECT Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 WHERE Col1='"&G1&"' AND Col17='N'",0),""));
{"Inactive","","","","","","","","","","",""};
ARRAYFORMULA(IFNA(QUERY(IMPORTRANGE("LINK_TO_SHEET","Assignments!A2:Q"),"SELECT Col2,Col3,Col4,Col5,Col6,Col7,Col8,Col9,Col10,Col11,Col12,Col13 WHERE Col1='"&G1&"' AND Col17='Y'",0),""))}
Another option to avoid creating so many "","","","" is to use this "trick" that divides a range by 0, and with IFERROR it returns empty cells:
{"Inactive",INDEX (IFERROR(SEQUENCE(1,11)/0,))};
Here's another approach:
=lambda(z,
{filter(choosecols(z,2,3,4,5,6,7,8,9,10,11,12,13),index(z,,1)=A1,index(z,,17)="N");
{"Inactive",makearray(1,11,lambda(r,c,iferror(1/0)))};
filter(choosecols(z,2,3,4,5,6,7,8,9,10,11,12,13),index(z,,1)=A1,index(z,,17)="Y")}
)(A1:Q4)
A1:Q4 part in the formula should be replaced with your importrange Fx
2,3,4,5,6,7,8,9,10,11,12,13 part can also be replaced with shorter sequence(1,12,2,1)

Lookup in subtables

I want to lookup a table consisting of classes and corresponding number of students:
Given a number and a letter (e.g: 7, 'C'), I need a formula that determine # of students in the class given by them (e.g: class 7C)? A formula like:
=vlookup(A1, studentTableOf(B1), 2, 0)
Where A1 is a cell containing the character C and B1 contains the number 7 for example, and studentTableOf() is the function I am trying to figure out.
What I tried
I tried restructuring my tables (at the cost of readability) which I think made the question more simple: applying vlookup twice in a multiple-keys table. But since it affect readability, I did not like setting up my tables like this.
I would recommend using the table approach that you mentioned trying. I feel that is far more elegant and efficient.
However if that is not possible then here is my solution given the data you've shared.
Use the formula in K2 to get the # of students for the Grade mentioned in I2 and the Class mentioned in J2
=QUERY(ArrayFormula(SPLIT({(A1&"✦"&A2:A&"✦"&B2:B);(C1&"✦"&C2:C&"✦"&D2:D);(E1&"✦"&E2:E&"✦"&F2:F)}, "✦")), "select Col3 where Col1 = '"&CONCATENATE("Grade ", I2)&"' and Col2 = '"&J2&"' and Col3 is not null", 0)
How it works:
Make a table (similar to the one you mentioned trying) by adding Grade names mentioned in Row 1 against the Class name and the # of students for every grade. The part of the formula that does this is:
ArrayFormula(SPLIT({(A1&"✦"&A2:A&"✦"&B2:B);(C1&"✦"&C2:C&"✦"&D2:D);(E1&"✦"&E2:E&"✦"&F2:F)}, "✦")). If you add more Grades then you will have to update this part of the formula accordingly. Note the table made here is not elegant as it repeats values like 'Class' and '# of students' but it will not matter as move to step 2.
Use Query to ingest the table created in step 1 and then filter the results of # of students for the Grade mentioned in I2 and the 'Class' mentioned in J2.
try:
=ARRAYFORMULA(IFNA(VLOOKUP(I3; INDIRECT(
ADDRESS(3; MATCH(I2&""; REGEXEXTRACT(1:1; "\d+"); ))&":"&
ADDRESS(ROWS(A:A); MATCH(I2&""; REGEXEXTRACT(1:1; "\d+"); )+1)); 2; )))
or shorter:
=INDEX(IFNA(VLOOKUP(I3; INDIRECT(
ADDRESS(3; MATCH(H2&" "&I2; 1:1; ))&":"&
ADDRESS(ROWS(A:A); MATCH(H2&" "&I2; 1:1; )+1)); 2; )))
or different approach:
=INDEX(IFNA(VLOOKUP("Grade "&I2&I3,
{FLATTEN(FILTER(A1:F1&A3:F100, MOD(COLUMN(A:F)-1, 2)=0)),
FLATTEN(FILTER(A1:F1&A3:F100, MOD(COLUMN(A:F), 2)=0))}, 2, )))

Display the multiple values from a drop down

here i have a sheet, in that we can find the sum of diff categories using query function by selecting from drop down list. but here I can select one month only at a time can i find the amount of January and February at the same time by adding another column for another month or in any other way. here I can find the sales of one month at a time. I want to find sales of two or three months at time.
Please help
https://docs.google.com/spreadsheets/d/1jdtrtdNQBsxiZt8FjvbaE9omCBs8x8vRgp0r2bc1_7c/edit#gid=0
There's no way you can make a drop down list with multiple choice in Google Sheets.
But there are some alternatives.
List of tick boxes (here as list of months)
Manual input of multiple values separated by comma or something else.
I give both:
Months are selected as list separated by | so it can be used as regex inside 'matches' clause in query
This generates list of months:
=join("|",query({A2:B7;C2:D7},"select Col2 where Col1 = true "))
Window with manual input works similar way
=substitute(substitute(F3,", ",","),",","|")
It takes its contents, removes spaces that are adjacent to comma, adds separator | instead of comma. It's case sensitive and I don't know how to get rid of this (?i) does not work within query.
All together it looks like on the picture and combined formula is:
=query(ORDERS!A1:R14,"select A, B, C , D where
A matches '"&join("|",query({A2:B7;C2:D7},"select Col2 where Col1 = true "))&"' and
B matches '"&substitute(substitute(F3,", ",","),",","|")&"'",0)
Here is my solution:
https://docs.google.com/spreadsheets/d/1fQ5_VdxZ-t4MqPbLqzb8q-saqp5Jqz3hVXeWHX_Lls4/copy
I copied your spreadsheet to do my testing. Here's what you can do.
Add another row of the same exact selection found on your "A" row.
Change your formula to this: ={query(ORDERS!A1:R,"Select * where A contains '"&$A2&"' and B contains '"&$B2&"'",1);query(ORDERS!A1:R,"Select * where A contains '"&$A3&"' and B contains '"&$B3&"'",0)}
What this does it run an array of two sets of formulas (In this case 2 queries) for the same set of data.
Here's the screenshot of the output if you're interested.
Sample Screenshot

Using COUNTIF's & Total Sums In Google Sheets To Work Out A Financial Total Sum

I'm looking for a specific Google Sheets formula. I believe it's using COUNTIFS & SUMIF, but I can't work out how to do it, any help would be really appreciated.
So, my "C" column has a number of letter combinations in each cell... These are "FHG" "CS" "MO" and afew others. They are a reference to specific market trading strategies and I'm using this sheet to track every trade I make. So C3 may contain "FHG" C4, "MO" C5, "FHG", C6 "FHG" etc.
In the "I" column, there is a 'profit and loss' column which states how much was generated/lost for that particular row... These are formatted to currency.
At the top of my sheet, I have a summary list of each set of letters and I'm using a COUNTIF statement to work out the total number (instance count) of each particular letter combo and from there, I can work out strike rates, percentages etc.
The thing I can't work out is, 'is there a way to count/sum the relative numbers in the "I" column based on a specific letter combo?' For example, let's say running down today's trades, FHG appears on C3, C6, C8, C12, C16 etc. can I collect and sum the numbers (to create a total) from the corresponding P&L column, I3, I6, I8, I12, I16 etc?
My main aim is to have the following at the top in my "Summary Section":
'Letter Combo/Name Of Trading Technique' - 'Number Of Trades' (Using COUNTIFS down C Column) - 'Total % Win Rate' (Number Of Winners/Number Of Total Trades For Said Strategy - This is done using a separate column of Y or N for winners or losers) - The one that I need help with is - "Total Sum Of Money Generated For Each Particular Letter Combo (Trading Strategy)"...... I've worked out how to do the first 3 columns, but I can't work out how to pick the numbers out from the P&L column (I) based on what kind of trade (letter combo) exists in the corresponding (C) column.
If every trade made the same about of money, this would probably be an easy calculation, but because each return is different, this is proving challenging.
Thanks for any insights :)
try:
=QUERY(A:I; "select C,sum(I) where A is not null group by C label sum(I)''")
or just for SB:
=QUERY(A:I; "select C,sum(I) where C = 'SB' group by C label sum(I)''")
if you want a count too:
=QUERY(A:I; "select C,count(C),sum(I) where A is not null group by C")

How to count # lates per student name?

sample attendance (google) sheet
I want count the number of total lates per student name on this sample sheet, e.g, how many lates Mary Love has (actual sheet has over 30,000 rows, 10 columns). If possible, this count needs to change as students have additional lates. I'd really appreciate any help someone might be able to provide. I thank you very much in advance.
In addition to previous post, a QUERY() function also seems a good option as it can output a 2D-array, containing the names and the counts in one formula (no need to drag down). As an example, try:
=query(A:G, "select A, count(B) where B ='Late' group by A ", 1)
If you want to limit the result tho those who had more then 3 late's, you can do:
=query(query(A:G, "select A, count(B) where B ='Late' group by A ", 1), "where Col2 > 3")
Also further filtering with date can be done very easily. However I think that may require some 'cleaning up' of the current data: I noticed a lot of different date formats in col D... :-)
For counting each of several students and to allow greater versatility I suggest a pivot table with Student Name for Rows, Late for Values with Summarise by: COUNTA and Late for Filter with Show: Late.
You can use the COUNTIFS function to get what you are looking for. This formula will count all matches for multiple criteria ranges and criteria (e.g. student name & late).
This is the syntax for the formula:
COUNTIFS(criteria_range1, criteria1, [criteria_range2, criteria2]…)
If you put Mary Love in cell I2, you could then put this in J2.
=countifs(B:B,"=late", A:A,I2)
Then as more rows are added it will automatically update the "lates". I would also suggest using named ranges, then you could replace B:B and A:A with the named range.
I would suggest using the =query function as documented below. If that doesn't meet your needs you could modify the above:
=if (countifs(B:B,"=Late", A:A,I2, D:D, ">="&N2) >= 3, "True", "False")
This will put "True" in the column if there are 3 or more lates, false if not. This will require you put the date you want to check against in column N2. Change N2 above if you want to use a different column.

Resources