Count values in another sheet in Google Spreadsheet where sheet name is a cell value using ARRAYFORMULA - google-sheets

I have a Google Spreadsheet with multiple sheets. I cannot share a sample because this is G-Suite Enterprise and it is locked. If absolutely needed I can create a dummy one on my personal Gmail.
One of the sheets has a list of lookup values and sheet names. I am trying to come up with an ARRAYFORMULA that will count the # of times Lookup Value shows in column A of Lookup Sheet Name.
| Lookup Value | Lookup Sheet Name | Count |
|--------------|-------------------|-------|
| one | Primary | ... |
| two | Secondary | ... |
| three | Stuff | ... |
| ... | Primary | ... |
| ... | ... | ... |
I came up with this formula For C2 but it does not work. I don't exactly know why but it shows 1 for every lookup value or just one row with 1.
=ArrayFormula(IF(A2:A <> "", COUNTIF(INDIRECT(B2:B & "!A2:A"), A2:A), ))
I do not want to use Google Apps Script to solve this.

Here is one possible solution that combines the values of the different sheets in one by adding a second column with constant value (the name of the seet for example).
=ArrayFormula(
IF(A:A<>"",
COUNTIFS(
{
FILTER('sheet A'!A:A,NOT(ISBLANK('sheet A'!A:A)));
FILTER('sheet B'!A:A,NOT(ISBLANK('sheet B'!A:A)))
},"="&A:A,
{
FILTER(IF(ISBLANK('sheet A'!A:A),,"sheet A"),NOT(ISBLANK('sheet A'!A:A)));
FILTER(IF(ISBLANK('sheet B'!A:A),,"sheet B"),NOT(ISBLANK('sheet B'!A:A)))
},"="&B:B
),
)
)

Related

Sorting data on google sheets

Looking for a bit of help creating a small database for orders.
So currently I'm parsing orders I receive to a order list on google sheets.
I have a few columns like customer name, date, reference number, address, etc.
Trying to have 1 main order sheet with all parsed orders, then have orders sorted by month in separate sheets and removing duplicate order entries.
You can write the script in given documentation for removing duplicates:
https://developers.google.com/apps-script/articles/removing_duplicates?hl=zh-TW
And For sorting of order:
var ss = SpreadsheetApp.getActiveSpreadsheet();//create object for google spreadsheet.
var timeSheet= ss.getSheetByName("sheetName");//create object for particuler sheet
var timeSheetLastRow = timeSheet.getLastRow();
timeSheet.getRange("A3:BC"+timeSheetLastRow).sort(7);// 7 is column number
You can try those things merge code for your sheet.
Let's create a dummy data that matches your content in Google Sheet.
A | B | C | D
-------------------------------------------------------
customer name | date | reference number | address
ABC | 1-1-1990 | 00100 | Home Towm
EFG | 3-2-1991 | 00200 | Farm Towm
HIJ | 2-3-1990 | 00300 | Your Towm
ABC | 1-1-1990 | 00100 | Home Towm
Let's assume the data above is added manually, and there is another sheet in the same SpreadSheet where sorted & unique data will be added from above sheet.
Formula: Unique(Sort('Sheet Name'!A:D, sort_column, is_ascending))
Example: Unique(Sort('Sheet Name'!A:D, 2, True))

Use named range variable name dynamically in Google sheets

I am new to Google sheets.
I have one sheet with multiple named ranges. Each range is named with "company_{{ id }}" e.g. "company_snapchat".
I have list of company ids as follows:
|---------------------|------------------|
| Company ID | Sum of Costs |
|---------------------|------------------|
| snapchat | <formula> |
|---------------------|------------------|
| facebook | <formula> |
|---------------------|------------------|
In the formula I want to reference its named range as "company_"&a2. However it doesn't work.
Use INDIRECT() to refer value in a cell as cell reference
=INDIRECT("company_"&A2)
Normally we work with A1 or R1C1 but also work with named range too

Google Sheets: How to eliminate duplicates in some columns and show only the most recent data in others?

I have a spreadsheet of books, with one row for every time a book was checked out (this is a small classroom library). Here are the columns:
BookTitle | Author | DateCheckedOut | CheckedOutBy | Status
=========================================================================
The BFG | Dahl, Roald | 6/1/2016 | Suzy | Out
The BFG | Dahl, Roald | 4/5/2016 | Johnny | Returned
The BFG | Dahl, Roald | 12/4/2015 | Wendy | Returned
Charlotte's Web | White, E.B. | | | Added
Wonder | Palacio, R.J. | 5/29/2016 | Joey | Returned
Wonder | Palacio, R.J. | 3/21/2016 | Mary | Returned
I want to query it to get only the row with the highest date value for each book and then display all columns of that row except CheckedOutBy.
I wanted to get a list of unique book title / author combinations and then join it with the original table the way I would in DB2, but it seems that joins like that are not possible in Google Sheets. I tried grouping and the max function, but when I get those things to work I either haven't been able to eliminate earlier dates or haven't been able to display columns that aren't being used in the aggregate function. My Google Sheets querying skills are not up to par :/
Is there a simple way to do this that I'm missing? I would appreciate any tips.
Here's a copy of that sample data from above in a Google Sheet.:
https://docs.google.com/spreadsheets/d/1J384S0fsc8tgxVMehPb_uyRNc5-6cQx-xKN-q8K8Gds/edit?usp=sharing
I created a new sheet and entered in cell A1
=ArrayFormula(iferror(vlookup(unique(Sheet1!A2:A), sort(Sheet1!A2:E, 3, 0), {1, 2, 3, 5}, 0)))
See if that works for you ?
BREAKDOWN:
The general idea behind the formula is to make use of the fact that VLOOKUP only returns the first match. We want that 'first match' to be the latest date per book.
So first we sort the table so that the latest dates are on top.
We 'lookup' the unique book titles in that sorted table and we return the columns {1, 2, 3, 5}.
Links:
sort() function
vlookup() function

Structuring a query between multiple tabs to join values by name

I'm trying to write a SQL query in Google Sheets to try and get data for "matching" results from two different tabs, but running into some trouble.
This is a sheet that's basically an automated scoring engine for instructors who take a two-part test (written and practical). After the results are entered, I'd like to use some SQL to take the results from the two tabs and collate them into a final score.
Link to the sheet in question.
There's a "Practical Scores" tab (which takes all the data from the associated Google Form), and a "Written Scores" tab. I'd like to get the name of the instructors who match in both those tabs, and give the associated score for them, but I'm mostly having trouble with writing the correct SQL.
Most of what I'm trying to do is working fine. I'm able to pull the final practical scores via the following SQL:
=query(PracticalScores!A2:E, "select A, count(E),SUM(E)/3 group by A")
I can also pull the written scores as follows:
=query('Written Scores'!B2:C,"select B,C")
But I want the intersection of the two as well, and that's where I'm running into problems.
=query(A8:E, "select A,C,D where A = E")
will simply return the rows where the names match up, and I want the instances where the names match up, regardless of whether the rows do.
That is, I want all the rows where the names match from tab 1 to tab 2 and not just the few rows that happen to line up perfectly.
If I'm not explaining this well, please let me know and I can provide additional information. Any assistance would be very greatly appreciated!
Since the query function does not support joins, this can't all be done in one query. Instead, the following device can be used:
=arrayformula(vlookup(name column, table, # of column to extract, False))
For example, suppose I have a table
+---+-------+---+
| | A | B |
+---+-------+---+
| 2 | Jim | 3 |
| 3 | Sarah | 4 |
| 4 | Bob | 5 |
+---+-------+---+
to which I want to add another column, taking it from
+---+-------+---+
| | E | F |
+---+-------+---+
| 2 | Sarah | 9 |
| 3 | Bob | 8 |
| 4 | Jim | 7 |
+---+-------+---+
The basic idea is to put in cell C2 the formula
=arrayformula(vlookup(A2:A, E2:F, 2, false))
which will look up every name from first table (column A) in the column E, and return the matching value in column F. Result:
+---+-------+---+---+
| | A | B | C |
+---+-------+---+---+
| 2 | Jim | 3 | 7 |
| 3 | Sarah | 4 | 9 |
| 4 | Bob | 5 | 8 |
+---+-------+---+---+
In practice, one should filter out empty lookup values to improve performance:
=arrayformula(vlookup(filter(A2:A, len(A2:A)), E2:F, 2, false))
If the second table contains some names not present in the first, they will not be returned by the above formula. In this case it is better to prepare a full list of names, for example with
=sort(unique({Sheet1!A2:A; Sheet2!A2:A}))
which collects the names from A columns of two sheets, eliminating duplicates and sorting. Then look up those using vlookup as above.

Trying to loop through all cells that start with the contents of another cell using ARRAYFORMULA and RegExMatch

I have a Google Spreadsheet setup; it's for a product import. I am trying to loop through all rows, when C2:C == "configurable" I want it to join (with a comma) all B2:B cells that start with the contents of B in the current "configurable" row.
For example, the table may look like this.
Name | SKU | Type | Simple Products
Prod 1 | 001 | configurable | 001.01,001.02
Prod 2 | 001.01 | simple |
Prod 3 | 001.02 | simple |
Prod 4 | 002 | configurable | 002.01,002.02,002.03
Prod 5 | 002.01 | simple |
Prod 6 | 002.02 | simple |
Prod 7 | 002.03 | simple |
The "Simple Products" column (D2) is where the formula would be located. It will scan each row, if C* = configurable, then it will use the B* value to find all "simple" SKUs that start with the value in B*.
I have it almost working, using the following formula:
=ARRAYFORMULA(
IF(
C3:C="configurable",
join(
",",
transpose(
iferror(
filter(
B3:B,
C3:C="simple",
RegExMatch(B3:B, B3)
)
)
)
),
""
)
)
However, the above always uses B3 for the RegExMatch, so every configurable row contains 001.01,001.02 rather than the row it is currently outputting data in.
Try:
=ArrayFormula(IF(C2:C="configurable",REGEXREPLACE(TRIM(REGEXEXTRACT(QUERY(B2:B,,ROWS(B2:B))&" ","(?:"&B2:B&"\.[0-9]+ )+"))," ",","),))
Although there's a couple of assumptions made based on your sample data (eg the SKUs are sorted).
The issue with your attempt is that the FILTER function can't iterate down the column.
Credit to Isai Alvarado who came up with the QUERY method of building very large strings; larger than the 50k character limit that is normally imposed.

Resources