2023: Week 11 - Solution
Solution by Tom Prowse and you can download the workflow here.
This week we are looking at calculating the distance for our closest customers by utilising the Haversine formula as our spatial calculations. We are looking at latitude and longitude and then calculating the distance with some trigonometry.
Step 1 - Combine Data
After this our number of rows will massively increase as we now have a row for each combination:
Step 2 - Transform Lat & Long
The next step is to transform each of the lat & long into radians. To make this easier we want to pivot the data so we have all the customer and branch lat longs in a single column. For this we can use a columns to rows pivot with each of the lat long values in the pivot values:
Then we can calculate the values as radians:
Then make sure the labels are updated to show we are calculating radians instead:
At this stage our table should look like this:
We can then pivot the table back to the original structure that we had before by using a rows to columns pivot where we are grouping by Radians, and Sum Transformed values:
At this stage our table should look like this:
We no longer require the lat and long fields so can remove those and our table should look like this:
Step 3 - Customer Priority Rating
Finally we want to assign a priority rating to each of our customers based on how close they are to a branch location.
First we want to rank the distances of the branches from each customer
Then we can use this field to filter to the closest branch for each customer (Keep Only = 1). This then provides us with a list of customers and their closest branch.
We then want to bring back the detail about the locations, so can join the table from before the first pivot step. By joining on Customer and Branch we can then return all of the details about each of the customer locations:
From here we can again rank the customers based on their distance and give them a priority score
This the final calculation that we need and our table is now ready to output: