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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
rlmeyer
Resolver I
Resolver I

Power Query - Invoke Custom Function then Commit Data Back to Source File

I have a query setup to invoke a custom function that uses the Bing Maps API call to retrieve the distance and drive time to a list of addresses. My data set currently only has 500 rows so the function gets used 500 times everytime I refresh the data. This takes about 15 minutes for the query to run through the 500 rows.

 

My question, is there anyway to commit the distance and drive time data into the query so that the next time I refresh the data it sees that it already retrieved that data and to ignore it this time around? I really would only want the custom function call to run with newly added rows of data and to ignore all the existing rows.

 

I was thinking worst case, all the data is saved in an Excel file, so after I ran the funtion the frist time, I could manually commit the API data back into the orginal Excel file and setup a second query that only runs if the data is missing. I would prefer not to have to manually commit the data into the Excel file. Maybe the answer is to somehow automate it to update the Excel file? If that seems to make sense to anyone, any advice on how to commit that data back?

1 ACCEPTED SOLUTION
rlmeyer
Resolver I
Resolver I

I used the following video to create an R Visual that exports the Excel file and overwrites the orginal. One query is used for the main dashboard. A second query creates the duplicate Excel file that gets exported out after the API call is finished.

 

Power BI - Write Data to Excel (On Multiple Tabs) - Bing video

library(openxlsx)
dataset2 = dataset[,c("Name","Address","Notes","Website","TravelDistance","TravelDuration","TravelDurationTraffic","Latitude","Longitude")]
tabs = list("Sheet 1" = dataset2)
hs = createStyle(fontSize=11, fontColour="#000000",halign="center",fgFill="#CCCCCC")
if(dataset$Selection[1]=="Write") {
    write.xlsx(tabs, file ="C:/Users/User/Data/Data.xlsx", colWidths = "12", headerStyle = hs)
    openXL(file = "C:/Users/User/Data/Data.xlsx") }
plot(1,1)

 

View solution in original post

1 REPLY 1
rlmeyer
Resolver I
Resolver I

I used the following video to create an R Visual that exports the Excel file and overwrites the orginal. One query is used for the main dashboard. A second query creates the duplicate Excel file that gets exported out after the API call is finished.

 

Power BI - Write Data to Excel (On Multiple Tabs) - Bing video

library(openxlsx)
dataset2 = dataset[,c("Name","Address","Notes","Website","TravelDistance","TravelDuration","TravelDurationTraffic","Latitude","Longitude")]
tabs = list("Sheet 1" = dataset2)
hs = createStyle(fontSize=11, fontColour="#000000",halign="center",fgFill="#CCCCCC")
if(dataset$Selection[1]=="Write") {
    write.xlsx(tabs, file ="C:/Users/User/Data/Data.xlsx", colWidths = "12", headerStyle = hs)
    openXL(file = "C:/Users/User/Data/Data.xlsx") }
plot(1,1)

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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