modify number from cell reference in QUERY function (Google Sheets) - google-sheets

Is it possible to modify a number from a cell reference within the syntax of a QUERY function in Google Sheets without a helper cell/column?
If I have "5" in a cell, can I reference that cell and somehow subtract 1 when using the LIMIT option in the QUERY syntax so it returns four results?
I am including a sample sheet below as well as working and nonworking formulas.
link to sheet & cell
https://docs.google.com/spreadsheets/d/1lyCK5eIEQYjGFOxh5T_4BxeuDS-1zvMLq80IYn0dc38/edit#gid=1018697126&range=B1
working formula:
=QUERY(INDIRECT(currentTable),"limit "&B1&"",1)
nonworking attempt:
=QUERY(INDIRECT(currentTable),"limit "&B1&"-1",1)

You can just subtract 1 from B1 instead before passing it into the query.
=QUERY(INDIRECT(currentTable),"limit "&B1-1,1)
Output:

Related

Using array formula and countif to lookup based on specific cells

I'm trying to embed a countif into an array formula in Google Sheets. I'm using the countif command to count the number of cells on another page with a given text string, stored in column B. Column B just contains a list of titles. The concatenate portion does a general keyword lookup.
Here is the formula that does not work. It just returns a 0 in each cell, which tells me the countif statement is failing.
=arrayformula(IF(B2:B<>"",COUNTIF(Registrations!C:C,(CONCATENATE("*", B2, "*"))),""))
If I pull out the countif statement, it works fine by itself.
=COUNTIF(Registrations!C:C,(CONCATENATE("*", B2, "*")))
I have even tried referencing B2:B in place of the B2 cell reference, but that does not work. What did I do wrong in the array formula statement?
Have you tried using & instead of the CONCATENATE function and referencing to B2:B in place of only B2?
The formula should look like this:
=arrayformula(IF(B2:B<>"",COUNTIF(Registrations!C:C,("*" & B2:B & "*")),""))
It works for me.
This is my 'Registrations' sheet:
And this is the sheet where the formula is written:

How to reference a different spreadsheet in HLOOKUP?

I try to reference another spreadsheet in the HLOOKUP formula but fail. I'm doing this like:
=HLOOKUP(A2,"https://docs.google.com/spreadsheets/d/1qOzV/","report!A2:B2",2,0)
where http... is the URL of another spreadsheet, report!... is the name of the tab in another spreadsheet and the data range.
This kind of referencing of another spreadsheet's URL works with =importrange("https://docs.google.com/spreadsheets/d/1qOzV/","report!A2:B2"), as I realized from different examples.
But with HLOOKUP while doing so I get an error:
Wrong number of arguments to HLOOKUP. Expected between 3 and 4 arguments, but got 5 arguments.
It seems, that the spreadsheet URL is counted as separate parameter.
How do I correctly reference the URL, the sheet name and the data range?
PS: I tried to chain HLOOKUP and importrange, as mentioned in https://stackoverflow.com/a/39305031/1992004 - but fail on it, got just #REF.
My formula was
=HLOOKUP(A2,IMPORTRANGE("https://docs.google.com/spreadsheets/d/1qOzV"; "report!$A$2:$B$2"),2,0)
you are searching the content of cell A2 against imported range A2:B2 and if the match is found you returning the 1st row (of the whole dataset) of imported range after match eg. that's cell B2 of report!$A$2:$B$2. in other words range A2:B2 is one row so you cant return 2nd row from the one-row matrix, therefore valid formula is:
=HLOOKUP(A2,IMPORTRANGE("1I69LQ37hA5NjjJmuwmWkrDI_daiU_cnYDrUBpMm","report!$A$2:$B$2"),1,0)

How do you count text from a cell in another sheet with Google Spreadsheets?

After researching for a solution on how to count text in a cell I've run into an issue and I'm unsure what's causing incorrect results or if I am not fully using this correctly. In my research I've read and tested:
Counting number of occurrences in column?
Need to be able to refer to a sheet within a count function
How to fetch data from 2nd sheet in Google SpreadSheets
SELECT and COPY row to another SHEET in Google Spreadsheets
Which suggest using COUNTIF and I can reference Sheet 1 named foo from Sheet 2 named bar using:
=COUNTIF(foo!$A$1,"$")
However, if the contents of Sheet 1 A:1 contain the text:
$1.00
$1.32
$13.98
$42.88
I'm looking for the count of times the $ is present in a cell but when I use =COUNTIF(foo!$A$1,"$") it just returns 1. When I click the help section on Learn more about COUNTIF I see where it shows COUNTA but now COUNTA only returns 2?
When I look up =QUERY it says for the range but I'm not looking for the range I'm looking for the total count of $ in a cell.
In Google Spreadsheets how can I return the correct count of $ in from Sheet 1 named foo from cell A:1 to Sheet 2 named bar cell A:1 so that if A:2 has 10 instances of $ it will show 10 in Sheet named bar A:2?
Bar!A1:
=ARRAYFORMULA(LEN(foo!A1:A5)-LEN(SUBSTITUTE(foo!A1:A5,"$",)))
It's common practice to just subtract length of substituted cell from the actual cell length to get count of the substitution string.

Google spreadsheets: conditional formatting with custom formula based on values from different sheet

I have a google spreadsheet with two sheets. On the first sheet, the items are listed in column A, and their types are in column B.
On the second sheet, I reference to the first sheet's column A with the formula: =UNIQUE(Sheet1!A:A).
Then I want to color the items in the 2nd sheet based on the item type (1st sheet's column B*). I try to add conditional formatting using the formula =Sheet1!$B:$B="Type1", but when I try to save rules, it says the formula is invalid.
What would be the correct formula for this case?
You cannot reference cells directly in the conditional format formula. Also, I do not think that formula would work.
First, you need the VLOOKUP function to get the object's type.
Here's an example of using this function. The difference would be that the corresponding data in the first parameter would be preceded by the sheet name in your case.
See this image:
As you can see, VLOOKUP searchs for the item from D in the matrix A2:B4, and then returns the corresponding value (the value in the same row) in the second column of the matrix, which is column B.
So if you to apply this formula to conditional formatting, you would have:
=VLOOKUP($D$2:$D;$A$2:$B$4;2)="Dragon"
The first parameter of VLOOKUP has to be a range, and I started it from D2 so it would not match the header. In your case, it would become:
=VLOOKUP($D$2:$D;Sheet1!$A$2:$B$4;2)="Dragon"
But as you cannot use references directly, you need to use the INDIRECT function. It receives a cell range as a string and returns a reference to that range. It's the same as using references directly, but in this case you add an extra step (go figure). So the formula finally becomes:
=VLOOKUP($D:$D;INDIRECT("$A$2:$B$4");2)="Dragon"
You can choose any range to apply the format if you are using the new version of Spreadsheets. You could, for example, color the entire row or just a single cell.

how to concatenate a section of a column of data

Is it possible to use a cell to define the end of a range for a formula?
Essentially, I would like to use the JOIN() statement to concatenate a section of a column of data.
The data starts at a cell, say A5, and runs to a cell with the word 'total' in it. I've created a cell (let's call it L12) which returns the number of rows of data that are available.
I'd like to use that cell to form part of the range details of the JOIN() formula thus:
=JOIN(" ", A5: A[the value in L12]).
Is this possible?
I've found the indirect() function does exactly what I needed it to do.
See the Google function list https://support.google.com/docs/bin/static.py?hl=en&topic=25273&page=table.cs

Resources