cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Helper II
Helper II

Need Measure to count nearby points on a Scatter

Quite simply I want a measure that counts the number of nearby points (relative to a given point) on a Scatter Plot visual. It could use an arbitrary radius (like 5 units). I would use this to assign color or size to points that are close together. 

 

Points in the red circle below have many nearby neighbors, and so the measure should return a higher number (so I can associate it with a "hot" color).  

bvy_1-1598645905851.png

 

bvy_2-1598645933479.png

 

Is this doable? 

 

 

 

17 REPLIES 17
Highlighted

@bvy - Seems like to implement this in the data model you would have to start with a GENERATE of the original table with itself I guess, which is the N^2. Then you would just calculate the distance for each row. But, if you are going to go down that route, you probably might look at doing it in Power Query with a Merge query and a custom column in Power Query. So, at the end of the day your data load would be slower, your data model bigger and performance in your visual should be improved. The degree to which each of these changes and whether one is better than the other is dependent. Obviously, if you are Pro and this technique blows your data model up beyond the 1GB limit, then that's bad. So, it's really going to come down to your specific set of circumstances.

 

Basically, you are trading data load/refresh speeds and data model size for visual performance/easier calculations.


---------------------------------------

@ me in replies or I'll lose your thread!!!

I have a NEW book! 
DAX Cookbook from Packt
Over 120 DAX Recipes!




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Highlighted

If you order the points somehow (does not matter how), then you'd have to check N*(N-1)/2 distances, which still is a lot. Not sure how SQL would cope with this (depends on how powerful the server is) but maybe you could do it in Python instead? I'll have to check myself if that's feasible.
Highlighted

@bvy - You know, I just had another thought on how to greatly speed up the original method of doing the distances dynamically, you could do this:

 

Measure =
  VAR __radius = 5
  VAR __x1 = MAX('Data'[X])
  VAR __y1 = MAX('Data'[Y])
  VAR __id = MAX('Data'[ID])
  VAR __Table = ADDCOLUMNS(FILTER(ALL('Data'),[X]<=__x1+__radius && [X]>=__x1-__radious && [Y]<=__y1+__radius && [Y]>=__y1-__radius),"Distance",SQRT( (__x1 - [X])^2 + (__y1 - [Y])^2) )
RETURN
  COUNTROWS(FILTER(__Table,[Distance]<=__radius))

 

So basically any point that is going to be within the specified distance by definition has to fall within the square defined by the radius so I *think* that should be a performance gain because you are not doing the distance calculation on every single point every single time. And here is another thought, you could technically get rid of the distance calculation all together and just do:

 

Measure =
VAR __radius = 5
VAR __x1 = MAX('Data'[X])
VAR __y1 = MAX('Data'[Y])
VAR __id = MAX('Data'[ID])
VAR __Table = FILTER(ALL('Data'),[X]<=__x1+__radius && [X]>=__x1-__radius && [Y]<=__y1+__radius && [Y]>=__y1-__radius)
RETURN
COUNTROWS(__Table)

 

Sure, it's not as "scientificky" as using the actual distance between points but it would be fairly accurate, probably within most margin of errors!

 


---------------------------------------

@ me in replies or I'll lose your thread!!!

I have a NEW book! 
DAX Cookbook from Packt
Over 120 DAX Recipes!




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Highlighted

@bvy - OK, I gave this a legitmate shot trying to create a Power Query based upon the same logic as my last reply with essentially "drawing" a box around the point and finding the other points in that box. I am quite certain that this is probably not the most elegant or efficient way of doing it because my Power Query fu is relatively weak compared to someone like @ImkeF or @edhans , who maybe they might want to jump into this thread and help optimize this. Anyway, I attached the PBIX below sig. Tables are Table (4), Table (5) and Query2. 

 

Table (4)
let
    #"Table (4)" = let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("PY7BEQAhCAN74e1D9PDOWhj6b+NMUB/ZSYIOuItKEaOiuLTlKoXUl5sU0rOc1rqJxugP0Qy8tgTyy1kC+YO1BPK888zH3w+K+xq3tr1VcWRnBUb8", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, X = _t, Y = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"X", Int64.Type}, {"Y", Int64.Type}})
in
    #"Changed Type"
in
    #"Table (4)"


Table (5)
let
    Source = #"Table (4)",
    #"Added Custom"=Table.AddColumn(Source,"Matches",each Query2([X],[Y],5)),
    #"Expanded Matches" = Table.ExpandTableColumn(#"Added Custom", "Matches", {"ID", "X", "Y"}, {"Matches.ID", "Matches.X", "Matches.Y"})
in
    #"Expanded Matches"


Query2 (it's a function)
(x as number, y as number, r as number) as table =>
let
    Source = #"Table (4)",
    Table = Table.SelectRows(Source,each [X]<=x+r and [Y]<=y+r and [X]>=x-r and [Y]>=y-r)
in
    Table

 

So, the total number of rows is dependent on how many matches are found. The obvious downside to this approach is that you won't be able to make the radius for matches dynamic based upon user input within the report. Data refresh will be slower, your model will be larger, your visuals should be more performant over a pure DAX solution. 

 

To bring @ImkeF and @edhans up to speed quickly. Lots of points in a scatter chart. Want to control the size of those points based upon how many "close neighbors" there are. I created a pure DAX solution that dynamically finds close points using standard distance formula then made improvements, @daxer-almighty suggested a Power Query approach, this message here tries to combine the two approaches into a single solution. 🙂

 

The attached file, Page 4 compares the various solutions. There is a difference even in the same dataset between the distance and box approaches.


---------------------------------------

@ me in replies or I'll lose your thread!!!

I have a NEW book! 
DAX Cookbook from Packt
Over 120 DAX Recipes!




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Highlighted

@Greg_Deckler Good stuff guys. Will pick this up in the morning when I'm legitimately "at work" again. Thanks for the brainpower. I like where this is going... 

Highlighted

Hi @Greg_Deckler ,

the only thing I can think of is to optimize the crossjoin in Table (5) with the approach that Chris Webb has posted lately: 

   #"Added Custom"=Table.AddColumn(Source,"Matches",each Query2([X],[Y],5)),

https://blog.crossjoin.co.uk/2020/08/30/optimising-the-performance-of-power-query-merges-in-power-bi... 

 

Don't think that the Table.SelectRows has any potential for optimization.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Highlighted

Hmm, read the Chris Webb article. Was actually trying to avoid a full cross join, but maybe it is faster to do the full cross join, do the calculation and filter down versus doing the filtering as part of the join essentially.


---------------------------------------

@ me in replies or I'll lose your thread!!!

I have a NEW book! 
DAX Cookbook from Packt
Over 120 DAX Recipes!




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Highlighted

Oh, I might got this one wrong @Greg_Deckler .

But it could be worth a try..

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Helpful resources

Announcements
Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

Community Conference

Microsoft Power Platform Communities

Check out the Winners!

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Top Kudoed Authors