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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Euclidean Distance Matrix using R in Power BI

I have a dataset of ~6000 rows (individuals) and ~40 columns (attributes). I want to scale these attributes and use the dist R function to create a pairwise similarity/distance matrix comparing every single row against one another. I have a functioning R code to produce this in R Studio:

 

*Read in data and set working directory etc.... (i've not included some transformations I did in R because I replicated these in the applied steps in the Query Editor)

data_scaled<-scale(dataloaded)

max<-nrow(dataloaded) # for testing - number of people to process (i.e. first n rows)

# compute the distance matrix using dist()
dataEuc_dist<-as.matrix(dist(data_scaled[1:max,], method="euclidean"))
View(dataEuc_dist)

 

(Tried with and without the last line: "View(...)"

 

When I use the above in the Run R Script a table is produced with only two columns ("Name", "Value") and a "This table is empty" notification. Is there a separate line needed to force the creation of this dataset to output as a new table? Is it possible to do this in the R Script creator, because an entirely new dataframe is being created rather than transforming the already existing table?

 

EDIT: I also know that I can do these actions within R and transfer the table, however, i'd like to make the process automated if possible for future data refreshes.

 

Not entirely sure how this function works in power BI so any input would be great!
Thanks!
Gareth

1 REPLY 1
TomMartens
Super User
Super User

Hey,

 

you should omit the

View(...)

line in your r script.

 

Instead the last line has to return a dataframe.

 

Maybe this will help

https://stackoverflow.com/questions/50025814/convert-matrix-into-dataframe-in-r

 

Regards,

Tom

 

 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.