Google Sheets multiple conditions with nested If statements? - if-statement

I'm really struggling with this formula, but I think I'm overthinking it. I'm not sure what's the proper way to write this:
What I want to happen is:
If cell A1 = John AND
cell B1 = Buyer
then take the dollar amount in C1 and multiply it by 10%
ELSE
If cell A1 = John AND
cell B1 = Seller
then take the amount in C1 and multiple it by 25% BUT
If A1 is NOT equal to John, then return a currency value of $0
I sincerely appreciate any help. Thank you!
Lisa

Try this:
=if(A1="John",ifs(B1="Buyer",C1*10%,B1="Seller",C1*25%),0)

or try:
=IF(A1="John",
IF(B1="Buyer", C1*10%,
IF(B1="Seller", C1*25%, )), 0)
or:
=IF((A1="John")*(B1="Buyer"), C1*10%,
IF((A1="John")*(B1="Seller"), C1*25%, 0))

Related

Filter formula for multiple cell references

How can I make it so the formula on E2 reads values entered in any of the search cells and displays it as results, considering I have a button to clear all search boxes and users are instructed to only search one box at a time and to press the button if multiple boxes are filled?
See image
Here's my editable Spreadsheet
Much appreciated.
try:
=IFNA(QUERY(A2:C4; "where A = '"&B6&"'
or B = '"&B7&"'
or C = '"&B8&"'"; 0))
I'd like results to be shown only if the data users look for are in the same row.
=IFNA(QUERY(A2:C4; "where A = '"&B6&"'
and B = '"&B7&"'
and C = '"&B8&"'"; 0))
UPDATE:
=IFNA(QUERY(A2:C4; "where "&TEXTJOIN(" and "; 1;
IF(B6="";;"A = '"&B6&"'");
IF(B7="";;"B = '"&B7&"'");
IF(B8="";;"C = '"&B8&"'"))&""; 0))
You can combine conditions via + and *
=IFNA(
FILTER(A2:C4;(A2:A4=B6)+(B2:B4=B7)+(C2:C4=B8));
"Enter the data for the request"
)

Replacing cells with cumulative count

is there a way to create a formula, that will cumulatively count cells with an x inside and output the result as a new range?
So this would be achieved:
It would also be completely fine if the cumulative count was in all cells (so the result would look like so):
Assuming you're looking for the formula to go in cells A6:C9 -
Put this formula into cell A6 and copy across/down:
=COUNTA($A1:A1)
If you want to specify the criteria for what increments the counter:
=COUNTIF($A1:A1,"x")
I ended up using this:
=ARRAYFORMULA(if(A2:C2="x",COUNTIFS(A2:C2,A2:C2,COLUMN(A2:C2),"<="&COLUMN(A2:C2)),))
which is a derived version of a formula in this tutorial:
https://infoinspired.com/google-docs/spreadsheet/running-count-in-google-sheets/
try:
=ARRAYFORMULA(IF(A1:C4="",,
IF(A1:A4<>"", COLUMN(A:C),
IF(B1:B4<>"", COLUMN(A:C)-1,
COLUMN(A:C)-2))))
update:
=ARRAYFORMULA(IF(A1:AI4="",,
IF(A1:A4<>"", COLUMN(A:AI), IF(B1:B4<>"", COLUMN(A:AI)-1,
IF(C1:C4<>"", COLUMN(A:AI)-2, IF(D1:D4<>"", COLUMN(A:AI)-3,
IF(E1:E4<>"", COLUMN(A:AI)-4, IF(F1:F4<>"", COLUMN(A:AI)-5,
IF(G1:G4<>"", COLUMN(A:AI)-6, IF(H1:H4<>"", COLUMN(A:AI)-7,
IF(I1:I4<>"", COLUMN(A:AI)-8, IF(J1:J4<>"", COLUMN(A:AI)-9,
IF(K1:K4<>"", COLUMN(A:AI)-10, IF(L1:L4<>"", COLUMN(A:AI)-11,
IF(M1:M4<>"", COLUMN(A:AI)-12, IF(N1:N4<>"", COLUMN(A:AI)-13,
IF(O1:O4<>"", COLUMN(A:AI)-14, IF(P1:P4<>"", COLUMN(A:AI)-15,
IF(Q1:Q4<>"", COLUMN(A:AI)-16, IF(R1:R4<>"", COLUMN(A:AI)-17,
IF(S1:S4<>"", COLUMN(A:AI)-18, IF(T1:T4<>"", COLUMN(A:AI)-19,
IF(U1:U4<>"", COLUMN(A:AI)-20, IF(V1:V4<>"", COLUMN(A:AI)-21,
IF(W1:W4<>"", COLUMN(A:AI)-22, IF(X1:X4<>"", COLUMN(A:AI)-23,
IF(Y1:Y4<>"", COLUMN(A:AI)-24, IF(Z1:Z4<>"", COLUMN(A:AI)-25,
IF(AA1:AA4<>"", COLUMN(A:AI)-26, IF(AB1:AB4<>"", COLUMN(A:AI)-27,
IF(AC1:AC4<>"", COLUMN(A:AI)-28, IF(AD1:AD4<>"", COLUMN(A:AI)-29,
IF(AE1:AE4<>"", COLUMN(A:AI)-30, IF(AF1:AF4<>"", COLUMN(A:AI)-31,
IF(AG1:AG4<>"", COLUMN(A:AI)-32, IF(AH1:AH4<>"", COLUMN(A:AI)-33,
COLUMN(A:AI)-34))))))))))))))))))))))))))))))))))))

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")

How to compare values in pandas between two different columns?

My Table:
A Country Code1 Code2
626349 US 640AD1237 407223
702747 NaN IO1062123 407255
824316 US NaN NaN
712947 US 00220221 870262123
278147 Canada 721AC31234 109123
278144 Canada NaN 7214234321
278142 Canada 72142QW134 109123AS12
Here in the above table I need to check country and code.
I want a 5th column with correct or wrong, pseudocode:
If 'Country' == 'US' and (length(Code1) OR length(Code2) == 9):
Add values to 5th column as correct.
else:
Add values to 5th column as incorrect.
If 'Country' == 'Canada' and (length(Code1) OR length(Code2) == 10):
Add values to 5th column as correct.
else:
Add values to 5th column as incorrect.
if no values are there either in Country or Code Column than insufficient information.
I am not able to understand how should I do this in pandas. Please help. Thanks.
I tried to first find the length of rows of Code1 and Code2 and store it in different df but after that I am not able to Compare the different set of data as what I need to do.
Len1 = df.Code1.map(len)
Len2 = df.Code2.map(len)
LengthCode = pd.DataFrame({'Len_Code1': Len1,'Len_Code2': Len2})
Please tell me the better way of how to do this in single dataframe if possible.
I tried this
df[(df.Country == 'US') & ((df.Code1.str.len() == 9)|(df.Code2.str.len() == 9))|(df.Country == 'Canada') & ((df.Code1.str.len() == 10)|(df.Code2.str.len() == 10))]
But it is getting long and I will not be able to write for many countries.
This will give you a 'is_correct' boolean column:
code_lengths = {'US':9, 'Canada':10}
df['correct_code_length'] = df.Country.replace(code_lengths)
df['is_correct'] = (df.Code1.apply(lambda x: len(str(x))) == df.correct_code_length) | (df.Code2.apply(lambda x: len(str(x))) == df.correct_code_length)
You will need to populate the code_lengths dictionary with more countries as necessary.

How can I sort and include rules sorting in django?

I'm not to confident that I am asking this question correctly but this is what I'd like to do.
In django admin, I would like to write an action that sorts the list of my contestants randomly and doesn't allow two people with the same first name to be within 4 records of eachother. So basically,
if you have John L. John C. Carey J, Tracy M. Mary T., the records would be listed like this:
John L.
Mary T.
Carey J.
Tracy T.
John C.
OR
How can I write an action that would create random groups where two people with the same name wouldn't be within the same group like so:
John L. John C. Carey J, Tracy M. Mary T. =
Group 1
John L.
Mary T.
Carey J.
Tracy T.
Group 2
John C.
Forgive me if it isn't very clear, let me know and I'll try to specify further but any help would be appreciated
EDIT:
Is this what you are referring to? I can't quite figure out how to compare the fields to see if they are the same
Model:
class people(models.Model)
fname = model.CharField()
lname = model.CharField()
group = model.IntegerField()
View:
N = 4
Num = randint(0, N-1)
for x in queryset:
x.group = Num
if group == group| fname == fname | lname == lname:
x.group = (Num + 1) % N
Your first question cannot be solved always. Just think of all contestants have the same name, then you actually cannot find a solution to it.
For the second question, I can suggest an algorithm to do that, though.
Since I do not see any restriction on the number of groups, I will suggest a method to create the least number of groups here.
EDIT: I assumed you don't want 2 people with same "First name" in a group.
The steps are
Count the appearance of each name
count = {}
for x in queryset:
if x.fname not in count:
count[x.fname] = 0
count[f.name] += 1
Find the name with the most appearance
N = 0
for x in queryset:
if count[x.fname] > N:
N = count[x.fname]
Create N groups, where N equals to the number of appearance of the name in step 2
For each name, generate a random number X, where X < N.
Try to put the name into group X. If group X has that name already, set X = (X + 1) % N and retry, repeat until success. You will always find a group to put the contestant.
from random import randint
groups = [[]] * N
for item in queryset:
X = randint(0, N-1)
while item.fname in groups[X]:
X = (X + 1) % N
groups[X].append(item.fname)
item.group = X
EDIT:
Added details in steps 1, 2, 4.
From the code segment in your edited, I think you do not actually need a definition of "group" in model, as seems you only need a group number for it.