Advance your Data & AI career with 50 days of live learning, dataviz contests, hands-on challenges, study groups & certifications and more!
Get registeredGet Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now
Very simple dataset here:
- Data - Each record represents a data point with (X, Y) coordinates; ID is unique to each point.
| ID | Level | X | Y |
| 1 | Level A | 50 | 35 |
- Neightbors - A record exists in this table for each pair of ID's that are in mutually close proximity. For example the following might exist in the table, indicating that points 2 and 5 are arbitrarily close. A second row with 5 then 2 would NOT appear however:
| ID1 | ID2 |
| 2 | 5 |
I want to add a DAX measure to the Data table indicating, for each ID, the the number of nearby points (i.e. the number of records in the Neighbors table). Here is my DAX:
Solved! Go to Solution.
That's good to hear.
In this case, I think you can get away with essentially adding zero to the measure.
In more complicated situations this can cause unwanted zeros to appear, and may require a more complex solution (see here).
Maybe to be on the safe side we could include a condition that SELECTEDVALUE ( Data[ID] ) is not blank to the overall measure as well (which ensures we have filtered on a single ID):
ClusterMeasure =
VAR This_ID =
SELECTEDVALUE ( Data[ID] )
RETURN
IF (
NOT ISBLANK ( This_ID ),
VAR AllSelected_ID =
CALCULATETABLE ( VALUES ( Data[ID] ), ALLSELECTED () )
VAR Neighbors1 =
CALCULATE (
COUNTROWS ( Neighbors ),
Neighbors[ID1] = This_ID,
TREATAS ( AllSelected_ID, Neighbors[ID2] )
)
VAR Neighbors2 =
CALCULATE (
COUNTROWS ( Neighbors ),
Neighbors[ID2] = This_ID,
TREATAS ( AllSelected_ID, Neighbors[ID1] )
)
RETURN
Neighbors1 + Neighbors2 + 0
)
Regards,
Owen
Company policy doesn't allow it. I think there's enough info in the original post to make suggestions though.
Can anyone help?
Hi @bvy
I am assuming that there are no relationships between Data & Neighbors, and that you prefer not to (or cannot) add any.
You could try a measure along these lines. This performs reasonably well in a test model at my end:
ClusterMeasure =
VAR This_ID = SELECTEDVALUE ( Data[ID] )
VAR AllSelected_ID = CALCULATETABLE ( VALUES ( Data[ID] ), ALLSELECTED() )
VAR Neighbors1 =
CALCULATE (
COUNTROWS ( Neighbors ),
Neighbors[ID1] = This_ID,
TREATAS ( AllSelected_ID, Neighbors[ID2] )
)
VAR Neighbors2 =
CALCULATE (
COUNTROWS ( Neighbors ),
Neighbors[ID2] = This_ID,
TREATAS ( AllSelected_ID, Neighbors[ID1] )
)
RETURN
Neighbors1 + Neighbors2
This measure ensures that both ID1 & ID2 are in Allselected_ID.
Neighbors1 has This_ID as ID1 and Neighbors2 = has This_ID as ID2.
Does this give expected results for you, and is performance ok?
Regards,
Owen
Hi @OwenAuger. This actually works pretty well. Can you see a way to adjust it to return 0 for data points that have no nearby neightbors? That's the only thing that's missing. Thanks so much.
That's good to hear.
In this case, I think you can get away with essentially adding zero to the measure.
In more complicated situations this can cause unwanted zeros to appear, and may require a more complex solution (see here).
Maybe to be on the safe side we could include a condition that SELECTEDVALUE ( Data[ID] ) is not blank to the overall measure as well (which ensures we have filtered on a single ID):
ClusterMeasure =
VAR This_ID =
SELECTEDVALUE ( Data[ID] )
RETURN
IF (
NOT ISBLANK ( This_ID ),
VAR AllSelected_ID =
CALCULATETABLE ( VALUES ( Data[ID] ), ALLSELECTED () )
VAR Neighbors1 =
CALCULATE (
COUNTROWS ( Neighbors ),
Neighbors[ID1] = This_ID,
TREATAS ( AllSelected_ID, Neighbors[ID2] )
)
VAR Neighbors2 =
CALCULATE (
COUNTROWS ( Neighbors ),
Neighbors[ID2] = This_ID,
TREATAS ( AllSelected_ID, Neighbors[ID1] )
)
RETURN
Neighbors1 + Neighbors2 + 0
)
Regards,
Owen
Working well. Thanks so much for taking the time.
@bvy
You can save your PBIX file on any cloud space like google drive, One Drive, etc, and share the link here.
⭕ Subscribe and learn Power BI from these videos
⚪ Website ⚪ LinkedIn ⚪ PBI User Group
Check out the November 2025 Power BI update to learn about new features.
Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!
| User | Count |
|---|---|
| 97 | |
| 76 | |
| 52 | |
| 51 | |
| 46 |