"Google Sheet" ,Compare two columns & return value - sql

I want to do some function like sql joint table.
But I really dont know if google sheet can do it.
I want to compare two columns and return the cost, how can I perform it?
ColA ColB ColG ColH
Type Cost Type Cost
A 100 B
B 200 E
C 300 D
D 400 A
E 500 C
I want colH would show colB data when comparing colG to colA,
is there any function in google sheet that can do it?
It's just like the joint table function sql table.
Thanks

Vertical Lookup function VLOOKUP is what you want to solve this problem.
VLOOKUP(search_key, range, index, [is_sorted])
Pass the following parameters:
search_key is the value to search in the costs array, column G in you case.
range is the costs array
index is the column to return in the costs array, the second one
is_sorted should be set to TRUE, as you do not want incorrect costs to be returned, if the value in column G does not exist in the costs array.
Hence, the formula to use in H2 is:
=VLOOKUP(G2,A1:B6,2,false)
Then, in order to allow you to use the same formula in all cells inside the column H, you need you make sure that it is always A1:B6 which is looked up as the costs array. If you copy paste the formula above from H2 to H3, it will adapt it and move the array one line below (A2:B7), which is not what you want. To solve this, you should use the dollar sign to lock the costs array range. This article explains how it works.
The formula then becomes the one below, and you can copy/paste it inside the whole column H:
=VLOOKUP(G2,$A$1:$B$6,2,false)
Demo sheet with the solution in place: https://docs.google.com/spreadsheets/d/1rzvCmgTi1DgA5Dbwb_iwKYc5RW_9uwFs2sVR0PbN7Ic/edit?usp=sharing

Related

How to use INDEX and MATCH to based to cells?

I have two sheets of data in excel. I want to combine cells to bring the second cell value. Here is an example:
when I enter combining columns name and list must get value form V1 and V2.like this:
I don't have the time to type your data, so this is a rare case of a non-tested formula and may need some tweaking.
For the V1 result: (top screenshot starts in Sheet1 column A, bottom screenshot in Sheet2, column A). This formula goes into cell B2 in Sheet 2:
=Index( Sheet1!$E$2:$E$100, MATCH(Sheet2!a2,INDEX(Sheet1!$A$1:$A$100&" | "&Sheet1!$D$1:$D$100,0),0))
Adjust the first index range from column E to F for the V2 result in C2.
Copy down.
Don't use that approach with whole columns. It will be slow. Use ranges with defined rows.
Of course, if the codes in Sheet1 column A are unique, you don't need to combine them with the value in column D and can look up only the first three characters, like this for V1 (use 6 for V2)
=vlookup(Left(Sheet2!A2,3),Sheet1!$A:$F,5,0)

VBA excel Copy Paste

Hi, I am totally new to Excel VBA. Firstly, I want to copy the data when the condition is met(copy data with reference to 144)
Secondly, compare the cells, if it is IT Operations(Table1) to IT Operations(Table2) then copy the price(money) to column F. If the variable is no there then leave blank.
This can be done with formulas. Here is one way of thinking about filling column F, with the prices for the matching items in column E, by matching the number given in the last row in E (144 Total); which i shall assume is E10 in this case.
Total formula in F1 which you then drag down is:
=IFERROR(IFERROR(VLOOKUP(E1,INDIRECT(CELL("address",OFFSET($H$1,MATCH(1*LEFT($E$10,FIND(" ",TRIM($E$10),1)-1),$G:$G,0)-1,,1,1))&":"&CELL("address",OFFSET($I$1,MATCH($E$10,$G:$G,0)-1,,1,1))),2,FALSE),VLOOKUP(E1,G:I,3,FALSE)),"")
In steps:
Extract the number of interest e.g. 144, and get rid of any trailing/leading whitespace using:
LEFT($E$10,FIND(" ",TRIM($E$10),1)-1)
Find which row this value is in as this will be the first row of the lookup range for this number. *1 converts text to a number.
MATCH(1*LEFT($E$10,FIND(" ",TRIM($E$10),1)-1),$G:$G,0)
This gives row 9.
We can use something simpler to find the last row of the range, which holds 144 Total
MATCH($E$10,$G:$G,0)
This gives row 15. So we know the data lies between rows 9 and 15 for 144.
We can turn this into a range to use in a VLOOKUP with INDIRECT and OFFSET.
=CELL("address",OFFSET($G$1,MATCH(1*LEFT($E$10,FIND(" ",TRIM($E$10),1)-1),$G:$G,0)-1,,1,1))&":"&CELL("address",OFFSET($H$1,MATCH($E$10,$G:$G,0)-1,,1,1))
This gives us $G$9:$H$15. Note adjustments of -1, to put OFFSET back in the right row, and that the OFFSET start cells are in different columns to provide the columns required for the VLOOKUP.
So we can now lookup column E values e.g. Enhancement, in our newly defined range which is accessed via INDIRECT:
=VLOOKUP(E1,INDIRECT(CELL("address",OFFSET($H$1,MATCH(1*LEFT($E$10,FIND(" ",TRIM($E$10),1)-1),$G:$G,0)-1,,1,1))&":"&CELL("address",OFFSET($I$1,MATCH($E$10,$G:$G,0)-1,,1,1))),2,FALSE)
This is saying VLOOKUP(E1,$G$9:$H$15,2,FALSE) i.e. get the price column from the range for the item specified in E1.
If this is not found i.e. returns #N/A, we can use this to first check if this is because of the merged cell that holds the 144 Total; where the value is actually in column G not H, and use an IFERROR to say, if not found in $G$9:$H$15 then try for a match using columns G:I and return column 3.
Which with pseudo formula, using priorLookup as placeholder, for the formula described in the steps above, looks like:
IFERROR(priorLookup, VLOOKUP(E1,G:I,3,FALSE))
If this still returns #N/A, we know the value is not present and we should return "". This we can handle this with another IFERROR:
IFERROR(IFERROR(priorLookup, VLOOKUP(E1,G:I,3,FALSE)),"")
So giving us the entire formula stated at the start.
Here it is used in the sheet:

Find value in column, based on 2 criteria

I have a file with 3 columns. Column A contains 300,000 rows, with about 200 separate IDs, all duplicated at least 1,000 times. Column B contains the date for each of the rows. Column C contains the values that I need to extract.
Each of the 200 IDs in Col A can have multiple values (e.g. ID 1234 might have dates 1/1/2001, 1/3/2001, 1/2/2015, etc). Similarly, each date on Col B will have multiple IDs (e.g. 1/2/15 might have IDs of 1234, 1874, 1930, 6043, etc).
In a nutshell, I need to check the values in Col A and Col B to find the relevant ID in Col A and the maximum value in Col B, and return the value in the relevant cell in Col C.
I've looked at Index/Match examples, but they don't seem to be suitable. Is there any suggestions on a macro I could run, that would accomplish what is needed.
Use this array formula:
=INDEX($C$1:$C$300000,MATCH(1,IF(($A$1:$A$300000="1234")*($B$1:$B$300000=MAX(IF($A$1:$A$300000="1234",$B$1:$B$300000))),1,0),0))
Being an array formula it must be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.
Change the "1234" to a reference cell with the appropriate ID.
You can accomplish this using array formulas. To start, you can retrieve the maximum date in column B when column A is 1234 using the below formula. Keep in mind that you have to use Ctrl-Shift-Enter when you finish typing an array formula.
{=MAX(IF($A$2:$A$24=1234,$B$2:$B$24))}
Note that you will need to change the ranges to include all of your data, rather than my test data on rows 2-24.
Now that you have a formula to retrieve the max date, you can put that inside an index/match and, again using Ctrl-Shift-Enter, use the below array formula to retrieve the value in column C for a row matching 1234 and the maximum date.
{=INDEX($C$2:$C$24,MATCH(1234&MAX(IF($A$2:$A$24=1234,$B$2:$B$24)),$A$2:$A$24&$B$2:$B$24,0))}

Filter if one or another value is equal to cell

I need to solve one simple problem. I do have a one huge table that contains around 300000 rows with data and basically what I need is to filter out row if the cell in either G or L row is equal to cell in 'Input sheet'!C2.
Please see attached image.
So if the either G column or L column is containing value from cell then I want to copy entire row into sheet called 'Direct'. The 'Direct' will not contain empty rows.
As It is huge amount of data, that will grow a bit the code must be a bit optimized.
Many thanks in advance!!!
Cheers,
F.

Google spreadsheet "=QUERY" join() equivalent function?

This question is concerning joining two databases in Google spreadsheet using =QUERY function
I have a table like so in range A1:C3
a d g
b e h
c f i
I have another table
c j m
a k n
b l o
I want the final table to look like this
a d g k n
b e h l o
c f i j m
I can do this by using a vlookup function pretty easily in cell D1 and paste it down and across, but my dataset is huge. I would need a whole page of vlookups and Google Spreadsheet tells I'm at my limit in complexities.
I look at the Google's Query Language reference... there doesn't seem to be an type of "join" functions mentioned. You would think it would be an easy "join on A" type operation.
Can anybody solves this without a vlookup?
Short answer
Google QUERY Language version 0.7 (2016) doesn't include a JOIN (LEFT JOIN) operator but this could be achieved by using an array formula which result could be used as input for the QUERY function or for other uses.
Explanation
Array formulas and the array handling features of Google Sheets make possible to make a JOIN between two simple tables. In order to make easier to read, the proposed formula use named ranges instead of range references.
Named Ranges
table1 : Sheet1!A1:C3
table2 : Sheet2!A1:C3
ID : Sheet1!A1:A3
Formula
=ArrayFormula(
{
table1,
vlookup(ID,table2,COLUMN(Indirect("R1C2:R1C"&COLUMNS(table2),0)),0)
}
)
Remarks:
Using open ended ranges is possible but this could make the spreadsheet slower.
To speed up the recalculation time :
Replace Indirect("R1C2:R1C"&COLUMNS(table2),0) by an array of constants from 2 to number of columns of table2.
Remove the empty rows from the spreadsheet
Example
See this sheet for an example
Note
On 2017 Google improved the official help article in English about QUERY, QUERY function. It still doesn't include yet topics like this but could be helpful to understand how it works.
So, this answers how you do it WITH a Vlookup-function, but in only one cell.
In your example, given that each table of data has the following cell references:
Table1: Sheet1!A1:C3
a d g
b e h
c f i
Table2: Sheet2!A1:C3
c j m
a k n
b l o
This is how the formula should be constructed.
Join-formula
=ArrayFormula(
{
Sheet1!A1:C,
vlookup(Sheet1!A1:A, {Sheet2!A1:A, Sheet2!B1:C}, {2,3}, false)
}
)
The key to get this formula to work, is to understand how to use curly brackets in the Vlookup Range. You basically define the first cell reference of the Range as the column which is to be a match to the Vlookup Search_Key. The rest of the cell references in the Range is in relation to the columns which you would like to join.
The Index is written as {2,3} to return the second and third column of the Range (the Range consists of a total of 3 columns); curly brackets has nothing to do with Arrayformula in the Vlookup Index, but is necessary to return multiple columns from the Vlookup function. The reason to not write {1,2,3} is because you would not like to include the column which is being used for the purpose of joining.
Example where the column in table2 used for joining, is located in a different column (to the right of the data which is to be joined)
This kind of Join-formula can be utilized even if the join-column in the second table is located as the third column of that table.
Let's say that the raw-data in this example would look like this:
Table1 (Sheet1):
a d g
b e h
c f i
Table2 (Sheet2):
j m c
k n a
l o b
If you write the formula like this, you'll still get the desired outcome (as displayed in the table of joined data):
=ArrayFormula(
{
Sheet1!A1:C,
vlookup(Sheet1!A1:A, {Sheet2!C1:C, Sheet2!A1:B}, {2,3}, false)
}
)
The table of joined data:
a d g k n
b e h l o
c f i j m
In the Join-formula, notice that the third column of Table2 is located as the first cell reference in the Vlookup Range!
The reason to why this works, is because when you use curly brackets in the Range (in conjunction with Arrayformula), the Vlookup Search_Key will NOT look for a column as a common denominator within the raw-data, instead it will use the Array within curly brackets as a reference to find a column as a common denominator (by default this is the first column of the Range).
I've written a comprehensive guide about this topic called:
'Mastering Join-formulas in Google Sheets'
You can use ARRAYFORMULA or YOU can just drag this formula:
after an import or QUERY-ing the first table; in the D column:
=QUERY(Sheet2!A1:C3, "Select B,C WHERE A='" & A1 & "'", 0)
I solved this by using Javascript LINQ (language integrated query).
It lets you specify Javascript with complex join conditions. You can also perform other SQL queries such as Grouping, Projecting, Sorting and Filtering your sheets as if they were database tables. Look at the links below.
Note that in the LINQ query language I replaced all spaces in column names with underscores to make them valid JS identifiers.
https://docs.google.com/spreadsheets/d/1DHtQlQUlo-X_YVfo-Wo-b7315sSk2pxL5ci4Y9lxvZo/edit?usp=sharing
https://script.google.com/d/1R5L2ReHJrBRwyoSoVOFLzEQZiGtxidPfPkAeVownt7SWX6TpacY7gA7j/edit?usp=sharing
If you can map each "index" (a, b, c) to a specific row or column, then you could use the INDEX function.
In this case, you could probably map 'a' to column A (or row 1), 'b' to column B (or row 2), and so on.
Also, Merge Tables seem to address this exact use case.
With the 'other' table in A5:C7, please try:
=query({A1:C3,query(sort(A5:C7,1,TRUE),"Select Col2,Col3")})