Skip to main content
Showing results for 
Search instead for 
Did you mean: 

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Helper II
Helper II

Map multiple points within distance of multiple sites

Hi All,

I have a table of crime stats and a table of CCTV sites, see example tables below. I'd like to map crimes around each CCTV site, for example within 500 m of each site as shown below (although not with the site labels, that was just for clarification), 


I can apply the haversine calculation (as shown be Phil Seamark in image below). I assume the best option is to add a calculated column to the crime table. I tried below, but I get a range of errors. I'll also need to add criteria to select the closest CCTV site in case there are two with 500 m, but thought I'd focus on the first part. 


Any thoughts on my approach or my calculated column?


CCTV Sites =
12742000 * ASIN((SQRT(0.5 - COS((CCTV(Latitude)) * DIVIDE( PI(), 180 ))/2 + COS(CCTV(Latitude) * DIVIDE( PI(), 180 )) * COS(Crime[Latitude] * DIVIDE( PI(), 180 )) * (1-COS((Crime[Longitude]- CCTV(Longitude) * DIVIDE( PI(), 180 )))/2)))  < 500), CCTV (ID),"")




CCTV Installaton  
Camera IDLongLatInstallation Year



Crime Stats    
IDLongLatCrime TypeYearCalculated Column








Super User
Super User

see message 6

Ahh, yes I did miss message 6. With large data sets, I'm getting some unusual banding when I specify distance of 200 m (see image below). Also, the haversine formula that you used gives quite different distance results. At least a factor of 2 different. I thought they would be similar. I checked the original formula by measuring a distance of Google Maps, and it gave me a matching result within 1 decimal place.  




I refactored the formula to its more common format - see if that is better. I also included a simpler version that gives the same result.



DC2 = 
var a = summarize(CCTV,CCTV[Camera ID],CCTV[Lat],CCTV[Long],"Dist",12742000 * asin(sqrt(power(sin(RADIANS(CCTV[Lat] - Crimes[Lat]) / 2), 2) +  power(sin(RADIANS(CCTV[Long] - Crimes[Long]) / 2), 2) *  cos(RADIANS(CCTV[Lat])) * cos(RADIANS(Crimes[Lat])))))
var b = TOPN(1,a,[Dist],ASC)
return CONCATENATEX(b,[Dist])

DC3 = 
var a = summarize(CCTV,CCTV[Camera ID],CCTV[Lat],CCTV[Long],"Dist",6371000 * acos(sin(RADIANS(CCTV[Lat]))*sin(RADIANS(Crimes[Lat]))+cos(RADIANS(CCTV[Lat]))*cos(RADIANS(Crimes[Lat]))*cos(RADIANS(CCTV[Long]-Crimes[Long]))))
var b = TOPN(1,a,[Dist],ASC)
return CONCATENATEX(b,[Dist])



Your banding is likely caused by picking a column from the wrong table.

Sorry for the delay responding. I've been testing the above. I was getting some spurious results, so I stuck with the original haversine formula. That combined with your "brut force it" method and a distance slicer worked. So, thanks very much for your help.


Ideally I would like to simplify the code so I only have one calculted column that returns the CCTV ID when less than 500 m away. So if anyone has any suggestions to refine the code it would be greatfully received. Otherwise I'll keep trying to figure out some options.

 that returns the CCTV ID when less than 500 m away. 

You can only decide that after you have computed the distance.So there is no advantage in throwing away the result. Imagine you then want to say "less than 1000m" etc.  Keep the actual values.


One thing you can consider is to ditch the Haversine and do the simple flat earth computation based on the average meters per degree for your latitude and longitude area.  Much easier to compute.

Super User
Super User

1. what are the errors?

2. wouldn't you want this as a measure, rather than a calculated column?


Hi Ibendin,

It's not recognising the references for longitude / latitude for each table. Even for the Crime[Latitude] and Crime[Longitude] is seems to be wanting a measure. I put a screen shot below. I think it's something to do with the fact that the tables are unrelated and I need to do a row by row operation. 


I would have thought a calculated column would be better for a row by row operation. Also, it can process the information during the refresh which means it should respond quicker (my basic understanding). But obviously my DAX isn't right. 


Basically I just want to add a CCTV site in the calculated column if it's within a certain radius of the crime location. In my head it makes sense, but I'm not sure how to get it to work with unrelated tables.  




You can combine both tables. Add a Location Type identifier to distinguish between cameras and crimes.


Combined = UNION(SELECTCOLUMNS(CCTV,"Lat",[Lat],"Lon",[Long],"Location Type","Camera","Crime Type",BLANK(),"ID",[Camera ID]),SELECTCOLUMNS(Crimes,"Lat",[Lat],"Lon",[Long],"Location Type","Crime","Crime Type",[Crime Type],"ID",[ID]))
I also see a very interesting error message - Your CCTV sample table has the wrong column headers.


 see attached

Hi Ibendin,

thanks for the example. I'm less worried about displaying the CCTV location and more interested in selecting the crimes within a certain radius based on the haversine calculation. Eventually I want to campare crime rates within a radius of CCTV locations vs crime rates beyond that radius to see if there is a difference. I think to do that, I'll need to have a calculated column in the crime table that adds a CCTV site if its within a radius. Any thoughts. 


Oh, and thanks re the headers.


Cheers Andrew

I would brute force it , calculate the distance from a crime location to all camera locations and then pick the closest.  Storing that distance will probably be useful too.


see attached for a possible implementation.  Note that DAX has a RADIANS function.

Note that the Haversine distance is rather meaningless in an urban environment.

Lol @ "brute force it". I'm open to anything. although for rural locations the nearest CCTV location may be miles away. But I could then filter them out. So how would "brute force it" look? Would it be a calculated column added to the Crime table?

Helpful resources

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.


Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.


Fabric certifications survey

Certification feedback opportunity for the community.