Field formula with Case/If statement - salesforce

I want to set a formula for a field C by check two picklist fields A and B (the value in the picklist will Yes, No, None)
If field A = "Yes" and field B = "Yes" then field C = "1"
If field A = "Yes" and field B <> "Yes" then field C = "2"
If field A <> "Yes" and field B = "Yes" then field C = "3"
If field A <> "Yes" and field B <> "Yes" then field C = "4"
I don't know how to set a formula for field C to make it works, I try CASE, IF statement without any luck.
Any help will be appreciated.
Thanks,

I would suggest to spend some times on these trailhead: Use Formula Fields and Advanced Formulas.
Bookmarking Formula Operators and Functions documentation page would be useful too.
Since A and B are picklist fields you must use ISPICKVAL(field, value).
If C is a text formula field:
IF( ISPICKVAL(A, 'Yes'),
IF ( ISPICKVAL(B, 'Yes'),
'1',
'2'
),
IF ( ISPICKVAL(B, 'Yes'),
'3',
'4'
)
)

Related

google sheet : How to vlookup by matching value in between max and min?

I have 2 sheets like this :
In that 2nd sheet, i want to lookup the id (S/M/L/XL) by checking if value is in between the Min value and Max value. So the goal is to have something like that 2nd sheet where in 'level' column, i need to put a formula so it will check the value is between what range so then it will retrieve the correct 'id' from 1st sheet.
the rule is if the value is >= minvalue and < max value
How can i do this ?
Thanks
use:
=INDEX(IF(A9:A="",,VLOOKUP(A9:A, {C2:C5, A2:A5}, 2, 1)))
Your first table, has overlapping values, so I suggest you think better about the rules you want to apply.
For example, 1, according your table can match both "S" and "M" , same for 3, which can be "M" or "L".
Once you have resolved that, you can use the QUERY function.
Example:
=QUERY($A$2:$D$5,
"select A,D where C<="&A2&" AND D >="&A2&" ORDER BY D DESC LIMIT 1 ")
Working solution can be found here:
https://docs.google.com/spreadsheets/d/1oLVwQqihT_df2y_ZQnfx7By77HnKSFz0bcbOzMuWqOM/edit?usp=sharing
Rather than have min and max columns, you could just use one column to list incremental values that determine the next size, and use vlookup() with a sort option of true - this avoids overlapping values:
=arrayformula({"level";if(A2:A<>"",VLOOKUP(A2:A,{Source!C:C,Source!A:A},2,1),)})

If blank return 0 else run vlookup

I would like to use the vlookup function to match two criteria values firstly based on the value selected in a dropdown menu (country) and the value in A2(name). If the value in A2 Sheet matches the one of the values in the A column in Sheet2 and the value of the dropdown menu in Sheet1 matches one of the values in Sheet2 Column D (Which is a concatenation of the name and country) I would like to return the corresponding value in Sheet2 ColumnC.
If the value is 0 or blank I would like to return 0.
This is what I have tried
=ARRAYFORMULA(
IF(
ISBLANK(
IFERROR(VLOOKUP(A2&C2,Sheet2!$A$2:$E$61,3,1),"0"))))
Not sure what I might be doing wrong
Here is a sample of my data
Sheet 1:
A B C
name1 (vlookup) [dropdownmenu]
and Sheet 2
A B C
name1 val concatenationofA&B
Here is a test sheet as requested:
https://docs.google.com/spreadsheets/d/1jsFnaGY7N9nXyPs5vR32jG5G838w1SgB2XIad7bEFXg/edit?usp=sharing
try:
=ARRAYFORMULA(IFNA(VLOOKUP(A2:A&C2:C, {Sheet2!A2:A&Sheet2!B2:B, Sheet2!C2:C}, 2, 0), 0))
I managed to resolve this using the query function
=QUERY(Sheet2!$A$2:$E$61,"select C where B = """&C12&""" and A = """&A12&""" ")
The only problem is I don't know how to suppress NA values/replace them with the value 0

Using COUNTIFS in Google Sheets with a possible blank criteria

Ok so I'm sure there is a way to do this, I am just not sure how to do it. Say I have 3 columns. Column A contains a "A" or "B", column B contains "1" or "2" and column C contains "!" or "?". I have separate cells in which I use to filter what I am looking for. I want to find each time a certain instance when all 3 character that I pick show up. D1 is where I choose "A" or "B", D2 is where I choose "1" or "2" and D3 is where I choose "!" or "?". I have a formula to find where if I choose say A, 1, !, it counts how many times all three of those appear in the same row. Now I want to leave one of the Cells blank (say D1) and only calculate how many times "1" and "!" appear together and not worry about "A" or "B" in the first column. Is there any way to do this? My current equation is just a long COUNTIFS equation that checks row A against cell D1, row B against D2 and row C against D3. Is there any way to use this equation but ignore one of the conditions if a cell is blank?
Sorry for the rambling and poor formatting. I hope this makes sense!
Now you can combine the criteria (1,2 or 3):
= countif
(
filter
(
if ( D2 = "" , left("", row($A$2:$A)^0), $A$2:$A) &
if ( D3 = "" , left("", row($B$2:$B)^0), $B$2:$B) &
if ( D4 = "" , left("", row($C$2:$C)^0), $C$2:$C)
, $A$2:$A <> ""
)
, "=" & D2 & D3 & D4
)
try:
=COUNTIFS(A:A, IF(E3="", "<>"&E3, E3), B:B, E4, C:C, E5)
for full independency you can do:
=COUNTIFS(A:A, IF(E3="", "<>"&E3, E3),
B:B, IF(E4="", "<>"&E4, E4),
C:C, IF(E5="", "<>"&E5, E5))

How to put two simple expressions together?

I'm working on a simple SSIS package for myself; I'm a newbie to all of this.
I have a flat file that has MALE or FEMALE values for Gender.
I want this to be only M or F in the Gender column on SQL Server when I load it. I have the below expressions:
REPLACE ( [Gender], "Female", "F")
REPLACE ( [Gender], "Male", "M")
I'm not sure how to club them together.
Any help/tips would be highly appreciated.
Thank you!
Chain them:
REPLACE (REPLACE ( [Gender], "Female", "F" ), "Male", "M")
Breaking it down, the inner REPLACE replaces all instances of "Female" with "F" but does nothing to any other values. The output of that function call ( which should either be "F" or "Male") is then passed to the outer REPLACE which transforms "Male" to "M". So, you should get "M" or "F" as appropriate.
Try CASE instead:
CASE
WHEN [Gender] = 'Female' THEN 'F'
WHEN [Gender] = 'Male' THEN 'M'
ELSE NULL
END
Use a derived column -
If the allowable values for that column are only Male and Female then this expression would suffice- ([Gender]=="Male"?"M":"F")
In case the column holds values other than Male/Female you might want to do something additional, here I am just passing the [Gender] value as is if it does not fall into Male/Female - ([Gender]=="Male"?"M":([Gender] == "Female"? "F":[Gender]))

T-SQL field parsing

There is a field in a 3rd party database that I need to group on for a report I'm writing. The field can contain a few different types of data. First it could contain a 3 digit number. I need to break these out into groups such as 101 to 200 and 201 to 300. In addition to this the field could also be prefaced with a particular letter such a M or K then a few numbers. It is defined as VARCHAR(8) and any help in how I could handle both cases where it may start with a particular letter or fall within a numeric range would be appreciated. If I could write it as a case statement and return a department based either on the numeric value or the first letter that would be the best so I can group in my report.
Thanks,
Steven
If I could write it as a case statement and return a department based either on the numeric value or the first letter that would be the best so I can group in my report.
case when substring( field, 1, 1 ) = 'M' then ...
when substring( field, 1, 1 ) = 'K" then ...
else floor( (cast( field as int) - 1 ) / 100) end
select ....
group by
case when substring( field, 1, 1 ) = 'M' then ...
when substring( field, 1, 1 ) = 'K" then ...
else floor( (cast( field as int) - 1 ) / 100) end
Matt Hamilton asks,
Any reason why you've opted to use substring(field, 1, 1) rather than simply left(field, 1)? I notice that #jms did it too, in the other answer.
I know substring is specified in ANSI-92; I don't know that left is. And anyway, left isn't a primitive, as it can be written in terms of substring, so using substring seems a little cleaner.
select
CASE (CASE WHEN substring(field,1,1) between 0 and 9 then 'N' Else 'C' END)
WHEN 'N' THEN
CASE field
WHEN ... THEN ...
WHEN ... THEN ...
END
WHEN 'C' THEN
CASE field
WHEN ... THEN ...
WHEN ... THEN ...
END
END

Resources