I have two columns of data — id's and data values. I can get vlookup to work if I want to return the correct fruit if I provide an id, but I need to also be able to account for cases where multiple values are provided as well.
Put another way, I need to be able to run my vlookup on each item in a comma-separated list in another cell.
id
fruit
input
output
2835
apples
4792
pears
2232
bananas
2835
apples
3244
peaches
1199,3244,2835,4790
should be: oranges,peaches,etc…
4792
pears
1199
oranges
Lookup multiple values in a single cell (comma separated) and then return the values to a single cell (also comma separated)
Vlookup using a comma separated search key in Google Sheets
I feel like I'm very close with the linked posts above, but I keep getting errors. This is what I have, though I'm open to an alternative approach (or something using Google Apps Script)
=arrayformula(left(concatenate(vlookup(split(D4,","),$A$2:$B$6,2,false)&","),len(concatenate(vlookup(split(D4,","),$A$2:$B$6,2,false)&","))-1))
You can try with this:
=IF(ISNUMBER(D4),vlookup(D4,A:B,2,0),join(",",query(A:B,"Select B where A = "&JOIN(" OR A = ",split(D4,",",1,1)))))
If D4 is a number (so it's not varios values) it does the VLOOKUP, if it is not it split the values and inserts them in a query, then join the results with commas again.
If you want it for the whole column:
=BYROW(D2:D,LAMBDA(each,IF(each="","",IF(isnumber(each),vlookup(each,A:B,2,0),join(",",query(A:B,"Select B where A = "&JOIN(" OR A = ",split(each,",",1,1))))))))
Here is an alternative solution using XLOOKUP function:
=JOIN(",",ARRAYFORMULA(XLOOKUP(SPLIT(D4,","),A:A,B:B,"",0,1)))
Result:
Related
I have a use case where I need to concatenate values from merged cell with other columns based on condition
Name
Frequency
Old Measure
New Measure
What's needed
Name1
Freq1
Mea1
Name1-Freq1-Mea1
Freq2
Nmea1
Name1-Freq2-Nmea1
Freq3
Mea2
Name1-Freq3-Mea2
Name2
Freq4
Mea3
Name2-Freq4-Mea3
Freq5
Nmea2
Name2-Freq5-Nmea2
Name3
Freq6
Mea4
Name3-Freq6-Mea4
Name4
Freq7
Nmea3
Name4-Freq7-Nmea3
Name5
Freq8
Nmea4
Name5-Freq8-Nmea4
Freq9
Nmea5
Name5-Freq9-Nmea5
The formula should check for column Old Measure and New Measure which ever is filled should concatenate with Name and Frequency.
I did try to take the answer from this similar question - Concatenate merged cells with Google Sheets
and added the check for Old Measure and New Measure column as below but not giving correct results.
=IFS(C1<>"", TEXTJOIN("-",false,INDEX(A:A,LARGE((ISBLANK(A:A)=FALSE)*(ROW(A:A)<=ROW())*ROW(A:A),1)),B1,C1) , D1<>"", TEXTJOIN("-",false,INDEX(A:A,LARGE((ISBLANK(A:A)=FALSE)*(ROW(A:A)<=ROW())*ROW(A:A),1)),B1,D1))
Here's a possible solution:
=ArrayFormula(query(byrow(
{lookup(row(A2:A),row(A2:A)/(A2:A<>""),A2:A),B2:D},
lambda(r,textjoin("-",1,r))),
"limit "&-1+max(if(B2:D<>"",row(B2:D)))))
Explanation
This part
lookup(row(A2:A),row(A2:A)/(A2:A<>""),A2:A)
Fills the gaps in A2:A with the last non-empty value above.
Then
byrow(...,lambda(r,textjoin("-",1,r)))
Concatenates row-by-row the values from the previous array and the values in B2:D.
And finally
query(...,"limit "&-1+max(if(B2:D<>"",row(B2:D)))
Constrains the resulting array to the last filled row.
I'm trying to do a couple of different things with a spreadsheet in Google and running into some problems with the formulas I am using. I'm hoping someone might be able to direct me to a better solution or be able to correct the current issue I'm having.
First off all, here is a view of the data on Sheet 1 that I am pulling from:
Example Spreadsheet
The first task I'm trying to accomplish is to create a sheet that lists all of these shift days with the date in one column and the subject ("P: Ben" or S: Nicole") in another column. This sheet would be used to import the data via a CSV into our calendar system each month. I tried doing an Index-Match where it used the date to pull the associated values however I found that I had to keep adjusting the formula offsets in order to capture new information. It doesn't seem like Index-Match works when multiple rows/columns are involved. Is there a better way to pull this information?
The second task I am trying to accomplish is to create a new tab which lists all the dates a specific person is assigned too (that way this tab will update in real time and everyone can just look at their own sheet to see what days they are on-call). However, I run into the same problem here because for each new row I have to change the formula to reflect the correct information otherwise it doesn't pull the correct cell when it finds a match.
I would appreciate any and all information/advice on how to accomplish these tasks with the formula combination I mentioned or suggestions on other formulas to use that I have not been able to find.
Thanks in advance!
Brandon. There are a few ways to attack your tasks, but looking at the structure of your data, I would use curly brackets {} to create arrays. Here is an excerpt of how Google explains arrays in Sheets:
You can also create your own arrays in a formula in your spreadsheet
by using brackets { }. The brackets allow you to group together
values, while you use the following punctuation to determine which
order the values are displayed in:
Commas: Separate columns to help you write a row of data in an array.
For example, ={1, 2} would place the number 1 in the first cell and
the number 2 in the cell to the right in a new column.
Semicolons: Separate rows to help you write a column of data in an array. For
example, ={1; 2} would place the number 1 in the first cell and the
number 2 in the cell below in a new row.
Note: For countries that use
commas as decimal separators (for example €1,00), commas would be
replaced by backslashes () when creating arrays.
You can join multiple ranges into one continuous range using this same
punctuation. For example, to combine values from A1-A10 with the
values from D1-D10, you can use the following formula to create a
range in a continuous column: ={A1:A10; D1:D10}
Knowing that, here's a sample sheet of your data.
First Task:
create a sheet that lists all of these shift days with the date in one
column and the subject ("P: Ben" or S: Nicole") in another column.
To organize dates and subjects into discrete arrays, we'll collect them using curly brackets...
Dates: {A3:G3,A7:G7,A11:G11,A15:G15}
Subjects: {A4:G4,A5:G5,A8:G8,A9:G9,A12:G12,A13:G13,A16:G16,A17:G17}
This actually produces two rows rather than columns, but we'll deal with that in a minute. You'll note that, because there are two subjects per every one date, we need to effectively double each date captured.
Dates: {A3:G3,A3:G3,A7:G7,A7:G7,A11:G11,A11:G11,A15:G15,A15:G15}
Subjects: {A4:G4,A5:G5,A8:G8,A9:G9,A12:G12,A13:G13,A16:G16,A17:G17}
Still with me? If so, all that's left is to (a) turn these two rows into two columns using the TRANSPOSE function, (b) combine our two columns using another pair of curly brackets and a semicolon and (c) add a SORT function to list the dates in chronological order...
=SORT(TRANSPOSE({{A3:G3,A3:G3,A7:G7,A7:G7,A11:G11,A11:G11,A15:G15,A15:G15};{A4:G4,A5:G5,A8:G8,A9:G9,A12:G12,A13:G13,A16:G16,A17:G17}}),1,TRUE)
Second Task:
create a new tab which lists all the dates a specific person is
assigned too (that way this tab will update in real time and everyone
can just look at their own sheet to see what days they are on-call).
Assuming the two-column array we just created lives in A2:B53 on a new sheet called "Shifts," then we can use the FILTER function and SEARCH based on each name. The formula at the top of Ben's sheet would look like this:
=FILTER(Shifts!A2:B53,SEARCH("Ben",Shifts!B2:B53))
Hopefully this helps, but please let me know if I've misinterpreted anything. Cheers.
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"
Motive: I am trying to compare 2 cells with comma separated values within each and find out the number of common occurrences. Also based on the number of common occurrences and Experience Input Values, i want to print a certain keyword.
Attaching The Screenshot:
GOAL: The mentioned text in Deep Red are my goals:
1. Count the number of common words between B2 & B6
2. Print B1 IF certain conditions are true.
B11:
=SUMPRODUCT(TRIM(SPLIT(B2,","))=UNIQUE(TRANSPOSE(TRIM(SPLIT(B6,",")))))
B12:
=IF(AND(B11>50,B7>=B3,B7<=B4),B1,"👎")
I'm trying to concatenate my search results. I found one article describing this, but couldn't get it to work.
I'm trying to do the following:
- I have created two tables (tblBus and tblJoin). I related the tables (1:M).
- I have created a search form with a few fields to search for data.
- I've also created a query.
For most of the part everything works, except if I try to concatenate my data.
Here is an example of what I'm trying to do:
Stop Number - Route Number
110 - 111
110 - 222
115 - 111
115 - 222
I would like to combine the route numbers like this:
Stop Number - Route Number
110 - 111, 222
115 - 222, 222
Both fields are Integer fields.
You will need to use a VBA record set to create the comma delimited list of numbers.
The VBA will store the data to be displayed in a temporary table.
Your VBA will open a record set based on a, SQL query that contains your example data. The code will loop through every row in the data detecting when the number in the first column changes resetting a string variable to empty string. As it loops through each row it will add to the comma delimited string.
Alternatively you could write a function that builds a single comma delimited string that is called by a query. The calling quiet will only list the unique values in the first column. The function may be slower than VBA method. Which method you use depends on the number of rows in your table and speed.