How to Find Top 3 Nearest Location Given Lat Long in Excel? - excel

so I have 2 sheets in Excel named Sheet1 and Sheet2. The table structure simply like below:
Sheet1
Sheet2
I want to find 3 nearest location from Sheet1 to Sheet2. Example of expected output should be:
I already tried =LOOKUP(1,1/FREQUENCY(0,MMULT((Sheet2!B$2:C$5-Sheet1!B2:C2)^2,{1;1})), Sheet2!A$2:A$5) but it only gave me 1 nearest location only.
Excel version: Excel 365
Thanks in advance!

Right, at risk of having a faulty calculation of distance, try:
Formula in E1:
=REDUCE("Nearest #"&{1,2,3},ROW(A2:A6),LAMBDA(x,y,VSTACK(x,TAKE(TOROW(SORTBY(A9:A13,ABS(INDEX(B:B,y)-B9:B13)+ABS(INDEX(C:C,y)-C9:C13))),,3))))
Using a calculated distance of ABS(lat1-lat2)+ABS(long1-long2) gave me the exact same results as trying to find the actual distance in, say miles, using other more intricate formulae like this one. Are you sure about your desired results?

So an example of vlookup() being used to do a straight line interpolation between two X values to give the calculated Y value.
=VLOOKUP(A2,$A$5:$B$8,2,1)+((A2-(VLOOKUP(A2+5,$A$5:$B$8,1,1)-VLOOKUP(A2,$A$5:$B$8,1,1)))/(VLOOKUP(A2+5,$A$5:$B$8,1,1)-VLOOKUP(A2,$A$5:$B$8,1,1)))*(VLOOKUP(A2+5,$A$5:$B$8,2,1)-VLOOKUP(A2,$A$5:$B$8,2,1))
Used this to get temperatures, density etc from steam tables aka tables of the properties of water and air.

I've used the Pythagoras distance to calculate nearest location using SUMPRODUCT and INDEX.
Sheet1:
Sheet2:
Notice I've added just a test column with Alpacca and a rank column just to check my output but you don't need them.
Array formula (you must introduce it pressing CTRL+SHIFT+ENTER:
=INDEX(Sheet2!$A$2:$A$6,SUMPRODUCT(--(SMALL (SQRT (($B2-Sheet2!$B$2:$B$6)^2+($C2-Sheet2!$C$2:$C$6)^2), RIGHT (D$1,1))= SQRT (($B2-Sheet2!$B$2:$B$6)^2+($C2-Sheet2!$C$2:$C$6)^2))*ROW(Sheet2!$A$2:$A$6))-1)
If, by any chance, 2 or more locations are exactly at the same distance, the formula won't work and will return wrong output.

Related

Find closest value in a Poisson distribution table

I am trying to find the closest value in this Poisson Distribution table to help create some variation in my NHL game simulator. Right now I have the minimum value set to =(MAX(H4:R14)/1.25) and the max as MAX(H4:R14). My rand (random) value is set to =RAND()*($V$4-$U$4)+$U$4. My question is, is how do you find the closest value in the table when compared to the random value? Returning the closest match (percentage) is ideal and the table's values will change whenever the teams change. Eventually, I am trying to return the respective column and row values (goals for each team), but this is step 1 and haven't been able to figure it out yet - even with index and match. Feel like it should be fairly straightforward...
You may benefit from SUMPRODUCT and array formula to get closes match and goals values in rows and section. Just as example:
The upper table would be your data. The bottom table is just to check it it works, you can delete. This bottom table is just an absolute value of cell minus your rand. The minimun one is highlighted in green and that's the closest match. Notice how the formulas get the position of that match and the goals values:
Formula to get ROW VALUE:
=SUMPRODUCT(--(ABS(H4:R14-U6)=MIN(ABS(H4:R14-U6)))*G4:G14)
Formula to get COLUMN VALUE:
=SUMPRODUCT(--(ABS(H4:R14-U6)=MIN(ABS(H4:R14-U6)))*H3:R3)
Both of this formulas are array formulas, so to introduce them yo must press CTRL+ENTER+SHIFT or they won't work!
To get the closest match is just INDEX classic formula (we add +1 due to goals indexed as 0,1,2...):
=INDEX(H4:R14,U9+1,V9+1)
See it working!:
I've uploaded the file in case you want to check the formulas:
https://docs.google.com/spreadsheets/d/1wV8bn6B-jZ4jCxAonGdWFMluAEybzwkA/edit?usp=share_link&ouid=114417674018837700466&rtpof=true&sd=true
Please, notice this will work properly as far as there is a single closest match. If by any case two cells are both closest match, formula will return incorrect result.
And remember, as I said before, second table is just to explain the solution, you can delete it and everything will work.
You could create a 2nd table where you calculate the absolute or squared difference between your Poisson table values and the random value.
Then you can simply check for the smallest value in the 2nd table.
In your example, the 2nd table of differences could be created with the formula
=ABS($H$4:$R$14-$U$6)
Let's assume that this 2nd table is just below the 1st table, i.e. in H16:R26
Then the smallest value in this 2nd table is obtained by
=SMALL($H$16:$R$26,1)
from there you can get the row number through
=SUM(($H$16:$R$26=SMALL($H$16:$R$26,1))*($G$4:$G$14))
and the column number through
=SUM(($H$16:$R$26=SMALL($H$16:$R$26,1))*($H$3:$R$3))
Then, to find the closest match in the original table, you can use the INDEX() function and refer to the row and column calculated in the previous step.
You can of course skip all the intermediary steps and write everything into a single formula:
=LET(
table,$H$4:$R$14,
diff,ABS(table-$U$6),
closest,SMALL(diff,1),
closest_row,SUM((diff=closest)*SEQUENCE(ROWS(table))),
closest_col,SUM((diff=closest)*SEQUENCE(1,COLUMNS(table))),
INDEX(table,closest_row,closest_col)
)
Which looks like this:
Note: the last formula uses dynamic arrays and functions only available Excel O365.

EXCEL calculating average with OFFSET (coord unspecified) of a given keyword

Fun situation I'm trying to calculate. Basically in one row I have names of products, and the row to the right of it the number of days that have passed since the product was first received.
The calculation to do the days for ex is
=TODAY()-BB2
What I'm trying to do NOW, is identify let's say the product word "truck," and then calculate how many days on average the holding time of truck is.
I understand the logic of the formula I need, just not how to execute precisely. Basically the formula is going to need to use this average calculator with a keyword identified COUNTIF
=COUNTIF($A$2:$A$900,"TRUCK")/COUNTA($A$2:$A$900)
What I'm missing is some type of...IF "TRUCK, OFFSET (GIVEN CELL) -1)
Thanks for any thoughts!
-Wilson
A formula (in C1 in the example):
=AVERAGEIF(A:A,"truck",B:B)
should work but I would recommend a PivotTable for the additional functionality it provides:

Excel: Sumif Multiple Columns based on the same Criteria

So I am trying to create a spreadsheet at allows a character in game to total the party inventory.
I am trying to sumif multiple columns based on the same criteria.
So say I am trying to sum all the rope they have.
In column A is the Item descriptions
In columns B-E are the different totals for each party member (one column per person)
Each party member has 50 rope, so I am expecting 200 rope.
I have used this formula:
=SUMIF(A:A,"Rope: Hemp",B:E) and it is only returning 50 as a value, If I utilise cell values (A1:A100 etc.) it returns a value of Zero.
I have been told that Sum Product could works so I also tried that:
=SUMPRODUCT((A1:A100="Rope: Hemp")*(B3:E100)) and I still get the incorrect result.
What am I doing wrong?
EDIT:
Here are some photos.
Here is my Raw Data. As you can see I have the inventory and tallies, when you look at rope it says 150, and this was calcualted by summing the B to E cells, however as the list is going to move and grow I thought SUMIF would be better.
As stated above, I have used a SUMIF making the range Columns B through E, and it only returns a value of 50 (I'm assuming Column B)
so came up with this:
=SUMIF(A3:A40,"="&J17&"",B3:B40)+SUMIF(A3:A40,"="&J17&"",C3:C40)+SUMIF(A3:A40,"="&J17&"",D3:D40)+SUMIF(A3:A40,"="&J17&"",E3:E40)
Which works, but I can only assume that sumif only work with ONE target range... And I tried curly brackets as well...
So, I did also use cell J17 for the object you are looking for, so you can drag it down, the "*" will find all occurences of "Rope: Hemp", or "Rope: Nylon" etc. I get a total of 150 as there are only 3 characters with rope...
Hope it helps. Someone else may have a better / neater suggestion!
I just tested it by entering Rope, rapier and rations into J17 and got the results I expect.
Image of spreadsheet:

Excel: Need to sum the lowest 5 values from a column of 10

Trying to build an Excel formula that will sum the 5 lowest golf scores out of 10. Currently using a table setup, provided by another forum, but it's not quite getting me there, as I think it's ignoring duplicates. The letter 'E' should also be equal to the value of zero.
Screen shot: http://imgur.com/G8xoul9
Here are the formulae in use, identified by cell:
G17: =SUM(IF(G5:G14<=G25,G5:G14)/COUNTIF(G5:G14,G5:G14))
G21: =MIN(G5:G14)
G22: =SMALL($G$5:$G$14,COUNTIF($G5:$G14,"<="&G21)+1)
G23: =SMALL($G$5:$G$14,COUNTIF($G5:$G14,"<="&G22)+1)
G24: =SMALL($G$5:$G$14,COUNTIF($G5:$G14,"<="&G23)+1)
G25: =SMALL($G$5:$G$14,COUNTIF($G5:$G14,"<="&G24)+1)
The result in G17 should be -7, but I'm not getting there just yet. Your generous help will be greatly appreciated. Thank you!
Try this, entered as an array formula (CTRL-ENTER):
=SUM(SMALL(scores,{1,2,3,4,5}))
where scores is the range of cells with your scores
Update for Google Sheets
This can also work in Google Sheets, but it seems like it requires an ArrayFormula. You enter the above with CTRL-SHIFT-ENTER or manually type the array formula:
=ArrayFormula(SUM(SMALL(scores,{1,2,3,4,5})))
You have made this more confusing than it needs to be.
Assume your golf scores are in any number of rows in column A. In column B, we want to pick up the 5 lowest scores. This can be written as follows, starting with B1 and copied down for as many low places you want to check:
=Small(A:A,row())
row() returns the current row that the formula is on (to start with, row 1, with B1).
Then on cell C1, simply sum these results, and that's all you need:
=SUM(B:B)

How do I sum only positive numbers from another column that must match certain criteria in my first column?

I have about 500 item #s in Column A that represent all of the different items we put in or out of production during the week.
In Column F I have the total lbs of that item with positive or negative numbers reflecting in to production and out of production respectively.
I already have the following formula setup to tell me what the total loss/gain is:
=SUMPRODUCT(--(ISNUMBER(SEARCH({"0883","0884","0885"},A6:A280)))*F6:F280)
I would like to then sum only the positive numbers so that I can get a shrink/gain percentage based on the product we put into the room. This is where I am stuck.
I believe you are looking for the SUMIF() function of Excel.
Here's a link to Office.com
You can add another condition as follows:
=SUMPRODUCT(--(ISNUMBER(SEARCH({"0883","0884","0885"},A6:A280))) * F6:F280 * (F6:F280>0))
You can just extend your formula with another condition like this
=SUMPRODUCT(ISNUMBER(SEARCH({"0883","0884","0885"},A6:A280))*(F6:F280>0),F6:F280)
What sort of values are in A6:A280? Which version of Excel are you using? - if 0883 etc. will be the full cell contents then you don't really need SEARCH and you might be able to use SUMIFS which will be more efficient, i.e. this version given your comments
=SUM(SUMIFS(F6:F280,A6:A280,{"0883","0884","0885"},F6:F280,">0"))

Resources