How to use substitute function with query function in google spreadsheet - google-sheets

I am trying to use substitute function inside a query function but not able to find the correct syntax to do that. My use case is as follows.
I have two columns Name and Salary. Values in these columns have comas ',' in them. I want to import these two columns to a new spreadsheet but replace comas in "Salary" column with empty string and retain comas in "Name" column. I also want to apply value function to "Salary" column after removing comas to do number formatting.
I tried with the following code but it is replacing comas in both the columns. I want a code which can apply the substitute function only to a subset of columns.
Code:
=arrayformula(SUBSTITUTE(QUERY(IMPORTRANGE(Address,"Sheet1!A2:B5"),"Select *"),",",""))
Result:
Converted v/s Expected Result
Note :
I have almost 10 columns to import and comas should be removed from 3 of them.

Based on your suggestions, I was able to achieve the objective by treating columns separately. Below is the code.
=QUERY({IMPORTRANGE(Address,"Sheet1!A3:A5"),arrayformula(VALUE(SUBSTITUTE(IMPORTRANGE(Address,"Sheet1!B3:B5"),",","")))},"Select * where Col2 is not null")
Basically, two IMPORTRANGE functions side by side for each column.
The same query on the actual data with 10 columns will look like this.
=QUERY({IMPORTRANGE(Address,"Sheet1!A3:C"),arrayformula(VALUE(SUBSTITUTE(IMPORTRANGE(Address,"Sheet1!D3:H"),",",""))),IMPORTRANGE(Address,"Sheet1!I3:J")},"Select * where Col2 is not null")
I used 3 IMPORTRANGE functions so that I can format the columns D to H by removing comas and changing them to number.

My suggestion is to use 2 formulas and more space in your sheets.
Formula #1: get the data and replace commas:
=arrayformula(SUBSTITUTE(IMPORTRANGE(Address,"Sheet1!A2:B5"),",",""))
Formula #2: to convert text into numbers:
=arrayformula (range_of_text_to_convert * 1)
Notes:
using 2 formulas will need extra space, but will speed up formulas (importrange is slow)
the second formula uses math operation (*1) which is the same as value formula.

Try this. I treats the columns separately.
=arrayformula(QUERY({Sheet1!A2:A5,SUBSTITUTE(Sheet1!B2:B5,",","")},"Select *"))

Thanks to Ed Nelson, I was able to figure out this:
=arrayformula(QUERY({'Accepted Connections'!A:R,SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE('Accepted Connections'!A:R,"AIF®",""),"APA",""),"APMA®",""),"ASA",""),"C(k)P®",""),"C(k)PS",""),"CAIA",""),"CBA",""),"CBI",""),"CCIM",""),"","")},"Select *"))
That removed all the text I didn't need in specific columns.

Related

Unnest two columns in google sheet

I have a table like this one here (basically it's data from a google form with multiple choice answers in column A and B and non-muliple choice data in column C) I need a separate row for each multiple choice answer.
Column A
Column B
Email
A,B
XX,YY
1#gmail.com
A,C
FF,DD
2#gmail.com
I tried to un-nest the first column and keep the remaining columns like this
enter image description here
I tried several approaches I found with flatten and split with array formulas but I don't know where to start really.
Any help or hint would be much appreciated!
You can use the split function on the column A and after that, use the index function. Considering the table, you can use:
=index(split(A2,","),1,1)
The split function separate the text using the delimiter indicated, returning an array with 1 line and 2 columns; the index function will return the first line and the first column from this array. To return the second element from the column A, just change to
=index(split(A2,","),1,2)
I think there's no easy solution for this. You're asking for as many combinations of elements as multiple-choice elections have been made. Any function in Google Sheets has its potentials and limitations about how many elements it can express. One very useful formula here is REDUCE. With REDUCE and sequences of elements separated by commas counted with COUNTA, you can stablish this formula:
=QUERY(REDUCE({"Col A","Col B","Email"},SEQUENCE(COUNTA(A2:A)),LAMBDA(z,c,{z;LAMBDA(ax,bx,
REDUCE({"","",""},SEQUENCE(ax),LAMBDA(w,a,
{w;
REDUCE({"","",""},SEQUENCE(bx),LAMBDA(y,b,
{y;INDEX(SPLIT(INDEX(A2:A,c),","),,a),INDEX(SPLIT(INDEX(B2:B,c),","),,b),INDEX(C2:C,c)}
))})))
(COUNTA(SPLIT(INDEX(A2:A,c),",")),COUNTA(SPLIT(INDEX(B2:B,c),",")))})),
"Where Col1 is not null",1)
Since I had to use a "initial value" in every REDUCE, I then used QUERY to filter the empty values:

How to create arrayformula sequence number separated by comma in google sheets

How to create arrayformula sequence number separated by comma in google spreadsheets
expected results is in column B
A
B
5
1,2,3,4,5
2
1,2
3
1,2,3
How about the following sample formula?
Sample formula:
=JOIN(",",ARRAYFORMULA(ROW(INDIRECT("A1:A"&A1))))
When you use this formula, please put this to a cell "B1" and drag down it.
The flow of this formula is as follows.
Create a1Notation of cells using "A1:A"&A1 using INDIRECT.
Retrieve row numbers from the a1Notation using ROW.
Join the row numbers with , using JOIN.
Result:
Note:
When you want to put all result values using one formula, unfortunately, I couldn't find the formula using the built-in functions. In that case, I would like to propose to a custom function as follows. When you use this, please copy and paste the following script to the script editor of Spreadsheet.
And, please put a formula of =SAMPLE(A1:A) to a cell "B1". By this, the result values are put to the column "B" using the values of column "A".
const SAMPLE = v => v.map(([e]) => e && !isNaN(e) ? [...Array(e)].map((_, i) => i + 1).join(",") : "");
References:
INDIRECT
ROW
JOIN
Custom Functions in Google Sheets
You can use TEXTJOIN() with SEQUENCE() function.
=TEXTJOIN(",",TRUE,SEQUENCE(A1))
You can also use this functions in desktop Excel365

google sheets, Make letters separated by comma given number of following columns. Not using app script, just formulas please

I'm using the query function in google sheets... I want to select columns F to AB.
I need a cell that has the output F,G,H,I... ,AB so I can put it in the Select statement.
This formula should have 2 inputs, the starting letter (F in this case) and number of following columns (22 in this case)
No app script code please. I know someone can do it with just formulas in one cell.
I believe your goal as follows.
You want to create the value of F,G,H,I... ,AB by giving the start column letter of "F" and the number of columns of 22.
For example, when the start column letter of "F" and the number of columns of 22 are given, you want to retrieve the value of F,G,H,I... ,AB and you want to use this like =QUERY(A1:AB,"SELECT F,G,H,I... ,AB").
You want to achieve this using the built-in functions of Google Spreadsheet without the Google Apps Script.
For this, how about this answer?
Sample formula 1:
In this sample formula, in order to create the value of F,G,H,I... ,AB by giving the start column letter of "F" and the number of columns of 22, I would like to propose the following formula.
=TEXTJOIN(",",TRUE,ARRAYFORMULA(REGEXREPLACE(ADDRESS(1,COLUMN(INDIRECT(A1&"1:"&ADDRESS(1,B1+COLUMN(INDIRECT(A1&"1")),4))),4),"\d+","")))
When A1 and B1 have the values of F and 22, respectively, the flow of this formula is as follows.
Using COLUMN, retrieve the column numbers.
Using ADDRESS, retrieve the column letters from the column numbers. At that time, the row number os removed using REGEXREPLACE.
Above formulas are used with ARRAYFORMULA.
Using TEXTJOIN, join the column letters with ,.
Result:
Sample formula 2:
In this sample formula, =QUERY(A1:AB,"SELECT F,G,H,I... ,AB") is created using above sample formula 1.
=QUERY(A1:AB,"SELECT "&TEXTJOIN(",",TRUE,ARRAYFORMULA(REGEXREPLACE(ADDRESS(1, COLUMN(INDIRECT(A1&"1:"&ADDRESS(1, B1 + COLUMN(INDIRECT(A1&"1")), 4))), 4),"\d+",""))))
Result:
In this sample result, the range of A2:AB7 is used for QUERY. So please be careful this. When the cells "A1" and "B1" are changed, the result of this formula is also changed.
Note:
In this case, when the start column and the number of columns are more than the existing maximum columns, an error occurs, please be careful this.
References:
COLUMN
ADDRESS
REGEXREPLACE
ARRAYFORMULA
TEXTJOIN
Added:
When you want to create Col6, Col7, Col8 ... Col28 by giving the start column letter of "F" and the number of columns of 22, how about the following sample formula?
Sample formula:
="Col"&TEXTJOIN(",Col",TRUE,ARRAYFORMULA(COLUMN(INDIRECT(A1&"1:"&ADDRESS(1,B1+COLUMN(INDIRECT(A1&"1")),4)))))
In this case, please put F and 22 to the cells "A1" and "B1", respectively.
if your range is F:AB then you can skip select parameter or use:
=QUERY(F1:AB; "select *"; 0)
if the range is larger put the range in curly brackets and try:
=ARRAYFORMULA(QUERY({A1:AB}; "select "&TEXTJOIN(","; 1; "Col"&COLUMN(F:AB)); 0))

Google Spreadsheet Function That Sums Numbers In A Column When the Row Contains An EXACT Text

I've been at this problem for a while now. I am trying to sum numbers under a specific column when the rows equal a certain text and then display that sum on a different sheet. So far I came up with this formula: =IF(EXACT(A2,Table!A2:A)=TRUE,SUM(Table!C2:C)); however the only problem is that is sums everything in column C (which makes sense).
I wish there was a way to do something like the following: SUM(Table!C2:C where EXACT(A2,TABLE!A2:A)=TRUE). I've also tried the SUMIF(), DSUM(), and QUERY() functions to no avail. I must be getting logically tripped up somewhere.
Figured it out: =SUM(FILTER(Table!E4:E, EXACT(Table!A4:A,A4)=TRUE)).
=sum ( FILTER (b1:b10, a1:a10 = "Text" ) )
// the above formula will help you to take the sum of the values in column B when another column A contain a specific text.
The formula is applicable only in Google Spreadsheets

pulling row number into query google spreadsheet

I have a data set that looks like this: starting on A1 with "1"
1 a
2 b
3 c
4 d
Column A is an arrayformula =arrayformula(row(b1:b))
Column B is manual input
i want to query the database and finding the row of the item by match column B so i have code as such
=query("A1:B","select A where B like '%c%')
this should give me "3"
My question:
is there a way to pull the 1-4 numbers into the query line? with something like array formula row(b1:b). I don't want to waste an extra column on column A
so basically I want just the manual input and when i query it gives me the row number.
No script code please.
I've tried a few things and it didn't work.
Looking for a solutions that starts with
=query()
You can also use a formula to pull in more than one row in the dataset which matches the condition, if this is important to you:
=arrayformula(filter(row(B:B); B:B="c"))
And you can have wildcard type operators, under certain circumstances (you are going to match text or items that can look like text (so numbers can be treated as text - but boolean will need more steps); that the dataset is not huge), using regular expressions. e.g.
=arrayformula(filter(row(B:B); regexmatch(B:B, "(c|d)")))
You could also use standard spreadsheet wildcard operators, e.g.
=arrayformula(filter(row(B:B); countif(B:B, "*c*")))
Explanation: In this case, the filter will be true when countif is greater than zero, i.e. when it sees something with a letter c in it, since spreadsheets see a value greater than zero as a boolean true and so, for that row where there is a countif match, there will be a a filter match, and so it will display that row (indeed, it is a similar situation with the regexmatch creating a true when there is a match of either c or d, in the case above).
Personally, I wanted to learn regex a bit, so I would go towards the regexmatch option. But that is your choice.
You can also, of course, create the match outside of the cell. This makes it easy to create a list of matches that you want to satisfy elsewhere on the sheet. So you could have a column of words or parts of words, from Z2 downwards, and then join them together in cell Z1 for example like this
="("&join("|",filter(Z2:Z50,len(Z2:Z50)))&")"
Then your filter function would look like this:
=arrayformula(filter(row(B:B), regexmatch(B:B, Z1)))
If you want to use like operator in the query function, you can try something like this:
=arrayformula(query(if({1,0}, B:B,row(B:B)),"select Col2 where Col1 like '%c%' "))
You can also use the regular expressions in the query function, for example:
=arrayformula(query(if({1,0}, B:B,row(B:B)),"select Col2 where Col1 matches '(.*c.*|.*d.*)' "))
I'm not entirely clear on the question, but as I understand it, you want to be able to enter a formula, and have it return the row number of the matched item in a range? I'm not sure where array formulas come in.
If I've understood your question correctly, this should do the trick:
=MATCH("C",B1:B,0)
In your example, this returns 3.
Please forgive me if I've misunderstood your question.
Note: If there are multiple matches, this will return the row number for the first instance of your search.
=QUERY({A1:A,ARRAYFORMULA(ROW(A1:A))},"SELECT Col2 WHERE Col1 LIKE '%c%'")

Resources