Excel replace numbers with custom unequal ranges - excel

Suppose I have the following data
Name Output
A 0.1
B 7
C 0.4
D 0.9
E 1.1
F 12
G 22
I would like replace the output variable by custom ranges:
Name Output Output_2
A 0.1 0m-0.3m
B 7 6y-10y
C 0.4 0.4m-0.6m
D 0.9 0.7m-1y
E 1.1 1y-5y
F 12 11y-20y
G 22 21y-40y
Right now, I am doing this (a long list of nested IFs)
=IF([#Tenor]<= 0.25, "0m-3m", IF([#Tenor]<=0.5, "4m-6m", IF([#Tenor] <= 1, "7m-1y",IF([#Tenor]<=5,"2y-5y",IF([#Tenor]<=10,"6y-10y",IF([#Tenor]<20,"11y-20y",IF([#Tenor]<40,"20y-40y")))))))
and it works but I am concerned that as the number of ranges increases, this will be painful to write. I was hoping I could write down a range somewhere and ask excel to look it up and do some case type thing.

Let's assume your first three columns are A, B and C (like in the second code block you posted). Add the following data into columns E and F (this will be your mapping data):
Output Output2
0 0m-3m
0.25 4m-6m
0.5 7m-1Y
1 2y-5y
5 6y-10y
10 11y-20y
20 20y-40y
40 20y-40y
Then write the following formula into C2 cell and drag it down:
=INDEX($F:$F,MATCH(B2,$E:$E,1))
UPDATE: you can do this even simpler with approximate VLOOKUP:
=VLOOKUP(B2,$E:$F,2,1)

Related

Shuffle a matrix using specified indexes in Excel?

My goal is to shuffle a matrix using a specified matrix of indexes.
For example, let's say this is my input matrix:
A B C
D E F
G H I
Now I want to shuffle my input matrix. But not in a random way, I want to use a custom set of indexes (0, 1, 2 etc.) that represent the final order of my matrix. Let's say this is the matrix of indexes:
5 3 8
1 0 4
2 7 6
The resulting matrix should be:
F D I
B A E
C H G
since A was in position 0, B was in position 1 etc.
I'm looking for the formula that I should insert in each cell of the resulting matrix. I tried INDEX and MATCH functions but I'm not sure this is the right way.
I would just use quotient/mod to get row and column
=INDEX($A$1:$C$3,QUOTIENT(E1,3)+1,MOD(E1,3)+1)
You could make it more general if you wanted to.

VLookup with Multiple Ranges

I'm trying to make a formula that would do the following: There are say 10 categories 1-10, given a number x and y, the line is in category 3 if and only if x is between 1 and 2 and y is between 5-7 for example. I don't know how to use VLookup given the multiple conditions and the two ranges that are completely different and not in a sequential order.
I tried using index match:
=INDEX(B5:B15,MATCH(1,IF(AND(K5>=C5:C15,K5<=D5:D15),1,0)*IF(AND(L5>=E5:E15,L5<=F5:F15),1,0),0))
but this returns an error where column B are the categories, K5 and L5 are x and y respectively and column C is the lower bounds for x per category with D as upper bounds and same for E and F for y.
Here's a mock representation of the data and rules:
Data
x y category
1.2 12 1
1.5 5 2
0.98 23 3
.
.
.
Rules
Category X-LB X-UB Y-LB Y-UB
1 1 2 9 15
2 1.5 1.7 1 9
3 0.8 1 20 23
.
.
.
LB is lower bound and UB is upper bound. For example given x and y above using the rules table we find the expected return column.
Thank you,
If you have only category which will fit the bill in each case, one way is to use SUMPRODUCT.
Formula in C2 and down is
=SUMPRODUCT(($B$10:$B$12<=A2)*($C$10:$C$12>=A2)*($D$10:$D$12<=B2)*($E$10:$E$12>=B2)*($A$10:$A$12))
In M5, copied down :
=INDEX($B$5:$B$15,MATCH(1,INDEX(($K5>=$C$5:$C$15)*($K5<=$D$5:$D$15)*($L5>=$E$5:$E$15)*($L5<=$F$5:$F$15),0),0))
Or,
Another shorter option.
Using SUMIFS function, formula in M5 copied down :
=SUMIFS($B:$B,$C:$C,"<="&$K5,$D:$D,">="&$K5,$E:$E,"<="&$L5,$F:$F,">="&$L5)

Distributing data with lower and upper boundaries in Excel

Here's a link to a screenshot with the formula used in Column B and some sample data
I have a spreadsheet with 48 rows of data in column A
The values range from 0 to 19
The average of these 48 rows = 8.71
the standard deviation of the population = 3.77
I've used the STANDARDIZE function in excel in column B to return the Z-score of each item in column A given that I know the mean (8.71), std dev (3.77), and x (whatever is in column A).
For example (row 2) has:
x = 2
z = -1.779
Using the z value, I want to create an lower (4) and upper (24) boundary and calculate what the value would be in this 3rd column.
Essentially, if x = 0 (min value), then z = -2.3096, and columnC = 4 (lower boundary condition)
Conversely, if x = 19 (max value), then z = 2.9947, and columnC = 19 (upper boundary condition)
and then all other values between 0 to 19 would be calculated....
Any ideas how I can accomplish this with a formula in the column C?
So if your lowest original value is 0 and your highest is 19 and you want to re-distribute them from 4 to 24 and we assume that both are linear that means:
Since both are linear we have to use these formulas:
we develope the first to c so we get
and replace the c in the second equation with that so we get
and develope this to m as follows
If we put this togeter with our third equation above we get:
So we finally have equations for m = and c = and we can use the numbers from our old and new lower and upper bound to get:
you can use these values with
where x is are your old values in column A and y is the new distributed value in column B:
Some visualization if you change the boundaries:
Idea for a non-linear solution
If you want 4 and 24 as boundaries and the mean should be 12 the solution cannot be linear of course. But you could use for example any other formula like
So you can use this formula for column D y2 with the following values a, b, c as well as calculating the mean, min and max over column D y2.
Then use the solver:
Goal is: Mean $M$15 should be 12
secondary conditions: $M$16 = 4 (lower boundary) and $M$17 = 24 (upper boundary)
variable cells are a, b and c: $M$11:$M$13
The solver will now adjust the values a, b and c so that you get very close to your goal and to get these results:
The min is 4 the max is almost 24 and the mean is almost 12 that is probably the closest you can get with a numeric method.

Concatenate values in columns/rows and getting the value of the result

I have an excel worksheet with something like below. The desired output are as shown in column G and the last row, which concatenates all the values in columns/rows and gives the value of the resultant expression.
It's actually some kind of a puzzle where the aim is to replace some numbers with operators such that the values in G matches with a specified value. I am replicating the same in excel from a paper version.
A B C D E F G
3 3 33
4 1 + 2 43
4 0 + 5 6 4 604
7 3 2 / 1 2 61
3 7 2 7 3727
3 0 30
47 4033 304 0.4 2617 42
I have tried the following formula:
="="&A2&B2&C2&D2&E2&F2
However it gives the result in text format instead of 43, which I expected:
=41+2
Is there any way to give the final output as 43.
I developed (recorded and modified) a macro which simply copies the above result, paste it as values in Column H and then run 'text to columns' on the column H which gives me the desired output exactly as expected. However, I still can't figure out how to do the same with the row.
Is there any way to achieve the above with a formula only?
I want to avoid using macros as the undo functionality is lost but suggestions are welcome.
You could create a UDF that uses Application.Evaluate to evaluate the text string into a value. Then your formula would just need =Eval(A2&B2&C2&D2&E2&F2)
Function Eval(txt As String)
Eval = Application.Evaluate(txt)
End Function

How do I define multiple formulas in excel?

I have no idea if this is the right place to ask this, but I am really struggling with excel. I am trying to define two formulas in excel, and then make a 2 variable data table to run these formulas through.
My formulas are:
Q = SQRT( 2*U*A ) / SQRT(h) , and if you use that best quantity Q then the acquisition and holding costs yield a corresponding TOTAL COST = SQRT( 2*U*A ) * SQRT(h).
We are then given a range of values for U and h, with A constant.
How do I define these equations in excel?
Here's a start, type this in (without the A B C... and 1,2,3... at top):
A B C D E
1 A U h Q Total
2 123 1 100 =SQRT(2*B2*$A$2)/SQRT(C2) =SQRT(2*B2*$A$2)*SQRT(C2)
3 2 200 =SQRT(2*B3*$A$2)/SQRT(C3) =SQRT(2*B3*$A$2)*SQRT(C3)
4 3 300 =SQRT(2*B4*$A$2)/SQRT(C4) =SQRT(2*B4*$A$2)*SQRT(C4)
Also, have a look at https://faculty.fuqua.duke.edu/~pecklund/ExcelReview/2001_Documents/2001XLGettingStarted.pdf

Resources