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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
LeopoldoG
Regular Visitor

How to Find Nearest Elements to a Selected Item Using X and Y Coordinates in Power BI?

Hello everyone,

I'm working on a Power BI report with data that includes X and Y coordinates for each item. I'd like to implement a feature that allows users to select an item and see a list or table displaying the closest items to it. I was able to calculate the distance of each item to a fixed coordinate in a column, but I couldn’t make that reference coordinate change based on the item selected in the slicer.

Current Setup:

  • A dataset with X and Y coordinates columns for each item.
  • A data slicer that allows the selection of a specific item.

Objective:

  • Calculate the distance between the selected item and other items.
  • Sort these items based on their proximity to the selected item.
  • Display the nearest items in a table or other visualization in Power BI.

Questions:

  1. Is it possible to achieve this directly in Power BI using DAX or any other built-in functionality?
  2. Is there an alternative approach to display the nearest items based on coordinates, perhaps using a map visual or a custom visual in Power BI?

Thank you in advance for any suggestions or DAX examples you can provide!

1 ACCEPTED SOLUTION

Hi @LeopoldoG ,

 

You can the following expression to get the coordinates in the slicer:

SelectedItemX = 
CALCULATE(
    MAX('Table'[X]),
    'Table'[CT] = SELECTEDVALUE('Table'[CT])
)

SelectedItemY = 
CALCULATE(
    MAX('Table'[Y]),
    'Table'[CT] = SELECTEDVALUE('Table'[CT])
)

 

Hope it helps!

 

Best regards,
Community Support Team_ Scott Chang

 

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
v-tianyich-msft
Community Support
Community Support

Hi @LeopoldoG ,

 

Not sure about your data model, but you can generally try the following expression:

SelectedX = SELECTEDVALUE(YourTable[X])
SelectedY = SELECTEDVALUE(YourTable[Y])

Distance = 
VAR SelectedX = [SelectedX]
VAR SelectedY = [SelectedY]
RETURN
    SQRT(POWER(YourTable[X] - SelectedX, 2) + POWER(YourTable[Y] - SelectedY, 2))

Rank = 
RANKX(
    ALL(YourTable),
    [Distance],
    ,
    ASC
)

 

Hope it helps!

 

Best regards,
Community Support Team_ Scott Chang

 

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

Thank you for your response! I actually tried something similar, but it doesn’t work as expected. It calculates the distance between each item and itself, rather than using the coordinates of the selected item in the slicer.

The data model:

LeopoldoG_2-1730201982706.png

 

 

LeopoldoG_1-1730201251175.png

 

Hi @LeopoldoG ,

 

You can the following expression to get the coordinates in the slicer:

SelectedItemX = 
CALCULATE(
    MAX('Table'[X]),
    'Table'[CT] = SELECTEDVALUE('Table'[CT])
)

SelectedItemY = 
CALCULATE(
    MAX('Table'[Y]),
    'Table'[CT] = SELECTEDVALUE('Table'[CT])
)

 

Hope it helps!

 

Best regards,
Community Support Team_ Scott Chang

 

If this post helps then please consider Accept it as the solution to help the other members find it more quickly.

Greg_Deckler
Super User
Super User

@LeopoldoG This should be helpful. If you can provide sample data, I can probably modify this to work for what you are trying to do: Near - Microsoft Fabric Community



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors