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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
MattJessop
Frequent Visitor

Combine two tables

Hi all,

 

I want to calculate the distance between two tables, so that we can analyse our hub locations and if we have positioned them to provide the shortest delivery times. Both tables contain postcode, latitude and longitude. I already know how to calculate the distance between two distances within the same table, however I am struggling to structure the data as I want it. I want to create a third table, that contains all hubs, and all deliveries

 

Ideally, this should be done as a calculated table, rather than a measure, as I'm expecting it to be quite intensive to calculate when we run large sets of data through it, and would rather handle this offline to make adjusting the data online a lot easier.

 

The two data tables we have are structured as follows:

 

Table 1PostcodeLatitudeLongitude
Hub 1AB1 2DE56.00301-3.59004
Hub 2FG3 4HI55.23307-2.96854
Hub 3JK5 6LM57.10344-7.41123

 

Table 2PostcodeLatitudeLongitude
Delivery 1NE1 0EG56.00301-3.59004
Delivery 2BT47 1AG55.23307-2.96854
Delivery 3TS12 1DT57.10344-7.41123

 

This is ideally how the data should come out.

 

Calculated Table 3Hub 1Hub 2Hub 3
Delivery 156.30493134.3403339.43034
Delivery 2102.3443429.0433485.32343
Delivery 392.403443.40304122.30232

 

I know that I could create a new table based off Table 2 and create calculated columns for each hub, however as we have 50+ of these this would be quite tedious and would need to be manually updated everytime we changed the dataset.

 

1 ACCEPTED SOLUTION

You could do something like this:

 

Initial Tables:

 

Table1:

 

Capture.PNG

 

 Table2:

 

Capture.PNG

 

 

Calculate Table DAX:

 

Table =
SELECTCOLUMNS (
    ADDCOLUMNS (
        GENERATE (
            SELECTCOLUMNS (
                Table1,
                "Table1", Table1[Table 1],
                "Latitude From", Table1[Latitude],
                "Longitude From", Table1[Longitude]
            ),
            SELECTCOLUMNS (
                Table2,
                "Table2", Table2[Table 2],
                "Latitude To", Table2[Latitude],
                "Longitude To", Table2[Longitude]
            )
        ),
        "Distance",
        VAR Pie =
            DIVIDE ( PI ()180 )
        VAR Arc =
            0.5
                - COS ( ( [Latitude To] - [Latitude From] ) * Pie )
                    / 2
                + COS ( [Latitude From] * Pie )
                    * COS ( [Latitude To] * Pie )
                    * (
                        1
                            - COS ( ( [Longitude To] - [Longitude From] ) * Pie )
                    )
                    / 2
        VAR KMDistance =
            12742 * ASIN ( SQRT ( Arc ) )
        RETURN
            KMDistance
    ),
    "HUB", [Table1],
    "Delivery", [Table2],
    "Distance KM", [Distance]
)

 

and at the end have: 

 

 

Capture.PNG

 

 Which will make it easier to filter your model by closet distance from each HUB

 


 


Did I answer your question correctly? Mark my answer as a solution!


Proud to be a Datanaut!  

View solution in original post

4 REPLIES 4
LivioLanzo
Solution Sage
Solution Sage

Hi @MattJessop

 

how do you come up with the values in the Table 3?  Is it the distance in Kilometers / Miles ?  because I do not tie if on my side if these are KM  or miles

 


 


Did I answer your question correctly? Mark my answer as a solution!


Proud to be a Datanaut!  

Hi Livio, the figures output would be KM, although I'd advise that all of the above is sample/junk data just to illustrate it. I already have got distance calculations working for them, I'm just struggling to get the tables to merge in the way I'd like.

You could do something like this:

 

Initial Tables:

 

Table1:

 

Capture.PNG

 

 Table2:

 

Capture.PNG

 

 

Calculate Table DAX:

 

Table =
SELECTCOLUMNS (
    ADDCOLUMNS (
        GENERATE (
            SELECTCOLUMNS (
                Table1,
                "Table1", Table1[Table 1],
                "Latitude From", Table1[Latitude],
                "Longitude From", Table1[Longitude]
            ),
            SELECTCOLUMNS (
                Table2,
                "Table2", Table2[Table 2],
                "Latitude To", Table2[Latitude],
                "Longitude To", Table2[Longitude]
            )
        ),
        "Distance",
        VAR Pie =
            DIVIDE ( PI ()180 )
        VAR Arc =
            0.5
                - COS ( ( [Latitude To] - [Latitude From] ) * Pie )
                    / 2
                + COS ( [Latitude From] * Pie )
                    * COS ( [Latitude To] * Pie )
                    * (
                        1
                            - COS ( ( [Longitude To] - [Longitude From] ) * Pie )
                    )
                    / 2
        VAR KMDistance =
            12742 * ASIN ( SQRT ( Arc ) )
        RETURN
            KMDistance
    ),
    "HUB", [Table1],
    "Delivery", [Table2],
    "Distance KM", [Distance]
)

 

and at the end have: 

 

 

Capture.PNG

 

 Which will make it easier to filter your model by closet distance from each HUB

 


 


Did I answer your question correctly? Mark my answer as a solution!


Proud to be a Datanaut!  

Thank you, that's a fantastic solution.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors