Forum Discussion
Measuring Distance to find closest point between points from a single table
- 7 years ago
OK, I took AlBs code and Anonymouss last post and turned it into a measure. First, I recreated 'Route Information' to have 5 dates' worth of data, one date of which is missing 3 of the locations (Dt is literally just a list of 5 dates 1/14 - 1/18):
Route Information = UNION ( CROSSJOIN ( SUMMARIZE ( 'Table2 (2)', 'Table2 (2)'[MEMNO], 'Table2 (2)'[Post Code], 'Table2 (2)'[Lattitude], 'Table2 (2)'[Longitude] ), FILTER ( Dt, Dt[Date] < "1/18/18" ) ), CROSSJOIN ( SUMMARIZE ( FILTER ( 'Table2 (2)', NOT ( 'Table2 (2)'[MEMNO] IN { "A21200", "A22701", "B10500" } ) ), 'Table2 (2)'[MEMNO], 'Table2 (2)'[Post Code], 'Table2 (2)'[Lattitude], 'Table2 (2)'[Longitude] ), FILTER ( Dt, Dt[Date] = "1/18/18" ) ) )Then taking the working code, transformed it into a measure, keeping the Date filter (I'm probably not doing it the most efficient way using variables - I still have trouble getting ALLEXCEPT and KEEPFILTER working without trial and error):
DCH (Miles) = VAR HouseLatitude = MIN ( 'Route Information'[Lattitude] ) VAR HouseLongitude = MIN ( 'Route Information'[Longitude] ) VAR EarthCircumference = 3959 VAR P = DIVIDE ( PI (), 180 ) VAR House = SELECTEDVALUE ( 'Route Information'[MEMNO] ) VAR __Dt = SELECTEDVALUE ( 'Route Information'[Date] ) RETURN MINX ( FILTER ( ALL ( 'Route Information' ), 'Route Information'[MEMNO] <> House && 'Route Information'[Date] = __Dt ), VAR CinemaLatitude = 'Route Information'[Lattitude] VAR CinemaLongitude = 'Route Information'[Longitude] VAR _DistanceFromCurrentRough = 80 * SQRT ( POWER ( ( HouseLatitude - CinemaLatitude ), 2 ) + POWER ( ( HouseLongitude - CinemaLongitude ), 2 ) ) RETURN IF ( _DistanceFromCurrentRough <> 0, _DistanceFromCurrentRough ) )As you can see, on 1/18/18, the distance is different for M49418
Hi AlB
Yeah i have tried the Calculated column on the full data set and prior to any slicers been applied have managed to verify the distances produced are very close to accurate, 100% accurate enough for what i intend.
Slight issue when we filter the data though, so there is a Date column which will date when these points are relevant or occured over the previous 7 days.
If i filter to only show houses on the 16th January as an example the closest distance remains the same but its closest may be from a house on the 18th January so in this scanario wouldn't be a possible closest.
Based on the conversation i did try creating a caluclated measure with the same formula without much sucsess it doesn't relate to the MEMNO so wont display the values for the closest distance in a table view using the MEMNO.
So it is working from a total dataset view but the information wont update as we filter down for a specific date or only houses with a closest cinema of Doncaster Vue as an example.
How would be make the same working formula work with filtered data, as a measure?
Thanks though from a dataset point of view its great and from what points ive validated is fairly accurate.
Thanks,
Josh
OK, I took AlBs code and Anonymouss last post and turned it into a measure. First, I recreated 'Route Information' to have 5 dates' worth of data, one date of which is missing 3 of the locations (Dt is literally just a list of 5 dates 1/14 - 1/18):
Route Information =
UNION (
CROSSJOIN (
SUMMARIZE (
'Table2 (2)',
'Table2 (2)'[MEMNO],
'Table2 (2)'[Post Code],
'Table2 (2)'[Lattitude],
'Table2 (2)'[Longitude]
),
FILTER ( Dt, Dt[Date] < "1/18/18" )
),
CROSSJOIN (
SUMMARIZE (
FILTER (
'Table2 (2)',
NOT ( 'Table2 (2)'[MEMNO] IN { "A21200", "A22701", "B10500" } )
),
'Table2 (2)'[MEMNO],
'Table2 (2)'[Post Code],
'Table2 (2)'[Lattitude],
'Table2 (2)'[Longitude]
),
FILTER ( Dt, Dt[Date] = "1/18/18" )
)
)Then taking the working code, transformed it into a measure, keeping the Date filter (I'm probably not doing it the most efficient way using variables - I still have trouble getting ALLEXCEPT and KEEPFILTER working without trial and error):
DCH (Miles) =
VAR HouseLatitude = MIN ( 'Route Information'[Lattitude] )
VAR HouseLongitude = MIN ( 'Route Information'[Longitude] )
VAR EarthCircumference = 3959
VAR P = DIVIDE ( PI (), 180 )
VAR House = SELECTEDVALUE ( 'Route Information'[MEMNO] )
VAR __Dt = SELECTEDVALUE ( 'Route Information'[Date] )
RETURN
MINX (
FILTER (
ALL ( 'Route Information' ),
'Route Information'[MEMNO] <> House
&& 'Route Information'[Date] = __Dt
),
VAR CinemaLatitude = 'Route Information'[Lattitude]
VAR CinemaLongitude = 'Route Information'[Longitude]
VAR _DistanceFromCurrentRough =
80 * SQRT ( POWER ( ( HouseLatitude - CinemaLatitude ), 2 ) + POWER ( ( HouseLongitude - CinemaLongitude ), 2 )
)
RETURN
IF ( _DistanceFromCurrentRough <> 0, _DistanceFromCurrentRough )
)As you can see, on 1/18/18, the distance is different for M49418