Excel, look values in table where both rows and columns are ranges - excel

I apologise if the title is confusing. it's hard for me to summarise this issue in one sentence.
I'm trying to automate some spreadsheets, but sadly using VBA is not an option (most people here get confused by them and end up avoiding those spreadsheets).
The problem: I have rows in one sheet with data for velocity and angle and I'm trying to get a value from this other table based on those parameters.
The issue is that this other table is based in ranges of values for both columns and rows.
A B C D E
1 0 1-30 31-60 61-90
2 0 to 1 10 20 20 30
3 1.1 to 2 10 20 30 30
4 2.1 to 3 20 30 30 40
5 '>3 30 40 40 40
Where column A is velocity range, Row 1 is angle range
So for example if I have a velocity of 1.5 m/s with an angle of 40°, I want to be able to get the result of 30.
My best idea is to create auxiliary or helper columns to indicate which velocity and angle range they belong to and then use a VLOOKUP MATCH combo.
Even though it's a simple solution, I just wanted to know if there is a more elegant solution available that comes to mind or if you think this is already elegant enough.
Thanks.

As Scott mentioned, using ranges as titles makes it difficult.
If you simply put in the minimums instead youcan make the following:
A B C D E F G H
1 0 1 31 61 Velocity 1.5
2 0 10 20 20 30 Angle 40
3 1.1 10 20 30 30 Result 30
4 2.1 20 30 30 40
5 3.1 30 40 40 40
Where H1 and H2 are your input cells.
H3 gives you the result with: =INDEX(B2:E5,MATCH(H1,A2:A5,1),MATCH(H2,B1:E1,1))

Related

Excel MERGE two tables

I have SET 1
CLASS
Student
TEST
SCORE
A
1
1
46
A
1
2
50
A
1
3
45
A
2
1
45
A
2
2
47
A
2
3
31
A
3
1
34
A
3
2
45
B
1
1
36
B
2
1
31
B
2
2
41
B
3
1
50
C
1
1
42
C
3
1
31
and SET 2
CLASS
SIZE
YEARS
A
39
7
B
20
12
C
31
6
and wish to COMBINE to make SET 3
CLASS
STUDENT
TEST
SCORE
SIZE
YEARS
A
1
1
46
39
7
A
1
2
50
39
7
A
1
3
45
39
7
A
2
1
45
39
7
A
2
2
47
39
7
A
2
3
31
39
7
A
3
1
34
39
7
A
3
2
45
39
7
B
1
1
36
20
12
B
2
1
31
20
12
B
2
2
41
20
12
B
3
1
50
20
12
C
1
1
42
31
6
C
3
1
31
31
6
so basically add the SIZE and YEARS columns from SET 2 and merge on CLASS onto SET 1. In excel how you can do this? I need to match on CLASS
Define both sets as tables and “left join” in PowerQuery. There you can choose the columns of the resulting table.
https://learn.microsoft.com/en-us/power-query/merge-queries-left-outer
If you have Set 1 on the top left of a worksheet "Set1" and Set 2 on the top left of a worksheet "Set2", then you can use the formula
=VLOOKUP(A2;'Set2'!$A$2:$C$4;2;FALSE), where $A$2:$C$4 is the range of Set2, and A2 is the class value from Set1, which is what is used to do the lookup in Set2. The next argument, 2, means to take the second row from Set2, and the FALSE at the end means that you only want exact matches on the CLASS. You can do auto-fill with this formula, and do similar steps for the years. If you look up the help for VLOOKUP within Excel, that should help you to understand how it works.
Your first set of data is essentially your primary set of data that you just want to add attribute columns to. I built this example on Google Sheets which should help explain. Using spill formulas, only a few cells are needed with their own formulas. You can see them as they are highlighted in yellow. When you use in Excel, obviously make sure you change the column references, but this would get you the answer.
Note you have to have SpillRange in Excel for this to work. To test, see if you have the formula =unique()
This solution may work for you if both sets start in the same column. As example in my image, both of them start at column A. You can get all data with a single VLOOKUP formula:
Formula in cell E2 is:
=VLOOKUP($A2;$A$22:$R$25;COLUMN($B22);FALSE)
Notice the mixed references at first and third argument and absolute references in the second one. Third argument is critical, because is the relational position between both sets, that's the reason it's easier if both sets start at same column. If not, you'll need to adjust this argument substracting or adding, depending on the case.
Anyways, with a single formula, you can get any number of columns. The only disavantage of this formula is that you need to manually drag to right until you got all the columns (10, 30 or whatever). You'll notice you are done because the formula will raise an error:
This error means you are trying to get a referenced outside of your column area.

Excel Multiply one set of values with given matrix and sum results

I need to redistribute values for Old entries by using new distribution in a given table.
Example:
Need to redistribute using given % in this table:
So New Value of Element 1 = 99% * old1 + 7% * old2 + 3% * old3 + 26% * old5
This is not whole table, it is pretty large. There must be a simpler way than adding things up manually.
You can use the MMULT() worksheet function for that.
Example:
A B C D E F
1 Amount
2 100
3 200
4 500
5 400
6
7 Percentages 1 2 3 4
8 99 7 3 26 =MMULT(B8:E8;A$2:A$5)/100
9 1 93 34 0 =MMULT(B9:E9;A$2:A$5)/100
10 0 0 63 74 =MMULT(B10:E10;A$2:A$5)/100
For your information: I've entered the first formula in F8, and dragged and dropped until F10.

IF formula too long

I’m trying to create a formula that will display mileage from one place to another.
Example: column one is location combinations (there are 39 locations and multiple combinations)
Eg-sams to Petes, sams to mc d, mc d to sams etc.
Last column with formula would automatically place mileage from point a to point b. Etc
The formula i created was IF but way too long
=IF(B12="SVES TO KHS",11,
IF(B12="SVES TO FRHS",4.1,
IF(B12="SVES TO CHS",6.9,
IF(B12="SVES TO KMS",9.5,
IF(B12="SVES TO ISM",6.2,
IF(B12="SVES TO HM",5.3,
IF(B12="SVES TO FHM",2.4,
IF(B12="SVES TO TSM",7.6,...
Is there a way to shorten the formula?
Best thing to do is create a separate table on sheet 2, in column a have a list of answers "SVES TO ", column b the miles. Then use a vlookup to find the miles
=Vlookup (b12, sheet 2!'a1:b50,2,0)
In this example there are 50 different SVES TO examples, change it to however many you have.
An X-to-Y/Y-to-X distance matrix should be your best bet. While VLOOKUP is good for a one-column-lookup/one-column-retrieval, an INDEX/MATCH/MATCH would be more appropriate for a true matrix.
Assume the folowwing data matrix with destinations along the first row and the first column.
a b c d e f g
a - 40 80 17 37 16 70
b 40 - 48 95 85 8 60
c 80 48 - 24 26 75 73
d 17 95 24 - 14 9 56
e 37 85 26 14 - 91 7
f 16 8 75 9 91 - 78
g 70 60 73 56 7 78 -
Note that distances like c-to-f are the same as f-to-c. (yes, there i a simple formula for this but that is another question). Obviously, any x-to-x or y-to-y should be zero when x = y.
In the sample image below your formula in L2 should be,
=INDEX($B$2:$H$8, MATCH(J2, A$2:A$8, 0), MATCH(K2, B$1:H$1, 0))
The cell and row highlighting were added with a couple simple conditional formats formulas.

How to generate random numbers from different intervals that add up to a fixed sum in excel?

I need to generate 13 numbers from 13 different intervals which will add up to 1360. In the chart below, "index" means the index of the 13 different numbers. Mean means the mean (average) of the intervals. The range will be plus or minus 15% of the mean as shown below. I will prefer to have the random numbers generated based on the normal distribution with N(mean, 7.5% of mean). I take it back. No normal distribution. Please use +- 15% as hard limits of the intervals.
It will be great if anyone could figure out how to do it in excel. Algorithms will be appreciated as well.
Index mean 15% low high
A 288 43 245 331
B 50 8 43 58
C 338 51 287 389
D 50 8 43 58
E 16 2 14 18
F 66 10 56 76
G 118 18 100 136
H 17 3 14 20
I 91 14 77 105
J 26 4 22 30
K 117 18 99 135
L 165 25 140 190
M 18 3 15 21
I would sort the table by increasing mean:
and use a column for a helper value (column H above).
The idea is to maintain -- while going to the next row -- the current deviation from a perfect aim for the final target. Perfect would mean that every random value coincides with the mean for that row. If a value is 2 less than the mean, then that 2 will appear in the H column for the next row. The random number generated for that next row will then not aim for the given mean, but for 2 less than the mean. The range for the random number will appropriately be reduced so that the low/high values will never be crossed.
By first sorting the rows, we can be sure that this corrected mean will always fall within the next row's low/high range, and so it will always be possible to generate an acceptable random number there.
The final value will be calculated differently: it will be the remainder that is needed to achieve the target sum. For the same reason as above, this value is guaranteed to be within the low/high range.
The formulas used are as follows:
| F | H
--+--------------------------------------------------+------------------------------
2 | =RANDBETWEEN(D2, E2) |
3 | =RANDBETWEEN(B3+H3-C3+ABS(H3), B3+H3+C3-ABS(H3)) | =SUM($B$2:$B2)-SUM($F$2:$F2)
4 | (copy above formula) | (copy above formula)
...| ... | ...
13 | (copy above formula) | (copy above formula)
14 | =SUM($B$2:$B14)-SUM($F$2:$F13) |
In theory the rows do not need to be sorted first, but then the formulas cannot be copied down like above, but must reference the correct rows. That would make it quite complicated.
If it is absolutely necessary that the rows are presented in order of the Index column (A, B, C...), then use another sheet to do the above. Then in the main sheet read the value into the F column with a VLOOKUP from the other sheet. So in F2 you would have:
=VLOOKUP(A2, OtherSheet!$A$2:$F$14, 6, 0)
Get the random number like this
num = Int ((300 - 200 + 1) * Rnd + 200) //between 200 and 300
Click here for more information
and the random number need to be the total sum minus the sum that you already got and the last one will be that left.
for example: (if we have 4 numbers sum up to 100)
A is a random number between 0 to 100 //lets say 42
then B is a random number between 0 to (100-42) => 0 to 78 //lets say 18
then C is a random number between 0 to (100-42-18) => 0 to 40 //lets say 25
then, in the end D is 100-42-18-25 => D is 15
*100-42-18-25 is the same as 100-Sum(A,B,C)
Here is my example generate random number based on low and high.
The formula in column F is just a RANDBETWEEN:
=RANDBETWEEN($D2,$E2)
Then you can get the result always equal to 1360 with the formula below for column G:
=F2/SUM($F$2:$F$14)*1360
So cell G15 will always be 1360 which is the sum of all those 13 intervals.

AGGREGAT with critiera and duplicates in array

I have the following Excel spreadsheet:
A B C D E
1 ProdID Price Unique ProdID 1. Biggest 2. Biggest
2 2606639 40 2606639 50 50
3 2606639 50 4633523 45 35
4 2606639 20 3911436 25 25
5 2606639 50
6 4633523 45
7 4633523 20
8 4633523 35
9 3911436 20
10 3911436 25
11 3911436 25
12 3911436 15
In Cells D2:E4 I want to show the 1. biggest and 2. biggest price of each ProdID in Column A. Therefore, I use the following formula:
D2 =AGGREGAT(14,6,$B$2:$B$12/($A$2:$A$12=$C2),1)
E2 =AGGREGAT(14,6,$B$2:$B$12/($A$2:$A$12=$C2),2)
This formula works as long as the prices are unique in Column B as you can see on the second ProdID (4633523).
However, once the price is not unique in Column B (for example 50 for ProdID 26026639 and 25 for ProdID 3911436) the functions in Cells D2:E4 does not show the right results.
Do you have an idea if you can solve this issue with the AGGREGAT-Formula and wihtout using an ARRAY-Formula?
you could check number of occurences of the first ProdID-price combinations and use that in the last argument of the AGGREGAT function. So instead of
=AGGREGAT(14,6,$B$2:$B$12/($A$2:$A$12=$C2),2)
you would have
=AGGREGAT(14,6,$B$2:$B$12/($A$2:$A$12=$C2),2+COUNTIFS(A:A,C2,B:B,D2)-1)
of course you can just put "1+COUNTIFS..." but I put it this way so it can be better understood that it uses position 2 + number of occurences of the combination of ProdID with biggest number after the first occurence.

Resources