I have two columns in a table: column_A with 3 different values a1, a2, a3 and column_B with 5 different values b1, b2, b3, b4, b5. I have to create column_C in Excel based on the following logic -
> if (A=a1)
> if(B=b1 or B=b2)
> output=a1_yes
> else
> output = a1_no
> elseif(A=a2)
> if(B=b3 or B=b4)
> output = a2_yes
> else
> output = a2_no
> elseif(A=a3)
> if(B=b5)
> output = a3_yes
> else
> output = a3_no
Nested if-else makes the logic very complex. Is there any function we could use for simplicity and avoid nested if-else?
Assuming you want to get rid of the nested if-else, I would suggest you use the function Switch instead.
You can concatenate the columns you are validating and provide results based on the possible values.
Given the table below:
Make 1
Model 1
Color 1
Use the formula:
SWITCH(expression,value1,result1,default_or_value2,result2,...)
Example:
=SWITCH(CONCAT(A1,B1),"Make1","a1_yes","Make2","a1_yes","a2_yes")
Reference
SWITCH Formula
I think in such complex cases it is best to create a lookup table and find the required value with the LOOKUP function which works starting from Excel 2007:
=LOOKUP(2,1/((A2=$H$2:$H$9)*(B2=$I$2:$I$9)+(A2=$H$2:$H$9)*(""=$I$2:$I$9)),$J$2:$J$9)
In the case of GS, this formula must be included in the ARRAYFORMULA function.
=ArrayFormula(LOOKUP(2,1/((A2=$H$2:$H$9)*(B2=$I$2:$I$9)+(A2=$H$2:$H$9)*(""=$I$2:$I$9)),$J$2:$J$9))
If you can't create lookup table here will be option without it:
=LOOKUP(2,
1/((A2={"a1","a1","a1","a2","a2","a2","a3","a3"})
*(B2={"","b1","b2","","b3","b4","","b5"})
+(A2={"a1","a1","a1","a2","a2","a2","a3","a3"})
*(""={"","b1","b2","","b3","b4","","b5"})),
{"a1_no","a1_yes","a1_yes","a2_no","a2_yes","a2_yes","a3_no","a3_yes"})
Related
how i can choose rows with a condition on columns for example in below data frame i want use a list of columns to find same condition between them
lis_column1=['trigger3','height','trigger2']
df[df[lis_column1]>50]
and also when i add 'flag' column i get error becuase the items aren't number
how i can apply condition on all my lis_column. (also consider 'flag')
I mean:
lis_column1=
['trigger3','height','trigger2']
lis_column2=['flag']
df[df[lis_column1]>50] & df[df[lis_column2]==yellow]
I have not included student and flag because you can't compare string to int.
You can do something like this
df_subset = df[(df['height'] > 50) & (df['trigger2'] > 50) ].copy()
or when you have multiple conditions, I prefer this
cond1 = df['height'] > 50
cond2 = df['trigger2'] > 50
df_subset = df[(cond1) & (cond2)].copy()
Once you figure out the logic to compare student A to 50 or flag yellow to 50, you can add more conditions as mentioned above.
I need to convert 3 whole number columns to text in a formula when adding a new column inside power query. I know how to do this in dax using FORMAT function but I can't make it work inside power query.
3 columns are - click to veiw
Then below is my CUSTOM COLUMN:
= Table.AddColumn(RefNo.3, "Refernce Number", each
if Text.Length([RefNo.3]) > 1 and Text.Length([RefNo.3]) < 11 then [RefNo.3]
else if Text.Length([RefNo.2]) > 1 and Text.Length([RefNo.2]) < 11 then [RefNo.2]
else if Text.Length([RefNo.1]) > 1 and Text.Length([RefNo.1]) < 11 then [RefNo.1]
else null)
However, at the moment I'm getting this error:
Expression.Error: We cannot convert a value of type Table to type Number.
Details:
Value=[Table]
Type=[Type]
So I know I need to convert the whole number columns to text first inside the formula. Also, I had to intentionally convert those 3 columns from text to whole number previously to get rid of redundant values (so that's not an option for me to revert that). thanks in advance guys.
There are any number of ways to solve this, depending on your real data.
Just set the columns to Type.Text before executing your AddColumn function.
If you do this, you would also have to check for null as they will cause the script, as you've written it, to fail
Or you could precede your testing with another line to replace the nulls with an empty string (""): Table.ReplaceValue(table_name,null,"",Replacer.ReplaceValue,{"RefNo", "RefNo2", "RefNo3"}),
If they are all positive integers, compare the values rather than the string lengths: eg >=0 and <10000000000
Construct a numeric array, and return the last value that passes the filter
= Table.AddColumn(your_table_name, "Reference Number",
each List.Accumulate(List.Reverse(List.RemoveNulls({[RefNo],[RefNo2],[RefNo3]})),
null,(state,current)=> if state = null then
let
x = Text.Length(Text.From(current))
in
if x > 1 and x < 11 then current else state
else state))
I am working on an Excel switch funciton but something is not working.
I have 2 tables. This is my table 1 -
And this is my Table 2 -
Now, what is want is - when the table 1 "Pick value" Column has a value of A it should enter code herepick the value from Table 2 column value A Value when the value is B, it should pick B Value, else C value from the corresponding field.
That IS WHY I am using this switch query in Excel -
=SWITCH([Pick Value];"A";Table2[A Value];"B";Table2[B Value];Table2[C Value];)
But it is not working -
Anyone knows what I am doing wrong here!
Here is a SWITCH version also using XLOOKUP:
=XLOOKUP([#ID];Table2[ID];SWITCH([#[Pick Value]];"A";Table2[A Value];"B";Table2[B Value];"C";Table2[C Value]))
Use FILTER:
=#FILTER(CHOOSE(MATCH([#Pick Value];{"A";"B";"C"};0);Table2[A Value];Table2[B Value];Table2[C Value]);Table2[ID]=[#ID])
INDEX/MATCH
If you don't have 365, you can use one of the following formulas.
Comma
Array formula (CRTL+SHIFT+ENTER):
=IFERROR(INDEX(Table2[#[A Value]:[C Value]],MATCH([#[Pick Value]],LEFT(Table2[[#Headers],[A Value]:[C Value]],1),0)),"")
or if using "A","B","C" explicitly (ENTER):
=IFERROR(INDEX(Table2[#[A Value]:[C Value]],MATCH([#[Pick Value]],{"A","B","C"},0)),"")
Semi Colon
Array formula (CRTL+SHIFT+ENTER):
=IFERROR(INDEX(Table2[#[A Value]:[C Value]];MATCH([#[Pick Value]];LEFT(Table2[[#Headers];[A Value]:[C Value]];1);0));"")
or if using "A","B","C" explicitly (ENTER):
=IFERROR(INDEX(Table2[#[A Value]:[C Value]];MATCH([#[Pick Value]];{"A";"B";"C"};0));"")
I've got a matrix, with two coordinates [i;j]
I'm trying to automatize a lookup:
As an example, this would have the coordinates of [1;2]
Here's a table of all the coordinates:
So here, obviously [1;2] would equate to 143,33
To simplify the issue:
I'll try to go step by step over what I'm trying to do to make the question bit less confusing.
Think of what I'm trying to do as a function, lookup(i, j) => value
Now, refer to the second picture (table)
I find all rows containing index [i] (inside column C) and then
only for those rows find row containing index [j] (inside column D ∩ for rows from previous step)
Return [i;j] value
So if u invoked lookup(2, 4)
Find all rows matching i = 2
Row 5: i = 2 ; j = 3
Row 6: i = 2 ; j = 4
Row 7: i = 2 ; j = 5
Lookup column j for j=4 from found rows
Found row 6: i = 2 ; j = 4.
Return value (offset for yij column = 143,33)
Now this isn't an issue algorhitmically speaking, but I have no idea how to go about doing this with excel formulas.
PS: I know this is reltively simple vba issue but I would prefer formulas
PSS: I removed what I tried to make the question more readable.
You can use SUMPRODUCT, which return 0 for not found values:
=SUMPRODUCT(($C$4:$C$18=$I4)*($D$4:$D$18=J$3)*$E$4:$E$18)
or AGGREGATE, which returns an error that can be hidden by the IFERROR function:
=IFERROR(AGGREGATE(15,6,(1/(($C$4:$C$18=$I12)*($D$4:$D$18=J$3)))*$E$4:$E$18,1),"")
You can use SUMIFS here assuming you will not have exact duplicate combinations of [i, j]. If you did have a duplicate combination, the amounts will be summed and placed in the corresponding cell
In cell B2 place this equation: =SUMIFS($Q$2:$Q$16,$P$2:$P$16,B$1,$O$2:$O$16,$A2) and drag across and over as needed
IF you want to convert the 0's to blanks you can nest the above formula inside a text formatter like so:
=TEXT([formula], "0;-0;;#")
I have 3 columns with the following name:
name1, name2 and value.
I want to obtain in another sheet two tables having two compulsory condition (min and max calculated using name1 and name2):
the name of the first table to be taken from the column with name2 and this table has two partition.
the first partition named max, is calculating the max for 30_-20, 40_-20, 50_-20, 30_22, 40_22, 50_22, 30_60, 40_60, 50_60 and second partition named min, is calculating the min for 30_-20, 40_-20, 50_-20, 30_22, 40_22, 50_22, 30_60, 40_60, 50_60.
What I want to say can be viewed in the following picture.
I need this for my job, and I don't know anything about macros. I think it will be necessary to learn macros.
Given a layout on a single worksheet similar to your sample image.
The standard formula(s) for G3, I3, M3 and O3 are:
=MAX(INDEX($C$2:$C$999*($A$2:$A$999=H3)*($B$2:$B$999=H$1), , ))
=MIN(INDEX($C$2:$C$999+(($A$2:$A$999<>H3)+($B$2:$B$999<>H$1))*1E+99, , ))
=MAX(INDEX($C$2:$C$999*($A$2:$A$999=N3)*($B$2:$B$999=N$1), , ))
=MIN(INDEX($C$2:$C$999+(($A$2:$A$999<>N3)+($B$2:$B$999<>N$1))*1E+99, , ))
Fill down as necessary. It is usually easier to reference a cell containing a value (e.g. 30_-20) than repeatedly hardcoding the value into a variety of similar formulas. I've used H3:H5 and N3:N5 for the column A values.
How it Works:
See MINIF, MAXIF and MODEIF with Standard Formulas.