Fetching data from other Spreadsheet by comparing some values - google-sheets

I have a sample Spreadsheet such as
And I want to import data from this spreadsheet to another spreadsheet, but after comparing some data, such as
My noob formula for the selected cell name in the second image:
=Query(ImportRange("URL of first spreadsheet","sheet!B:D"),"Select Col2 where Col2=A.....(what to add here? this column 'A' is not working for comparing the names)
My Approach:
I will compare the values from the import range with the existing column i.e. previousSpreadsheet.name.A=thisSpreadsheet.this_name.A and compare every name and SELECT values accordingly for the next three columns (in yellow). I hope I'm clear.
What should I add here? Is my approach right? If not, What are the alternatives to achieve this?
Thanks.

EDIT
(following OP's request)
Also, can I compare two cols with one? such as where LOWER(Col2)='"&LOWER(A2)&"' or '"&F2&"' or Col2=A2 || F2 something like this? for comparing two cols with one?
If you start using more than one names as variables, you may want to consider using other alternatives for the where clause, like matches
=Query(ImportRange("URL of first spreadsheet","sheet!B:D"),
"Select Col2 where Lower(Col2) matches'"&LOWER({A2&"|"&F2})&"'")
Original answer
Please use
=Query(ImportRange("URL of first spreadsheet","sheet!B:D"),"Select Col2 where Col2='"&A2&"'")
(where A2 is the name you need)
Pay notice to the syntax referring the cell: single quotes ' double quotes" ampersand & cell A2 and again &"'.
No spaces in between

try:
=ARRAYFORMULA(IFNA(VLOOKUP(A2:A, IMPORTRANGE("ID", "sheet!B:D"), {1,2,3}, 0)))

Related

Sumif + Index/Match

Essentially I am trying to find the sum in column A that corresponds to names distributed in the array to the right. Below is a picture of what I am trying to do and what my formula is as of now. Please let me know either how to modify my formula or how to restructure my data.
enter image description here
use:
=INDEX(TRANSPOSE(QUERY(SPLIT(FLATTEN(IF(B2:G14="",,B2:G14&"×"&A2:A14)), "×"),
"select Col1,sum(Col2) where Col2 is not null group by Col1 label sum(Col2)'Totals'")))
Here is a formula I came up with that can accomplish this:
=sum(filter($A$2:$A$11, arrayformula(REGEXmatch(ARRAYFORMULA((IF(ROW($A$2:$A$11)=1,"formulaTest",$B$2:$B$11 & $C$2:$C$11 & $D$2:$D$11 & $E$2:$E$11 & $F$2:$F$11))),B13))=TRUE))
This formula should be placed in the cell below Name1, next to Totals, and then copied across the row under each name. The cell references should be changed to match your sheet/needs. Anything with $ before it defines either the duration column, or each column that contains the names. The last cell reference (B13) is a reference to the cell that contains the name you are looking to total. A screenshot of an example of this is shown at the bottom of this answer.
In simple terms, this formula combines the names from each row into a single string (per row), searches for each name within that combined string, filters the duration value if there is a match, and then sums that value.
Hope this helped! Let me know if there is anything I should explain further or better clarify.

IF cell contains, THEN return certain value with more values and return possibilities

I have a sheet with the following columns:
Column 1: contains text of the form "TS001", "TS002", "DR001", "MS002" etc.
The 2 letter in the beginning are a code for the manufacturer name, so for example "MS=Microsoft".
For the second column, I would like to have a formula that goes through the first column and searches for those letters, in order to then return the complete name of the manufacturer.
For example, it should look something like this:
Column 1
Column 2
MS001
Microsoft
TS002
Tesco
DR001
DR. Pepper
TS003
Tesco
Is something like that possible?
Thank you very much!
When you say "MS=Microsoft" it implies somewhere you have a table with that reference. For the purposes of the following example I created a sheet named ReferenceTable where column A contains the two letter code, and column B contains the name of the company. So it looks like this:
A
B
MS
Microsoft
TS
Tesco
And now in the main sheet in column B you would write the following formula:
=ARRAYFORMULA(VLOOKUP(MID(A1:A,1,2),ReferenceTable!A1:B,2,FALSE))
This will give you the name of the company, looked up from the reference table.
The array formula is there so that you only have to put this formula in cell B1, and assumes you will use the ReferenceTable sheet as a list; that way as you add records to Column A Column B is populated by the arrayformula in B1.
I'd simply use a Reference Table and a VLOOKUP formula
If cell B7 contains "MS0001"
the following formula will attempt to match just the first two letters again a reference table located in cells O7:P9
=VLOOKUP(MID(B7,1,2),O7:P9,2,FALSE)
and will return "Microsoft" when it finds "MS"
In order to achieve what you want, somewhere you need to have a list of the two letter codes and the corresponding company name.
As with all vba, there’s any number of ways to do this, but I would probably put the two letter code and company data into an array, then iterate through col1 to create the desired output for col2.
E.g below assumes the two letter code and company names are in col3 and col4 respectively, but you can change it to wherever they’re located.
Sub CompName()
Dim Cmpname () as string
Dim col1 as range, rng as range
Cmpname = range(range(“C1”), range(“D1048576”).end(xlup))
Set col1 = range(range(“A1”), range(“A1048576”).end(xlup))
For each rng in col1
For i = lbound(Cmpname, 1) to ubound(Cmpname, 1)
If left(rng, 2) = Cmpname(i, lbound(Cmpname, 2)) then
rng.offset(0,1) = Cmpname(i, ubound(Cmpname, 2))
Exit For
End if
Next
Next
End Sub
I’ve admittedly just written this on my phone and have not tested it, but hopefully there’s minimal mistakes.
I just reread your question and realized that you may actually want a formula rather than vba code.
If this is correct, using an INDEX MATCH is probably your best bet.
In this example I’ll assume the same setup as described above - col3 has company codes and col4 has company name - and this formula can be inserted into cell B1:
=index(D:D,match(left(A1,2),C:C,0))
You can then just filldown for the rest of the entries in col2.
Again, done from memory without testing so hopefully got it right.

Can de search key of MATCH be a row of values?

I am would like to use the values in the name column (search key) and look them up in a table with the headers [name, regular, overtime].
The formula I am using is:
=ArrayFormula(index(rateTable, match(formName,workers,0),match(formType,rateTypes,0),0)*{formHours})
It works except the search key in the MATCH formula, does not reference every respective entry in the name column, but only works with one fixed reference to a cell.
Am I trying to use this formula beyond its capabilities? I'd like to know if I should stop searching. Thanks.
Here is a link to a copy of my sheet:
https://docs.google.com/spreadsheets/d/1sovuTB4zSTpl0RUHciYltr-pf8g4KPTgkYVE5lB4Ifk/edit?usp=sharing
=ArrayFormula(index(rateTable, match(formName,workers,0),match(formType,rateTypes,0),0)*{formHours})
for this task is best if you use VLOOKUP formula. paste in I2 cell:
=ARRAYFORMULA(IF(LEN(formName), IFERROR(VLOOKUP(formName, rateTable,
IFS(formType="Regular", 2,
formType="Overtime", 3), 0)) * formHours, ))
demo spreadsheet
INDEX doesn't work with arrays as needed for ARRAYFORMULA. Either…
use copied formulas rather than a single ARRAYFORMULA, e.g.
=IF(NOT(LEN(A2)),"",index(rateTable, match(C2,workers,0),match(G2,rateTypes,0))*H2)
and drag-copy it down column I,
or refactor your INDEX(,MATCH( use to rely instead on VLOOKUPs and HLOOKUPs.

How to refer to column 'BY' in query?

In my Google spreadsheet, i'm using the query function to get data from one sheet onto another. The query looks something like this:
=QUERY('mySheet'!$A$1:100,"select F where "&C$3&"='myValue'")
This works fine until cell C3 has value "BY" (because the word "by" has significance in the query language). I've tried using single quotes, but then the query uses header "BY" instead of column BY and it returns an empty result.
Any ideas on how to work around this?
Put it in backticks.
=sum(QUERY(select `BY` where `BY` is not null limit 7))
This will sum the first 7 values in column BY.
(This was fun to debug. The formula worked in every other column...)
"BY" is a special word. It is present in clause group by
You may use this:
=QUERY({'mySheet'!$A$1:100},"select Col5 where Col"&C$3&"='myValue'", 0)
and paste the column number into C$3
see more info here
BTW you may use function column() to know BY is column 77 or find the right number by header name: =match("column name", 'mySheet'!1:1, 0)

Importrange() where the source columns/rows change

I have an importrange("key", "sheet1!D" & targetRow) formula, but I also need the column of the importrange() to be dynamic too in case I add/delete columns in the source data:
E.g. importrange("key", "sheet1!" &targetColumn &targetRow)
I researched query() language but being forced to use Col1, Col2 etc instead of named column identifiers makes this useless for what I'm trying to achieve.
Can someone help me with this? Easiest way to get column letters without a script? Thanks very much.
I've figured out a somewhat inelegant workaround:
=IMPORTRANGE("key", "Sheet!" & IMPORTRANGE("key", "Dept") & targetRow)
Where "Dept" is a single-cell named range that contains the column letter of the column I want.
The column letter (e.g. 'K') is the result of the following formula:
=substitute(address(row(K6), column(K6),4), row(K6),"")
Is there really no easier/more robust way of doing this? This will be used to calculate bonuses so it's actually a very critical spreadsheet.

Resources