used if statement in select query does not work - if-statement

I have an order list in csv file. I want to transform the data.
here's my query. I have to add 2 new columns "gift box set" and blue oyser mushroom". If name in V column contains gift box set, then show 1, otherwise leave it blank. Please help me to correct my query. also how can I give a new column name for those 2 added columns?
=query(orders!A2:AP134, "select Q, K, V, if(V="Gift Box Set 5lbs", "1", ""), if(V="Blue Oyster Mushroom 3lbs", "1", "")", true)

There is no if statement in QUERY syntax.
Try a simpler approach without QUERY:
=ARRAYFORMULA(
{
Q2:Q134,
K2:K134,
V2:V134,
IF(V2:V134 = "Gift Box Set 5lbs", 1, ""),
IF(V2:V134 = "Blue Oyster Mushroom 3lbs", 1, "")
}
)

try:
=ARRAYFORMULA({Q2:Q, K2:K,
IF(V2:V="Gift Box Set 5lbs", "1", ),
IF(V2:V="Blue Oyster Mushroom 3lbs", "1", )})
or:
=ARRAYFORMULA(FILTER({Q2:Q, K2:K,
IF(V2:V="Gift Box Set 5lbs", "1", ),
IF(V2:V="Blue Oyster Mushroom 3lbs", "1", )},
REGEXMATCH(V2:V, "Gift Box Set 5lbs|Blue Oyster Mushroom 3lbs"))
or:
=QUERY(INDEX(IF(REGEXMATCH(V2:V, "Gift Box Set 5lbs|Blue Oyster Mushroom 3lbs"),
{Q2:Q, K2:K, ROW(A2:A)^0}, )),
"where Col3=1", 0)

Related

Fetch for value and convert it to another according to condition

SHEET A - origin
Column A = ID
Column B = can contain "Accepted"(+ more text), "Rejected"(+more text), "Partially Accepted"(+ more text)
SHEET B - destination
Column A = Column A from Sheet A
Column B = If Column B from Sheet A = "Accepted*" return 1, "Rejected*" return 2, "Partially*" return 3, else return 4
WHAT I've tried so far:
a) Works but I can't make it into an array
=IF(COUNTIF(SHEETA!A2,"*Rejected*"),2,IF(COUNTIF(SHEETA!A2,"*Partially*"),3,IF(COUNTIF(SHEETA!A2,"Accepted*"),1,4)))
b) Been trying to make it work (simplified version) but it's not working
=if((VLOOKUP(A2,A2:B2,2,FALSE))="Rejected*","2","1")
Can anyone give me a hand?
Thank you in advance
try in row 2:
=INDEX(IFNA(VLOOKUP(A2:A, {SheetA!A2:A, IFNA(CHOOSE(MATCH(
REGEXEXTRACT(SheetA!B2:B, "(?i)accepted|rejected|partially accepted"),
{"accepted", "rejected", "partially accepted"}, ), 1, 2, 3), 4)}, 2, )))

Counting and adding multiple variables from single cell in sheets

I have a sheets document that has cells that users input data into. They know to input the data in a certain format; a 'number' and a 'letter', followed by a space, a SKU number, and then a comma.
I'd like to have a formula that counts the amount of each 'letters' and then adds the 'numbers' for each letter.
There are only five 'letters' users can choose from; M, E, T, W, B.
The data they input isn't restricted to a set order, and there isn't a limit of how much they can input, as long as it follows the aforementioned syntax.
I attached a screenshot of an example of how this should look.
The yellow cell is the user inputted data, and the green cells is data created by formula.
Or here's a link to a live version: link
I tried doing it with COUNTIF but that didn't work. I'm guessing it would be done with an array, but I don't know where to start. If I can see an example of something similar, I could probably do the rest.
yes:
=INDEX(REGEXREPLACE(SPLIT(REGEXREPLACE(FLATTEN(QUERY(TRANSPOSE(QUERY(TRANSPOSE(SORT(TRANSPOSE(QUERY(SPLIT(
FLATTEN(REGEXREPLACE(TRIM(SPLIT(A2:A9, ",")), "\b(\d+(?:\.\d+)?)(.+?)\b(.*)", ROW(A2:A9)&"×$1$2×$1×$2")), "×"),
"select count(Col2),sum(Col3) where Col2 is not null group by Col1 pivot Col4 label count(Col2)''")))),
"offset 1", 0)*1&TRIM(REGEXREPLACE(TRANSPOSE(SORT(FLATTEN(QUERY(SPLIT(
FLATTEN(REGEXREPLACE(TRIM(SPLIT(A2:A9, ",")), "\b(\d+(?:\.\d+)?)(.+?)\b(.*)", ROW(A2:A9)&"×$1$2×$1×$2")), "×"),
"select count(Col2),sum(Col3) where Col2 is not null group by Col1 pivot Col4 limit 0 label count(Col2)''")))),
".*sum", ))),,9^9)), "([^ ]+ [^ ]+) ", "$1×"), "×"), "(\d+(?:\.\d+)?)$", "($1)"))
I've added a new sheet ("Erik Help") with the following solution:
=ArrayFormula(FILTER( SPLIT("B E M T W", " ") & " (" & IFERROR(VLOOKUP(ROW(A1:A) & SPLIT("B E M T W", " "), QUERY(FLATTEN(SPLIT(QUERY(FLATTEN(IFERROR(REPT(ROW(A1:A) & REGEXEXTRACT(SPLIT(REGEXREPLACE(A1:A&",", "\d+,", ""), " ", 0, 1), "\D") & "~", 1*REGEXEXTRACT(SPLIT(REGEXREPLACE(A1:A&",", "\d+,", ""), " ", 0, 1), "\d+")))), "WHERE Col1 <>'' "), "~", 1, 1)), "Select Col1, COUNT(Col1) GROUP BY Col1"), 2, FALSE), 0)&")", A1:A<>""))

Concatenate cells between dynamic start and end row

I'm trying to concatenate a number of cells into one if they are between two cells with a certain string.
For example: In the Element column there are modalOpen and modalClose and in between those are modalFields. Between modalOpen and modalClose I need to add the Name of each row with Element modalField into the Output column for the modalOpen row.
The number of modalFields can vary from 2 - 20.
delete everything in C column and paste this in C2 cell:
=ARRAYFORMULA(TRIM(SUBSTITUTE(IFERROR(VLOOKUP(B2:B,
SPLIT(TRANSPOSE(SPLIT(QUERY(IF(B2:B<>"",
IF(A2:A="modalOpen", "♥"&B2:B&"♦"&B2:B&" with",
IF(A3:A="modalClose", "& <"&B2:B&">", "<"&B2:B&">,")), )
,,999^99), "♥")), "♦"), 2, 0)), ">, & ", "> & ")))
=ARRAYFORMULA(REGEXREPLACE(TRIM(TRANSPOSE(SPLIT(QUERY(FILTER(IF(A2:A="modalClose","",IF(A2:A="modalOpen","♠"&B2:B&" with ","<"&B2:B&">,")),A2:A<>""),,2^99),"♠"))),"(, )(\<[^<>]\>),$"," and $2"))
The result:
Test1 with <1>, <2> and <3>
Test2 with <1>, <2>, <3> and <4>

How to Return Text with IF Function in an Array

In Google Sheets, I'm trying to query a column and look for a state abbreviation, and if that abbreviation is a match, then "East" if not then "West"
Wanting to return text values in my column based on state abbreviation. We have territory manager split into two domains--East and West. So, trying to easily sort my data by East/West.
Here's what I have:
=IF(M:M={"AL", "CA", "DE","FL","GA","IA","KY","ME","MD","MA","MN","MS","NH","NJ","NY","ND","RI","SD","TN","VT","VA","WV","WI"},"East","West")
But, when I fill down, it just fills down East, and does not seem to actually query M:M
Thoughts?
Not the cleanest code, but this should work:
=ARRAYFORMULA(IF(LEN(A:A), IF((A:A = "foo")+(A:A = "bar") = 1, "WEST", "EAST"), ))
To use IF with an OR in an ARRAYFORMULA, you evaluate the column with 1s and 0s. The A:A = "foo" will evaluate to 1 if foo is in the cell. So if one of your OR criteria is in the cell, the total value in the IF will be 1.
You have a lot of criteria so writing each of them in will take a while ...
E.g. IF( (A:A = "AL") + (A:A = "CA") ... (A:A = "WI") = 1, "East", "West")
Use ISERROR/MATCH():
=IF(ISERROR(MATCH(M:M,{"AL", "CA", "DE","FL","GA","IA","KY","ME","MD","MA","MN","MS","NH","NJ","NY","ND","RI","SD","TN","VT","VA","WV","WI"},0)),"West","East")

Beginner rbind function

I cannot for the life of me understand the rbind function. I've tried using the examples on here, but I can't figure out what I am doing incorrectly. All I would like to do is add the data from my second data frame under the first.
Does rbind require the columns be the same name or...?
ParticipantA=c("A","B","C","D")
Score1A=c("21","20","21","21")
Score2A=c("32","40","32","31")
Score3A=c("47","50","43","46")
BlockA=data.frame(ParticipantA,Score1A,Score2A,Score3A)
BlockA$Major=c("Computer_Science","Computer_Science","Computer_Science","Computer_Science")
BlockA$Gender=c("Female","Female","Male","Male")
ParticipantB=c("E","F","G","H")
Score1B=c("28","28","21","22")
Score2B=c("30","36","37","32")
Score3B=c("41","49","49","46")
BlockB=data.frame(ParticipantB,Score1B,Score2B,Score3B)
BlockB$Major=c("Medical","Medical","Medical","Medical")
BlockB$Gender=c("Female","Female","Male","Male")
rbind requires that all columns be of the same name and class.
The problem is in the column titles. rbind uses column titles to orient how it will bind the rows. The columns can be in different orders, R will just use the first element to determine column order.
Alternatively, adding another column to your data frames, with the value "A" or "B" in it could preserve your information without putting "A"s and "B"s in your column names <-- the reason you can't use rbind. The additional column would also allow you to do more analyses in R, e.g. regression and other linear models.
Here is one way to handle your data:
Create a uniform set of column names that can be used for the data frames "BlockA" and "BlockB"
final_colnames <- c("Block", "Participant", "Score1", "Score2", "Score3")
Create a new list to identify which block the participants belong to.
BlockA = c("A", "A", "A", "A")
Your previous data
ParticipantA = c("A", "B", "C", "D")
Score1A = c("21", "20", "21", "21")
Score2A = c("32", "40", "32", "31")
Score3A = c("47", "50", "43", "46")
The label "BlockA" is recycled here to name the new data frame, but not before adding the "BlockA" column list of "A" "A" "A" "A".
BlockA = data.frame(BlockA, ParticipantA, Score1A, Score2A, Score3A)
The new column names have to be added at this point, so that the number of names and the number of columns are equal.
colnames(BlockA) <- final_colnames
Now you can add the remaining columns
BlockA$Major = c("Computer_Science", "Computer_Science", "Computer_Science", "Computer_Science")
BlockA$Gender = c("Female", "Female", "Male", "Male")
BlockB is the same process
BlockB = c("B", "B", "B", "B") # the extra column
ParticipantB = c("E", "F", "G", "H")
Score1B = c("28", "28", "21", "22")
Score2B = c("30", "36", "37", "32")
Score3B = c("41", "49", "49", "46")
BlockB = data.frame(BlockB, ParticipantB, Score1B, Score2B, Score3B)
colnames(BlockB) <- final_colnames # renaming the columns
BlockB$Major = c("Medical", "Medical", "Medical", "Medical")
BlockB$Gender = c("Female", "Female", "Male", "Male")
Uniform column names mean that rbind will now work.
rbind(BlockA,BlockB)