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
Anonymous
Not applicable

distance calculation : optimized route mapquest api

Hi !
I'm using the MapQuest API that calculates the optimized route given a starting location and several served locations.
For each trip, given the different locations, I want to have the distance of the trip.

 

I saw how to calculate a distance between TWO locations with a chosen API, it works as follows :

(in power query)
1) create a function with the API

2) add a column in a table where there is a column for origins and a column for destinations, these columns are used as parameters of your function

3) expand the added column
⇒ it gives you several new columns with the distance, the duration, ... and you can select information you want
(https://www.youtube.com/watch?v=ZVE3POxiRKs)

I need to do something like that but my problem is that instead of having 2 location parameters (origin and destination), I have several location parameters (start location and several served locations) and the number of locations changes according to the number of locations visited during the trip.
I don't see how I can put the locations in different columns since there is a different number of location for each trip, and originally they are stored in different rows.

TRIP_IDSTART_LOCATIONSERVED_LOCATION
1DenverWestMinster
1DenverLakewood
1DenverBoulder
2New York CityBoston
2New York CityPittsburgh
3Santa FeSalt Lake City
3Santa FeSeattle
3Santa FeSacramento

 

Here is an example of the API code to obtain information about trip 1:

https://www.mapquestapi.com/directions/v2/optimizedRoute?json={"locations":["Denver, CO","WestMinster, CO","Lakewood, CO","Boulder, CO"]}&outFormat=json&key=KEY
⇒ I can add as many locations as I want


I don't know how to adapt the process to my case, would you have any idea ? I'm really stuck here.

 

Thank you very much for your help !

 

1 ACCEPTED SOLUTION

Here is some additional instruction

1. Starting with your existing query, group it on the Trip_ID and Start_Location columns, using the Group By button in the ribbon.  Call the column "Locations".  Choose All Rows as the aggregation.

2.  Add a custom column called "ListOfLocations" with this formula -    = List.Combine({{[START_LOCATION]}, [Locations][SERVED_LOCATION]}) 

3.  Add a custom column called "ListInQuotes" with this formula -    = Text.Combine(List.Transform([ListOfLocations], each """"&_&""""), ", ")

4. Concatenate that new column with the rest of your web call to get your results on each row/for each Trip_ID

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

12 REPLIES 12

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.

Top Solution Authors
Top Kudoed Authors