Tools Advisor | Power BI

Determining Nearest Store for Each

This solution in Power BI helps find the nearest store for each customer based on their addresses. It involves creating a relationship between the customer and store tables, calculating the distance between each customer's address and all store


Empty image or helper icon

This Query related with thread "Geographical Distance Calculation in DAX using Haversine"

Prompt

So, what I want to accomplish is to be able to tell what store a customer is near based on the customer’s address. There would be a table of customers with their addresses and then a table of stores with their addresses. I want to be able to find which store is the shortest distance to the customer’s address.

Answer

Problem Analysis:

The user wants to determine the nearest store for each customer based on their addresses. They have a table of customers with their addresses and a table of stores with their addresses. The goal is to find the store that is the shortest distance from each customer's address.

Strategic Solution Development:

To solve this problem in Power BI, we can follow these steps:

  1. Create a relationship between the customer table and the store table based on the address fields.
  2. Calculate the distance between each customer's address and all store addresses using the Haversine formula or the Bing Maps API.
  3. Find the store with the shortest distance for each customer.
  4. Display the nearest store information for each customer in a visual or table.

Comprehensive Advice:

Here is a step-by-step guide on how to accomplish this task:

  1. Create a relationship:

    • Ensure that both the customer table and the store table have a unique address field that can be used to match the records.
    • In the "Relationships" view, create a relationship between the address field in the customer table and the address field in the store table.
  2. Calculate the distance:

    • Option 1: Haversine formula (Power Query):

      • In Power Query, combine the customer table and store table into a single table using the Append Queries feature.
      • Create a new calculated column using the Haversine formula to calculate the distance between each customer-store pair.
      • Use the "Group By" feature to find the minimum distance for each customer, grouping by the customer's unique identifier.
      • Merge the result back with the customer table to get the nearest store information.
    • Option 2: Bing Maps API (Power Query):

      • Register for a Bing Maps API key.
      • Use the "Web" connector in Power Query to call the Bing Maps API and pass the customer address and store addresses as parameters.
      • Parse the API response to extract the distance information.
      • Merge the result back with the customer table to get the nearest store information.
  3. Display the nearest store information:

    • Create a visual or table to display the customer information along with the nearest store details.
    • Include columns such as customer name, address, and the name and address of the nearest store.

Code-Based Solution (using Haversine formula):

let
    customers = Excel.CurrentWorkbook(){[Name="Customers"]}[Content],
    stores = Excel.CurrentWorkbook(){[Name="Stores"]}[Content],

    appended = customers & stores,

    distances = Table.AddColumn(appended, "Distance", (row) => 
        let
            lat1 = Number.From(row[Lat]),
            lon1 = Number.From(row[Lon]),
            lat2 = Number.From(row[Lat]),
            lon2 = Number.From(row[Lon]),
            earthRadius = 6371, // radius of the Earth in kilometers
            dLat = Radians(lat2 - lat1),
            dLon = Radians(lon2 - lon1),
            a = Sin(dLat / 2) * Sin(dLat / 2) + Cos(Radians(lat1)) * Cos(Radians(lat2)) * Sin(dLon / 2) * Sin(dLon / 2),
            c = 2 * Atan2(Sqrt(a), Sqrt(1 - a)),
            distance = earthRadius * c
        in
            distance, type number),

    minDistances = Table.Group(distances, "CustomerID", {{"MinDistance", each List.Min([Distance]), type number}}),

    nearestStoreInfo = Table.Join(customers, "CustomerID", minDistances, "CustomerID"),
    
    result = Table.Join(nearestStoreInfo, {"CustomerID", "MinDistance"}, appended, {"CustomerID", "Distance"}),
    
    finalResult = Table.SelectColumns(result, {"CustomerID", "CustomerName", "CustomerAddress", "StoreName", "StoreAddress"})
in
    finalResult

Detailed Explanation:

  • The code performs the following steps:
    • Retrieves the data from the "Customers" and "Stores" tables.
    • Appends the two tables together.
    • Calculates the distance between each customer-store pair using the Haversine formula.
    • Groups the data by the customer's unique identifier and finds the minimum distance for each customer.
    • Joins the result back with the customer table based on the customer's unique identifier.
    • Joins the result back with the appended table to get the nearest store information.
    • Selects the desired columns for the final output.
  • You can replace the column names (Lat, Lon, CustomerID, etc.) and table names (Customers, Stores) with your actual column and table names.

Note: The Haversine formula is a basic method for calculating distances based on latitude and longitude coordinates. If you need more accurate distance calculations, consider using the Bing Maps API or other specialized libraries.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This solution in Power BI helps find the nearest store for each customer based on their addresses. It involves creating a relationship between the customer and store tables, calculating the distance between each customer's address and all store addresses, finding the store with the shortest distance for each customer, and displaying the nearest store information in a visual or table. The solution provides both a code-based solution using the Haversine formula and a detailed explanation for implementation.