Join two tables and count values from second table in Google Sheets - google-sheets

I have two Sheets with data like so-
Sheet 1:
A
--------
1 | Name |
2 | sue |
3 | bob |
4 | mary |
5 | john |
Sheet 2:
A B C D
---------------------------------------------
1 | ID | Asignee | Due | Days Left |
2 | ID001 | sue, bob | 1 | 5 |
3 | ID002 | sue, mary | 2 | 8 |
4 | ID003 | bob | 3 | 2 |
5 | ID004 | bob, john | 1 | 9 |
6 | ID005 | bob, mary, john | 4 | 1 |
7 | ID006 | sue, bob | 1 | 8 |
8 | ID007 | john, sue, mary | 2 | 6 |
On a 3rd sheet, I want to join and combine the data to get some totals/counts.
Sheet 3:
A B C D
---------------------------------------------------------
1 | Name | Number Rows | Total Due | Minimum of Days Left |
2 | sue | 4 | 6 | 5 |
3 | bob | 5 | 10 | 1 |
4 | mary | 3 | 8 | 1 |
5 | john | 3 | 7 | 1 |
For the 3rd sheet:
It has the same # of rows and values as Sheet 1
Column Sheet 3!B is the # of rows in Sheet 2 where Sheet 2!B contains Sheet 1!A (or Sheet 3!A)
There are 4 rows in Sheet 2 where Sheet 2!B contain sue
There are 5 rows in Sheet 2 where Sheet 2!B contain bob
There are 3 rows in Sheet 2 where Sheet 2!B contain bob
There are 3 rows in Sheet 2 where Sheet 2!B contain bob
Column Sheet 3!C is the total of Sheet 2!C where Sheet 2!B contains Sheet 1!A (or Sheet 3!A)
Column Sheet 3!D is the smallest value of Sheet 2!D where Sheet 2!B contains Sheet 1!A (or Sheet 3!A)
I've been staring at a blank sheet and am not sure where to start. I think I have to use filter, and arrayformula but I'm not sure how or where to start.

=ARRAYFORMULA(QUERY(SPLIT(TRIM(TRANSPOSE(SPLIT(TEXTJOIN("♀", 1,
IF(IFERROR(SPLIT(B2:B, ","))<>"",
SPLIT(B2:B, ",")&"♦"&C2:C&"♦"&D2:D, )), "♀"))), "♦"),
"select Col1,count(Col1),sum(Col2),min(Col3)
group by Col1
label count(Col1)'',sum(Col2)'',min(Col3)''"))
Edit by #IMtheNachoMan to add details on why/how I think the above formula works:
split the values in column B and concatenate the values in column C and column D with an arbritary value that is assured to not be used in any of the columns
because everything is wrapped in an arrayformula, each value from the column B split will get concatenated
splitting column B will create an errror for rows that don't have a value in column B
so the if and iferror will check if the split will create an error and if it does it will return null instead of the concatenated string from the first bullet
at this point we have one row for each row in the source table with column B split and concatenated with column C and D
join all the rows using a second arbritary value that is assured not to be in any of the columns
be sure to ignore empty values
empty values will be there from the rows that didn't have any values in the split from the first bullet
split the joined data (that doesn't have empty rows cause of the previous bullet) on the 2nd arbritary value that was used
transpose it back into rows
trim each row to remove spaces (not sure how/where the spaces got added though)
split the column in each row with the first arbritary value
use this as the input for a query call and use aggregate functions to get the data we want
if you really need to preserve order do:
=ARRAYFORMULA(IFERROR(VLOOKUP(Sheet1!A2:A,
QUERY(SPLIT(TRIM(TRANSPOSE(SPLIT(TEXTJOIN("♀", 1,
IF(IFERROR(SPLIT(B2:B, ","))<>"",
SPLIT(B2:B, ",")&"♦"&C2:C&"♦"&D2:D, )), "♀"))), "♦"),
"select Col1,count(Col1),sum(Col2),min(Col3)
group by Col1
label count(Col1)'',sum(Col2)'',min(Col3)''"), {1, 2, 3, 4}, 0)))

Related

How to get the cell value of an intersection between 2 named ranges in Google Spreadsheet

I am trying to get a value of an intersection of 2 named ranges.
Been searching for a solution for a while now, but can't seem to find any.
So lets say I have a table like this:
# Col1 Col2 Col3 Col4
Row1 | 1 | 6 | 3 | 4 |
Row2 | 6 | 7 | 4 | 2 |
Row3 | 4 | 13 | 9 | 12 |
Row4 | 12 | 3 | 18 | 14 |
These rows and columns are named ranges so Col1 = Sheet!B:B and Row1 = Sheet!2:2 etc.
Now how do I get the value from Col2 Row3 for example?
I have tried =Col2 Row3 and vice versa but it doesn't seem to work that way.
Edit: Here's an example of what I'm trying to do
Thanks in advance,
Dave
the baseline would be:
=INDIRECT(ADDRESS(3, 2))
where row and column can be easily substituted with formulas
Use INDEX/MATCH :
=INDEX(A:E,MATCH(B11,A:A,0),MATCH(B10,1:1,0))

Google Sheets get an average value from unique values in a single column

On Google Sheets, I want to find an average for all values in rows that contain a unique value in one column.
For example, I have a sheet like this:
Dog | 2
Dog | 1
Dog | 4
Cat | 3
Rat | 2
Cat | 1
And I want to create another table below it for the average value of each unique value in the row:
Dog | 2.33
Cat | 2
Rat | 2
You can use
=QUERY(Data!A1:B7;"SELECT A, AVG(B) GROUP BY A")
assuming your data (containing titles) are on the A and B columns of the sheet.

Return a list of all pairs row/column if condition of cell value is met

Given a Google Sheet table similar to the following:
+---+---A--+---B--+---C--+---D--+
| 1 | | col1 | col2 | col3 |
|---+------+------+------+------+
| 2 | row1 | 1 | X | 45 |
| 3 | row2 | 5 | | |
| 4 | row3 | 4 | | 34 |
+---+------+------+------+------+
where row1, col1 are header labels and "X" is also a valid value for the combination "row/column";
I need to retrieve a list of all the possible combinations row/column headers that are not null, meaning in this example:
row1 | col1
row2 | col1
row3 | col1
row1 | col2
row1 | col3
row3 | col3
I tried in different ways such as using the ISBLANK function or the QUERY one as:
=QUERY(A1:D4, "SELECT A,B,C,D WHERE B IS NOT NULL OR C IS NOT NULL OR D IS NOT NULL",1)
but is simply a subset of the precedent table AND I cannot GROUP BY because there's no aggregate function;
Consider a spreadsheet with 3 sheets, "Main", "LabelTable", and "List".
"Main" contains the data:
+---+---A--+---B--+---C--+---D--+
| 1 | | col1 | col2 | col3 |
|---+------+------+------+------+
| 2 | row1 | 1 | X | 45 |
| 3 | row2 | 5 | | |
| 4 | row3 | 4 | | 34 |
+---+------+------+------+------+
A named range from A2:D4 is created and named "Data".
My solution was to first make an accompanying table on "LabelTable" of the form:
+-------+---A--+------B-----+------C-----+------D-----+
| 1 | | col1 | col2 | col3 |
|-------+------+------------+------------+------------+
| 2 | row1 | row1 col1; | row1 col2; | row1 col3; |
| 3 | row2 | row2 col1; | row2 col2; | row2 col3; |
| 4 | row3 | row3 col1; | row3 col2; | row3 col3; |
+-------+------+------------+------------+------------+
where the number of rows and columns matches the size of the table on "Main".
If you have a large table, you can easily generate these by using the formula in B2 as =$A2&","&B$1&";" and copying across and down to the bottom right of your required range.
I then created a named range "Labels" from B2:D4 (or for larger tables, the full extent of the contents of this table)
On "List", I then entered the formula:
=transpose(split(concatenate(arrayformula(if(not(ISBLANK(Data)),Labels,""))),";"))
This produces a list of pairs, like "Row1Col1", corresponding to every value that is not blank.
Description
This uses ISBLANK() to determine if a cell is null or not. In combination with ARRAYFORMULA() on the "outside" this creates a table of TRUE or FALSE values. It then wraps the TRUE/FALSE table with IF() to create a table of labels corresponding to where the TRUE/FALSE table has TRUE. This is where the named range "Labels" comes in - it provides the cell reference of where there was not a null value.
All of this is then wrapped in CONCATENATE() to give a single cell with the references joined. Then SPLIT() is used to turn this into an array - and this is also why ";" was added into the cell references in "Labels". Finally, TRANSPOSE() is used to turn the 1xN array into an Nx1 array, i.e., a single column.
Limitations
If you have a dynamic table then you'll need to also make "Labels" dynamic to match. This method does not do that.
If for some reason there is a requirement that means you can't have the accompanying table on "LabelTable" then this method will not work. Some more will need to be done to "build that in" to the formula.

Google Spreadsheet, for each distinct item in column A, Sum columns B through E

I'm trying to make a spreadsheet for Zelda: Breath of the Wild where I count how many of each item I need to build a bunch of different armor sets.
Here is a link to the spreadsheet I'm working with, read only: https://docs.google.com/spreadsheets/d/161OmMq46BJuXN5KopDFvs7RwRFinKIM8AoKYhiB0ew0/edit?usp=sharing
Column A has a list of material names, and some of them repeat.
Columns B through E have a number indicating how many of the item in Column A is needed.
Each column, B through E, represents a level 1 through 4.
Column F has a =SUM(B{ROWNUM}:E{ROWNUM}
So I can have:
| Level 1 | Level 2 | Level 3 | Level 4 | Total
--------------------------------------------------------
Item one | 5 | 10 | 0 | 0 | 15
--------------------------------------------------------
Item two | 0 | 5 | 8 | 0 | 13
--------------------------------------------------------
Item one | 0 | 0 | 10 | 15 | 25
And what I want is:
| Total
-------------------
Item one | 40
-------------------
Item two | 13
I'm trying now to do it with Google Script, but I've never used it before, so I don't have anything to show for that as of now.
Query should do it. I assume your data is in Sheet1 and query is in another sheet.
=query(Sheet1!A1:F4,"select A, sum(F) group by A label sum(F) 'Total'")

Sort range by order of values in separate column

Given a Google Sheet with the following data:
table 1
| A | B | C
1 | q | w | e
2 | a | s | d
3 | z | x | c
table 2
| A
1 | a
2 | z
3 | q
What formula(s) can sort the rows in 'table 1' according to the stored order of values in 'table 2'?
Edit: How can this be done using a formula in only one cell?
Does this formula work as you want (assuming your tables are named ranges):
=SORT(table1,MATCH(FILTER(A:A,LEN(A:A)),table2,0),1)
See this example Sheet to see it working: https://goo.gl/veSFI4
(1) If you want to sort table 1 as a one-off, add the following in D1 and pull down, then sort on column D:-
=MATCH(A1,table2!A$1:A$3,0)
(2) If you want a table which updates dynamically, in a new sheet (say table 3) enter this in A1 and pull down:-
=table2!A1
and enter this in B1 of the new sheet and pull down and across:-
=INDEX(table1!B$1:B$3,MATCH($A1,table1!$A$1:$A$3,0))
I'm assuming there are no duplicates.

Resources