I am currently trying to create a calculated column that takes the latitude, longitude, and radius of locations and returns a list of locations that overlap radius for each location in the table.
I have the logic figured out i think, but i am running into issues with either syntax or it might be limitations of dax itself with the way that table variables work. any ideas on how to skin this?
It almost seems like i need to create a real table for each record to add the calculated column to, but this is not a scalable solution for 4000+ locations
Overlapping GLIDs =
//storing values for current row
VAR __GLID1 = [GLID]
VAR __lat1 = [POS_LATITUDE]
VAR __long1 = [POS_LONGITUDE]
VAR __radius1 = [POS_RADIUS]/1000 //converting radius from m to km
//storing a duplicate location table as a table variable
VAR table1 = SUMMARIZE('LocTable1','LocTable1'[GLID],'LocTable1'[POS_LATITUDE],'LocTable1'[POS_LONGITUDE],'LocTable1'[POS_RADIUS])
//haversine formula to calculate distance between stored row and each row in the stored table
VAR __radiusEarth = 6371 // earth radius in kilometers
VAR __multiplier = PI()/180
VAR __latDiff = Calculate((selectedvalue(LocTable1[POS_LATITUDE])-__lat1) * __multiplier,table1)
VAR __lonDiff = Calculate((selectedvalue(LocTable1[POS_LONGITUDE])-__long1) * __multiplier,table1)
VAR __formula1 = Calculate(SIN(__latDiff/2) * SIN(__latDiff/2) +
COS(selectedvalue('LocTable1'[POS_LATITUDE]) * __multiplier) * COS(__lat1 * __multiplier) *
SIN(__lonDiff/2) * SIN(__lonDiff/2),table1)
VAR __formula2 = Calculate(2 * ATAN(DIVIDE(SQRT(__formula1),SQRT(1-__formula1))),table1)
VAR __distance = Calculate(__radiusEarth * __formula2,table1)
//add distance column to stored table and filter table to locations whos radius+the stored row's radius is less than distance between locations
VAR table2 = addcolumns(table1,"distance",__distance)
VAR table3 = calculatetable(
filter(table2,
[distance]<(__radius1+(selectedvalue(LocTable1[POS_RADIUS])/1000))
))
//turn filtered table into a string of "overlapping GLIDs"
VAR __ListGLIDs = Concatenatex(table3,[GLID],"^")
RETURN
__ListGLIDs
I ended up creating a second table that had a row for each location pairing, including lat, long, and radius columns for both. turned 4k rows into 16k rows. added a calculated column for distance with haversine formula
VAR __radiusEarth = 6371 // earth radius in kilometers
VAR __multiplier = PI()/180
VAR __latDiff = ([POS_LATITUDE1]-[POS_LATITUDE2]) * __multiplier
VAR __lonDiff = ([POS_LONGITUDE1]-[POS_LONGITUDE2]) * __multiplier
VAR __formula1 = SIN(__latDiff/2) * SIN(__latDiff/2) +
COS([POS_LATITUDE1] * __multiplier) * COS([POS_LATITUDE2] * __multiplier) *
SIN(__lonDiff/2) * SIN(__lonDiff/2)
VAR __formula2 = 2 * ATAN(DIVIDE(SQRT(__formula1),SQRT(1-__formula1)))
VAR __distance = __radiusEarth * __formula2
RETURN
__distance
added a column for added radiuses converted to km
([Radius1]+[Radius2])/1000
then added a calculated column in the original table summarizing this second table, filtering to locations that the distance is < the added radius and turning it into a string
Overlapping GLID =
VAR __GLID = 'Location Master'[GLID]
VAR table1 = Filter(summarizecolumns(LocDistanceTable[GLID],LocDistanceTable[GLID2],LocDistanceTable[Added Radius km],LocDistanceTable[Distance km]),LocDistanceTable[GLID]=__GLID)
VAR table2 = Calculatetable(
filter(table1,
LocDistanceTable[Distance km]<LocDistanceTable[Added Radius km] && LocDistanceTable[GLID2]<>__GLID)
)
VAR __ListGLIDs = Concatenatex(table2,[GLID2],"^")
RETURN
__ListGLIDs
and voila!