I want to pull all the values from a particular column in another table. My goal is to take a handful of different tables and put particular columns from each of them into a single, collated table.
For example, let's say I have tables about different kinds of objects
FRUITS
name flavor
banana savory
orange sweet
peach sweet
PETS
name lifespan
dog long
fish short
cat long
Imagine that I now want to make a third table with the name column from fruits and pets.
COLLATED
name source
banana fruits
orange fruits
peach fruits
dog pets
fish pets
cat pets
I tried to install the powerpivot add-in to do this, but I wasn't sure how to do it with a Mac. I'd prefer to use any "table connection" features that Excel offers in case that is possible.
A combination of ideas from both #Ike and #JosWoolley great answers would be this:
=LET(
n,{"Fruits","Pets","Cars"},
w,(tblFruits[Name],tblPets[Name],tblCars[Name]),
y,COUNTA(w),
s,SEQUENCE(AREAS(w)*y,,0),
q,1+QUOTIENT(s,y),
z,CHOOSE({1,2},IFERROR(INDEX(w,1+MOD(s,y),,q),""),INDEX(n,q)),
FILTER(z,INDEX(z,0,1)<>""))
For a new table, the table name would be added to the n variable and the column/range to the w variable without the need to edit the rest of the formula.
Edit #1
Adding more columns can get tricky using this approach but it can be done. For example having an extra 'Price' column in all tables would require something like this:
=LET(
n,{"Fruits","Pets","Cars"},
w,(tblFruits[Name],tblPets[Name],tblCars[Name]),
p,(tblFruits[Price],tblPets[Price],tblCars[Price]),
y,COUNTA(w),
s,SEQUENCE(AREAS(w)*y,,0),
q,1+QUOTIENT(s,y),
z,CHOOSE({1,2,3},IFERROR(INDEX(w,1+MOD(s,y),,q),""),INDEX(n,q),IFERROR(INDEX(p,1+MOD(s,y),,q),"")),
FILTER(z,INDEX(z,0,1)<>""))
where you have an extra p variable and the CHOOSE is updated to reflect the new values. Of course, you could change the order of the columns in the CHOOSE by either changing the order of the 3 parts or by simply changing the numbers in the {1,2,3} array (e.g. {1,3,2}).
=LET(w,(tblFruits[name],tblPets[name],tblCars[name]),x,AREAS(w),y,COUNTA(w),z,IFERROR(INDEX(w,1+MOD(SEQUENCE(x*y,,0),y),,1+INT(SEQUENCE(x*y,,0)/y)),""),FILTER(z,z<>""))
Amend the table column names as required, adding in as many as required.
This should work for reasonably small ranges, though x*y could certainly be improved as a lower bound.
Agreed with Ike that a recursive lambda would probably be of help here.
I added two tables to a sheet: tblFruits and tblPets.
Then you can put the following formula in any cell on the same sheet or another sheet.
=LET(
a,CHOOSE({1,2},tblFruits[name],"Fruits"),
b,CHOOSE({1,2},tblPets[name],"Pets"),
rowIndex,SEQUENCE(ROWS(a) + ROWS(b)),
colIndex,SEQUENCE(1,COLUMNS(a)),
IF(rowIndex<=ROWS(a),
INDEX(a,rowindex,colIndex),
INDEX(b,rowindex-ROWS(a),colIndex)
)
)
The first four rows of the formula are used to retrieve variables that are then used in the final IF-function:
a and b will return "virtual" arrays of each name column plus the "new" column giving the type.
rowIndex returns a single array {1,2,...(number of rows of both tables)}
colIndex returns an array that is build of the number of columns - in this case 2 (name and type)
These variables are used in the IF-formula:
Think of it as a For i = 1 to Ubound(rowIndex)-loop.
If the first value from the rowIndex-Array is smaller than the number of rows of tblFruits,
then INDEX-result is based on virtual array a,
if not the rowindex for b is calculated and INDEX-result is based on virtual array b.
The result is a spill-down array - you can use a filter on it. Just add a header row and add filter.
But you won't be able to create a table based on it.
Therefore you will have to use VBA to create the combined data.
This would be the formula with a third table:
=LET(
a,CHOOSE({1,2},tblFruits[Name],"Fruits"),
b,CHOOSE({1,2},tblPets[name],"Pets"),
c,CHOOSE({1,2},tblRooms[name],"Rooms"),
rowIndex,SEQUENCE(ROWS(a)+ROWS(b)+ROWS(c)),
colIndex,SEQUENCE(1,COLUMNS(a)),
IF(rowIndex<=ROWS(a),
INDEX(a,rowIndex,colIndex),
IF(rowIndex<=ROWS(a) + ROWS(b),
INDEX(b,rowIndex-ROWS(a),colIndex),
INDEX(c,rowIndex-(ROWS(a)+ROWS(b)),colIndex))))
I have the following table:
And I should display the name and salary of an employee whose id = 1020. It seems like an easy question. But I don't know how to do it in Excel or Sheets.
SELECT name, salary FROM Employees
WHERE id = 1020;
That's how you display it in SQL. Could you show me the way in Excel, please?
You can go in different paths here. Imagine you have a table like so:
VLOOKUP Solution
One obvious solution is VLOOKUP:
=VLOOKUP(E2, A1:B6, 2)
After that you would need to add a cell for every column you want to add, changing the third parameter on each cell.
QUERY Solution
But for your case maybe you are more comfortable using the QUERY function that reassembles a SQL.
=QUERY(A1:B6,"select B where A = "&E2&" label B '' ", 1)
As you can see in both cases you have the same result but in the former you can modify it to get averages, max of a column, sum multiple columns and much more.
References
QUERY
VLOOKUP
Query Language Reference
Try this formula. =VLOOKUP(Lookup Value,Lookup Table,Column Number,Match Type)
I am using Excel from Microsoft Office 365 ProPlus.
Here's a really simple data table.
I want to build a Pivot Table around it to look like this...
... except that the "what I want" column (which is the count of items in Column C divided by the count of items in Column B) should be a part of the pivot table.
I have tried all sorts of things using calculated fields, calculations on fields, etc., to add the "what I want" column and just cannot make it work.
Can anyone help?
Calculated Fields only operate on the Sum of the elements in the data tables. Wherever you see a Field Name in the formula for a Calculated Field, picture it as meaning the sum of all elements for that field (that match any other row/column criteria in the Pivot Table).
Putting "= B / C" actually means "= SUM(B) / SUM(C)" for elements of columns B and C that fit that section of the Pivot Table.
The only way to achieve your goal is with two helper columns:
B Count: =COUNT([#B])
C Count: =COUNT([#C])
The sum of these columns then give you the count of columns B and C, so you can use these helper columns to give you what you want:
The Data Field based on the Calculated Field then says "Sum of What U Want", but it will always just be the result of your calculation, even if you change how the field is summarized through Value Field Settings. You can manually rename the Data Field, but it still needs to be different to the Calculated Field name you chose earlier.
Click in the Pivot Table, then go to the Analyze tab, click on Fields, Items, & Sets, then select Calculated Field. Your formula is probably
= B / C
I have a daily CSV file which has a column containing a big list of addresses (about 5000/day) received from a call center, some of those addresses contain the name of the neighborhoods around the city and it's usually in the first 5 words.
in another table i have the exact name of all the neighborhoods in one column and in another column in the same row i have the courier name which provides delivery service in that neighborhood.
I'm looking for a method in power pivot to search each row of the [Address] column & if a value similar to the neighborhood's name found give me the name of the courier of that neighborhood.
This an example for my data sets, the fist sheet describes 5 sample of my daily data, and the second sheet is districts information and their courier ID
Disclaimer - this method doesn't handle for similar words, only exact matches.
The easiest method I found is to make a calculated table. On the Modeling tab, click on New Table and enter this formula.
Results = SELECTCOLUMNS(
FILTER(CROSSJOIN(Addresses, Couriers), SEARCH(Couriers[District], Addresses[Address], , -1) > 0),
"Address", Addresses[Address],
"CourierID", Couriers[Courier ID]
)
This cross joins your two tables and then filters the results down to only where the District from the Couriers table can be found in the Address. As I said in the disclaimer, this is only looking for exact matches.
Then to get that back into the Address table, add a new column with this formula. This will look up the address into the table we just made and return the Courier ID.
CourierID = LOOKUPVALUE(
Results[CourierID],
Results[Address],
Addresses[Address]
)
The reason we need to do this in two steps is because the LOOKUPVALUE function cannot take expressions.