Google sheets =QUERY() matching names on two sheets - arrays

I've hesitated to ask since this seems so simple of a formula but I've been having difficulty getting it to function. Data is imported from a different workbook to sheet1 and a different set of data is imported to sheet2. Now, I am attempting to compile data by matching column E of sheet1 with column D of sheet2 (Both columns contain names) and inputting it into sheet3 of the same workbook. Pending a match, I'm attempting to display only the pertinent data that I need for records (Columns A(date), H(number), F(number), S(text)).
=QUERY(Sheet1!A:S,"select A, H, F, S where E matches '"&Sheet2!D:D&"'",1)
This returns the heading of the requested columns, but does not display any matching information (which from a manual count there should be 5 rows displayed). Ive fiddled with different formulas to include Indexes and Filters but I cant seem to get the format down to get close to replicating what I currently have. I'm assuming the issue lies in [where E matches '"&Sheet!2D:D&"'"]. Is this comparing the length of the two columns rather than the values located in the columns? Do I need to index the columns first?

try:
=QUERY(Sheet1!A:S,
"select A,H,F,S
where E matches '"&TEXTJOIN("|", 1, Sheet2!D:D)&"'", 1)

Related

How to add values in cells ONLY when other columns contain data from a query result

Link to example file:
https://docs.google.com/spreadsheets/d/1dCQSHWjndejkyyw-chJkBjfHgzEGYoRdXmPTNKu7ykg/edit?usp=sharing
The tab "Source data" contains the data to be used in the query on the tab "Query output". The tab "Desired result" shows what I would like the end result to look like.
The goal I'm trying to achieve is to have the formula in cell A2 on the tab "Query output" to populate the data in all four of the columns, so that it looks exactly like the "Desired result" tab. I know I can get the same result simply by entering additional formulas in C2 and D2, but this is not the objective, I need the results to come specifically from the single formula in A2.
The information in the "Additional data 1" column should simply repeat the word "Test" for every row that contains data in the first two columns. The information in the "Additional data 2" column should simply repeat the data from cell 'Source data'!A1 for every row that contains data in the first two columns.
Please feel free to edit the example file as it only contains dummy data. If you like, you can copy the tab "Query output" to create your own working formula for illustrative purposes.
EDIT:
I'm thinking along the lines of creating an array that consists of the required data for the columns "Additional data 1" and "Additional data 2" and then combining that array with the array of the query result which provides the first two columns. I've been experimenting with this in various ways, but so far the only result I have achieved is an error on the first cell of the query results. I also have no idea yet how I could make sure that the second array contains an equal amount of rows to the query result.
You can add static data into query:
=QUERY('Source data'!A3:B,"SELECT A,B, 'Test', '" & 'Source data'!A1 &"' WHERE A IS NOT NULL LABEL A '', B '', 'Test' '', '" & 'Source data'!A1 &"' ''")
Many thanks to #basic for the provided assistance! The insights were a great help to solving my issue. That said, I have muddled along a bit, and I've come up with a slightly different solution which I find better suited as it gives true blank values instead of a column filled with spaces.
First of all, instead of querying directly on the source data, I built an array and queried on that. I used the two existing columns (A and B) from the source data and added a third column to the array which does not exist in the source data. In order to make sure that the third column would consist of blank values, I used the IFERROR formula.
=IFERROR(0/0)
The formula above returns a blank because dividing by zero forces an error and the IFERROR method returns a blank unless an alternative return value is specified.
In order to be able to use this formula in an array however, it had to be tweaked slightly, because as it is it would only return a single blank cell value instead of a column of blank values. To do this, I used an already existing column from the source data, and then encapsulated it in an ARRAYFORMULA.
=ARRAYFORMULA(IFERROR('Source data'!A3:A/0))
Using this, the resulting array has the following formula.
=ARRAYFORMULA({'Source data'!A3:A,'Source data'!B3:B,IFERROR('Source data'!A3:A/0)})
This creates an array consisting of the two original columns A and B from the source data, plus an additional third column filled with blank values. This array can now be queried upon, and using the tricks previously provided by #basic the desired result as specified in the original question can be achieved.
Due to the query now being used upon a user-defined array, the columns in the SELECT statement now have to be referred to as Col1, Col2, Col3, instead of A, B, C. The final formula now looks like this.
=QUERY(ARRAYFORMULA({'Source data'!A3:A,'Source data'!B3:B,IFERROR('Source data'!A3:A/0)}),"SELECT Col1,Col2,'Test',Col3,'"&'Source data'!A1&"' WHERE Col1 IS NOT NULL LABEL 'Test' '','"&'Source data'!A1&"' ''")
I hope this information may prove of use to someone else as well.

Google Sheets: Sum, Query, other Formulas Treating Values (Formatted as NUMBERS) as Zeroes in Formulas - Unclear why

I have 15 Google Workbooks that import into 1 Workbook tab through an Importrange + Query combo function. The data is then queried and transformed across several other tabs, but the problem definitely begins on this tab.
Although I've manually forced a format change to "Number" in each workbook source AND in Columns CO:CQ (where the problematic data lands), all functions see the data as zero or null. Here is what happens when I set a random cell (CW33) equal to a cell in one of the trouble columns (CO33)
However, when I wrap the cell in an =sum(), the workbook returns "0":
I have no idea how to force the workbook to see the values in these rows as numbers without creating an entirely new column - does anyone have any ideas on how to fix it while preserving the column structure?
Thanks!
try:
=SUM(CO33*1)
for multiple cells it would be:
=ARRAYFORMULA(SUM(CO33:CO34*1))
or:
=SUMPRODUCT(CO33:CO34)
AFAIK, in Sheets, a Number formatted cell displays contents right-aligned and Plain-Text are left-aligned. The highlighted cell looks like a text cell.
Try changing the format of cell or column manually to 'Number' from the 'Format' tab.

Concatenating two columns of text Alphabetically in Excel

I am working with this spreadsheet of airport data.Sorry can't figure out how to format the headers, but first column is Origin IATA, 2nd is Dest IATA, and 3rd if Concatenate Route
Origin IATA, Dest IATA, Concatenate Route
LHR JFK LHR-JFK
JFK LHR JFK-LHR
CAN PEK CAN-PEK
PEK CAN PEK-CAN
JFK LAX JFK-LAX
LHR DXB LHR-DXB
DXB LHR DXB-LHR
Picture version:
Right now, I just have =CONCATENATE(C:C,"-",D:D) as the formula, but I actually need them to concatenate in alphabetical order so I can collapse CAN-PEK as one round trip route.
I found someone with a similar question here: Excel formula to take values from cell range, sort alphabetically and write as a single string
And I tried to modify the code of the accepted answer, but I keep getting errors because I don't know what I'm doing. I also tried VBA code, doing it in access, and many other Stackoverflow solutions, but I am stuck.
Does anyone know how I need to write the code to get it to do what I want?
Thank you!
With Excel one can use mathematical inequalities for text strings in many cases. In C2 put:
=IF(A2<B2,A2&"-"&B2,B2&"-"&A2)
Then copy down
When using a formula, you would need to use your existing formula as a helper column and then add the final column containing the sorted results. The example below has the named range "concat" assigned to the concatenate column and the named range "order" assigned to the sort order column.
Note, if your data is in an Excel structured table, you don't need to use defined names. You can select table column and Excel will insert the proper syntax.
Sort Order Column (column E, copy down)
=COUNTIF(concat,"<="&E2)
Alpha Sorted Column (column F, copy down)
=INDEX(concat,MATCH(ROWS($E$2:E2),order,0))

Formula to make the outputs of two separate Filter functions display consecutively

So I have a workbook with 3 sheets. The first contains data from A:Z. The second also contains data from A:Z, but the data is different. The third page is used to query the other by using Filter.
I want to be able to write a single formulate that will Filter data from sheet 1 and display the results, and then starting from the next available row, Filter data from sheet2 and display those results, without any gaps or overwriting.
If the two filters are something like this:
=FILTER(sheet1!A:Z, sheet1!A:A="Bob")
and
=FILTER(sheet2!A:Z, sheet2!A:A="Bob")
So, the only difference between the two filters is the sheet name.
I won't know in advance how many rows of output each filter will produce, so I can't just put the second Filter formula in a cell in a lower row, because I don't want there to be any blanks rows between the two sets of output.
I thought that I could use COUNT or COUNTA to work out how many rows of output there's be from the first Filter formula, but even knowing that, how could I choose in which row the second Filter would begin displaying results?
I thought of using &, but that just puts the first result from each sheet into one cell and gives no other rows or columns of results.
I tried ARRAYFORMULA, but I've never used that before and don't think it is suitable.
I tried JOIN, but it gives an error saying it can only be used for a single row or a single column.
UPDATE: Here's my new code, based on player0's advice
={IFERROR(FILTER('2019 (H904)'!B9:AK, '2019 (H904)'!C9:C=A1, '2019
(H904)'!J9:J=E1), SUBSTITUTE(COLUMN(B9:AK)^0, 1, );
IFERROR(FILTER('2018 (H517)'!B9:AK, '2018 (H517)'!C9:C=A1, '2018
(H517)'!J9:J=E1), SUBSTITUTE(COLUMN(B9:AK)^0, 1, )}))
But I get a formula parse error.
put them in array:
={FILTER(sheet1!A:Z, sheet1!A:A="Bob");
FILTER(sheet2!A:Z, sheet2!A:A="Bob")}
the issue is if one of them outputs nothing. then you need something like this:
=ARRAYFORMULA(QUERY({
IFERROR(FILTER(sheet1!A:Z, sheet1!A:A="Bob"), SUBSTITUTE(COLUMN(A:Z)^0, 1, );
IFERROR(FILTER(sheet2!A:Z, sheet2!A:A="Bob"), SUBSTITUTE(COLUMN(A:Z)^0, 1, )},
"where Col1 is not null", 0))

Google Sheets linking two sheets using a primary key

I have two sheets that I want to link using a "primary key". At the moment, I have imported from sheet1 into sheet2 some columns using the function
=Sheet1!A1 (for the first cell for instance). My purpose is to complete the data related to each imported line in sheet2. However, sheet1 is shared with other people, thus they can modify the content of a line without deleting or modifying the data I have added in sheet2 (and that doesn't exist in sheet1).
Given the fact that my table has a column 'id' that can be considered as a primary key, how can I add new data in sheet2 in so far as it will be related to the 'id' rather than the position of the line (so if I ever change an id in sheet1 the data I added will be deleted or if I move an id to another line, all the data will be moved too)?
you can use VLOOKUP and bring over data based on ID, like:
=ARRAYFORMULA(IFERROR(VLOOKUP(D:D, Sheet1!A1:B, 2, 0),))
for more columns add them in an array:
=ARRAYFORMULA(IFERROR(VLOOKUP(D:D, Sheet1!A1:B, {2,3,4,5}, 0),))
demo spreadsheet
I like player0's answer, but I found this easier to understand:
=QUERY(data_to_search,"select column_you_want_to_show where other_sheets_primary_key_column='"&this_sheets_primary_key_column&"'")
e.g.
=QUERY(Sheet4!$A$2:$F$10,"select F where A='"&$A2&"'")

Resources