cancel
Showing results for 
Search instead for 
Did you mean: 
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
August 1 episode 9_no_dates 768x460.jpg

The Power BI Community Show

Watch the playback when Priya Sathy and Charles Webb discuss Datamarts! Kelly also shares Power BI Community updates.

Power BI Dev Camp Session 24 without aka link and time 768x460.jpg

Ted's Dev Camp - July 28, 2022

Watch Session 24 of Ted's Dev Camp along with past sessions!

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Top Solution Authors
Top Kudoed Authors