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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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