Does this sound feasible?
Use an array formula that constructs an array of values that are blank when there is no Engine otherwise the value is Time + Distance/1000.
The position of the smallest element in this array will be the same as the position of the Station number in $A$7:$A$73 where there is an engine and the Time is the smallest and the Distance is the shortest.
Similarly for the second (and third) smallest element, its position corresponds with the position of the Station where there is an Engine and the Time is the second smallest (which could be the same as the smallest Time) and the Distance is the second smallest (which could be the same as the smallest Distance).
The part of the final array formula that generates this array is...
The smallest element in this array is returned by...
When the formula is filled down two more rows the ROW($Q1) increments to ROW($Q2) then ROW($Q3) to return the second and third smallest elements.
The position of the smallest array element, and hence the position of the appropriate Station, is found using the MATCH function...
.
The Station number is then found using the value returned by the MATCH function inside the INDEX function acting on $A$7:$A$73...
Filling this formula down 2 more rows yields the second and third closest Stations (with respect to Time first then Distance second).
If fewer than 3 Stations satisfy the "Has Engine" criterion then one or more of the three formulas will return #NUM! error.
If you prefer the error message to be bypassed and replaced with a blank cell instead then the ROW($Qn) part can be compared with a count of the non-blank elements in the array generated by...
like this...
Filled down 2 more rows then across 1 more column for the corresponding Stations with a Ladder.
See attached doc.
Please note that the formula would fail to distinguish between different Stations satisfying all three criteria with exactly the same values.
For example, say two Stations with numbers m and n (m<n) both have an Engine, and both have the same Time and Distance values. They will both result in the same value in the array returned by...
The array element corresponding to Station m will appear closer to the top of the array than will the same value element corresponding to Station n.
If the array element corresponding to Station m is the kth smallest array element then Station n's array element will be the (k+1)th smallest element.
When the MATCH function looks for the kth smallest element it will correctly return the position of Station m.
However, when it looks for the (k+1)th smallest element it will again return the position of Station m, not the position of Station n.
Hopefully the likelihood that your data could contain different Stations with identical Time and Distance values, while also being in the possession of an Engine or a Ladder, is so remote that it need not be considered.
Otherwise a more complicated formula will be required to break such ties.
Bookmarks