My spreadsheet contains data from hospital patients, based on different criteria, and one of them are household's coordinates. The question is which household is the nearest neighbour to the household that contains the most people, based on straight-line distances and return the hserial value for the household. I have the households identified by their serial number:
hserial | hhcoords | persnum | ||
---|---|---|---|---|
101051 | 346350 | 1 | ||
101151 | 347312 | 1 | ||
101201 | 433616 | 1 | ||
101271 | 609464 | 1 | ||
101271 | 609464 | 2 | ||
101351 | 228562 | 1 | ||
101351 | 228562 | 2 | ||
101351 | 228562 | 3 | ||
101371 | 556408 | 1 | ||
101371 | 556408 | 2 |
hhcoords: (x,y) coordinates of the household location. First three digits are x, last three digits are y. persnum: number of persons within the household hserial: is the serial number of the household
Once you have their coords you can calculate the straightline distance between all houses, using Pythagorean Theorem.
For example, if we have the coords for a house 1 (x1,y1), and we want to know how far away it is from house 2 (x2,y2) then we can calculate a straight line distance using:
√((x1 – x2)2 + (y1 – y2)2) The nearest neighbour is the one with the shortest distance (but careful with zeros).
I can't alter or change any of the data, even if I identify an error.
I think using the MAXIF formula would be useful as well as filtering it with FILTER, but I don't know how to aplly it to this case.
EDIT: This is my code but I have the #VALUE error:
=LET(hserial,UNIQUE(Table2[[#Headers],[hserial]]),hhcoords,UNIQUE(Table2[[#Headers],[hhcoords]]),hserial1,hserial,hserial2,TRANSPOSE(hserial),getXλ,LAMBDA(h,LEFT(INDEX(hhcoords,MATCH(h,hserial,0)),3)),getYλ,LAMBDA(h,MID(INDEX(hhcoords,MATCH(h,hserial,0)),4,3)),m,SQRT((getXλ(hserial1)-getXλ(hserial2))^2+(getYλ(hserial1)-getYλ(hserial2))^2),posMaxValue,MATCH(MAX(m),TOCOL(m),0),firstHH, CHOOSEROWS(hserial1,INT(posMaxValue/COUNT(hserial1)+1)),secondHH,INDEX(hserial1,MOD(posMaxValue,COUNT(hserial1))),HSTACK(firstHH,secondHH,MAX(m)))
You can use this formula:
Maybe you don't have
HSTACK
- then you would have to use three formulas.