Google Sheets - Trick to have flexible ARRAYFORMULA - google-sheets

first time posting here!
I have the following scenario:
1 Google Sheet with information sorted in tables (Master Data)
1 Google Sheet that =Importrange the data from the Master Data Google Sheet.
I need to import one time, or multiple times, some of the rows that are Imported from the Master Data based on the following criteria:
On the Master Data Google Sheet, a column would be present, showing in which Country/Countries the student lives. If the student lives 1 country, import the row once. If it is in 2,3,4... countries, import the same row it 2,3,4... times.
Right now, I am using the following formula:
=QUERY({IMPORTRANGE(Reference!A8,Reference!$A$2&Reference!B6)},"select Col6 where Col10='"&'Advanced Settings'!B5&"'")
This formula Imports from the Master Data file (Reference!A8), a particular tab (Reference!$A$2) and a particular range in this tab Reference!B6. Finally, it filters the data imported (only the 6th Col of the range, and only if on Col 10 the row has a particular value (Advanced Settings'!B5).
Is there a way to Import the name of the student as many times as countries they live in inside the same Array formula?
Right now, I am just adding more importrange (if there are 3 countries, I will add Importrange three times) with filters, but I would like to make it dynamic for the number of countries, without manual input every time. Also, the number of students imported varies every time so I can't look manually at the number of rows and then add a formula after the last cell of the array formula.
Thanks!
EDIT:
Sample Data and expected result:
Sample Data
Student Name
Gender
Class Level
Home State
Country
Alexandra
Female
4. Senior
CA
UK, US
Andrew
Male
1. Freshman
SD
UK
Anna
Female
1. Freshman
NC
UK, US
Becky
Female
4. Senior
SD
US
Benjamin
Male
4. Senior
WI
UK
Filter on both Class Level (4. Senior) and Country
Name
Reason for appearing (explanation for you)
Alexandra
Appears because Alexandra is Senior, UK
Alexandra
Appears because Alexandra is Senior, US
Becky
Appears because Becky is Senior, US
Benjamin
Appears because Benjamin is Senior, UK
The expected result here is that Alexandra appears twice as she's Senior and both US and UK.

if Reference!B6 is a range and IMPORTRANGE for each country is the same try:
=QUERY({IMPORTRANGE(Reference!A8, Reference!A2&Reference!B6)},
"select Col6
where Col10 matches '"&TEXTJOIN("|", 1, 'Advanced Settings'!B5:B)&"'", )

IF C = Senior and E is splittable, create a array of corresponding As. Then, FLATTEN the array and REDUCE to remove all empty items in the array.
Sample:
=ARRAYFORMULA(
REDUCE(
"Senior List",
FLATTEN(
IF(
C2:C6="4. Senior",
IF(ISTEXT(SPLIT(E2:E6,",")),A2:A6,),
)
),
LAMBDA(a,c,IF(c="",a,{a;c}))
)
)

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)

Matching Columns From Different sheets

There are many similar questions on here but I can not find the answer I am after.
I am working in Google Sheets and need to write a conditional formatting equation that will highlight the B:C columns if they match an entry on the second sheet in the B:C columns. I do not know any search criteria past that which is where I keep running into an error. All the other questions here know what they are searching for. My work is fluid and new entries come in that may match an old one and I need to know that!
I use this formula on the first sheet to match entries specifically on the first sheet,
=COUNTIF(ARRAYFORMULA($B$3:$B$389&$C$3:$C$389),$B2&$C2)>1
This formula works great. If Lexington, SC is in row 2 and Lexington, SC is added again later in row 100, BAM they both light up red. Love that. The second equation I need is not so simple.
my data is:
SHEET 1 - Column B and Column C
County
State
Lexington
SC
Douglas
KS
Chase
KS
Clay
OH
Greenwood
NY
SHEET 2 - Column B and Column C
County
State
Saratoga
NY
Douglas
KS
Chase
KS
Clay
OH
Greenwood
SC
In this example above, I need the new formula to highlight Douglas KS, Chase KS, Clay OH on sheet one, because these three counties exists on Sheet Two.
I have tried MATCH, INDEX, VLOOKUP, etc. I keep running into an issue when I want TWO columns to be compared to TWO columns. It needs to be county name AND state.
Thank you to the community for any help you can give. I will provide more information if it is needed! I think this sums up my issue though.
QUERY ATTEMPTS
=COUNTIF(ARRAYFORMULA($B$2:$B$20&$C$2:$C$20),$B2&$C2)>1
=COUNTIF(ARRAYFORMULA('Sheet 2'!$B$2:$B$20&$C$2:$C$20),$B2&$C2)>1
=COUNTIF(ARRAYFORMULA(indirect("Sheet1!$B$2:$B$C$2:$C"),indirect("Sheet2!$B2:$B&$C2:$C")))>1
=ARRAYFORMULA(IFERROR(VLOOKUP(B2:C,"Sheet 2!$B2:$C",2, FALSE),""))
=ARRAYFORMULA(INDEX(B2:B19, MATCH(1, FIND(E2, C2:C19)), 0))
=ARRAYFORMULA(INDEX(B2:C,MATCH(indirect("Sheet 2!$B2:$C"),0)))
=MATCH($B2&$C2,indirect('Sheet 2'!$B2:$B&'Sheet 2'!$C2:$C"),0)
=MATCH($B2,indirect("Sheet 2!$B2:$C"),0)
=MATCH("Sheet 1!$B2&$C2",indirect("Sheet 2!$B2:$C"),0)
=MATCH("Sheet 1!$B:$C"),indirect("Sheet 2!$B:$C"),0)
=MATCH((B2&C2),indirect("Sheet 2!(B2&C2)"),0)
=INDEX($B2:$C2,MATCH($B2&$C2,indirect("Sheet 2!$B2:$C"),0)
=INDEX(B:C,MATCH($B2&$C2,indirect("Sheet 2!$B2:$C"),0)
=INDEX(B2:C710,MATCH(0,(B:C="Sheet 2!$B:$C"),0))
The only query I have gotten to work only works on the B column
=match($B2,indirect("Ready to Start!$B2:$B"),0)
in conditional formatting (and data validation) whenever you want to refer to another sheet, that sheet's range needs to be INDIRECTed. if you got a joint range (like you do) both of them need to be wrapped in INDIRECT
try:
=COUNTIF(ARRAYFORMULA($B$3:$B$389&$C$3:$C$389),
INDIRECT("Sheet 2!B2")&INDIRECT("Sheet 2!C2"))>1
or:
=COUNTIF(ARRAYFORMULA(INDIRECT("Sheet 2!B3:B389")&INDIRECT("Sheet 2!C3:C389")),
$B2&$C2)>1
but note that this will work only if let's say B4&C4 on BOTH sheets are the same.
if one sheet compared to other has different sorting of the same values you will need a completely different formula...
if your sheets are named exactly:
Sheet 1
Sheet 2
try this in Sheet 1:
=($A2<>"")*REGEXMATCH($A2&$B2, TEXTJOIN("|", 1,
INDIRECT("Sheet 2!A2:A")&INDIRECT("Sheet 2!B2:B")))

Google Sheets VLOOKUP formula stops working when new item values are added to the column being looked up

Hello stack overflowers.
I have recently been creating a nutrition tracker to better track and control my nutrition. However I have run into an issue. So currently I have a 1 sheet in the nutrition tracker which is a "database" of foods and their macro nutrients per gram. In this sheet, I will enter all the foods that I generally eat and their associated per G nutrients.
this food "database" sheet has the following columns.
FOOD NAME, CALORIES, PROTEIN, CARBS, OF WHICH SUGARS, FAT, OF WHICH SATURATED, FIBER, SALT
This database is then used as a reference, so that when I input each meal as I eat it, I can simply select the food from a drop down list and type the number of G in that meal, and the nutrients will all be calculated for me.
I currently have it setup so that I can select food from the drop down list generated by "foods" sheet, within each meal table I have created. This is then correctly filling in the rest of the columns as expected once I input a weight for each meal. There is however a huge problem.
As soon as the FOOD NAME column of the Foods sheet had values in it below row 7 (not sure why this row is the limit) the whole thing stops working, the data grabs based on VLOOKUP just return 0 and do not act as they are meant to. The strange thing is they work absolutely fine until I enter too many foods (7 foods) into the foods sheet.
Please find below a link to my spreadsheet, maybe you can duplicate it and play around a little yourselves to better understand the issue.
https://docs.google.com/spreadsheets/d/1orwih7s_Z4ew8G1vJcR6qlxyMpX8pqK-3Ynj42qQjcQ/edit?usp=sharing
(if you help me fix it, you will have a free nutrition tracking spreadsheet to help you take control of your diet aswell)
Thanks in advance.
In the June tab, clear all formulas in the range D11:K18.
Then enter in D11
=ArrayFormula(IF(LEN(B11:B18), IFERROR(VLOOKUP(B11:B18, FOODS!A:I, {2, 3, 4, 5, 6, 7}, 0)),))
This single formula will process all values entered in B11:B18.
Note the third parameter of VLOOKUP (set to false). If it is ommitted (as in your formula) it will default to 'true'. That means vlookup expects a 'sorted order' which may not be the case for your data.
References
VLOOKUP
try:
=INDEX(IF(B11:B18="";; IFNA(VLOOKUP(B11:B18; FOODS!A:I; COLUMN(B:G); ))))

Is there a way to average from filtering specific data from an importrange?

I'm working on a different Google Sheets spreadsheet to input data (film details such as English Title, Original Title, Release Date, Rating, Country of Origin and a Link), while on the one I'm analyzing the data I managed to use importrange successfully.
Here is the code I used successfully in order to get the average rating of a country's list of movies:
=AVERAGE(IMPORTRANGE("LINK_TO_INPUT_DATA_GOOGLE_SHEETS", CONCAT(A1:A, "!D1:D")))
This average is outputted to column C, while the name of the Country (which is also the name of the sheet for importrange) is in Column A.
I want to create a similar query but for movies that have the Country of Origin matching the Country from Column A (Any movie that has multiple countries of origin are inputted with the first one in the spreadsheet and copied over in all the other countries of origin's respective sheets).
I tried using the QUERY from Google Sheets to make my resultset, but in the best case scenario, it gives the same result as the previous average, while in the worst case scenario it just gives out errors. Here is my latest attempt at the query:
=AVERAGE(QUERY (IMPORTRANGE("LINK_TO_INPUT_DATA_GOOGLE_SHEETS", A1:A), "SELECT Col4 WHERE Col5="&A1&""))
As far as I can tell, this should work, but at the moment it says it cannot find the range or sheet for the imported range.
Any help is deeply appreciated!
EDIT:
Here's a link of the input sheet: https://docs.google.com/spreadsheets/d/1bopmJu7Av71sCh8iUoG20WubGL9ssx09dOnBZnys4Ko/edit?usp=sharing
Here's a link of the analysis spreadsheet (the query should be in the MOVIES sheet):
https://docs.google.com/spreadsheets/d/1-hfQdqvDWXXtGR2fmTy-lZEOtp9sdxkvoget4toi1W4/edit?usp=sharing
I am not sure If I got you right:
- you want: import the average of the ratings (column 4) by movie title (column 1) where the country matches your current column A?
If so it can simply done with queries, especially if you include the average in the query as well. But you need to include all columns you use in the importrange:
=QUERY(IMPORTRANGE("https://...", "Syria!A1:E"),"SELECT AVG(Col4) WHERE Col5='"&A2&"' LABEL AVG(Col4) ''")
Explanation: group by will aggregate all columns by the column you declared as being used as average.
this is the correct syntax:
=AVERAGE(QUERY(IMPORTRANGE("ID_OR_URL"; "Sheet1!A1:A"); "SELECT Col4 WHERE Col5='"&A1&"'"; 0))

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