Forum Discussion

MattJessop's avatar
MattJessop
Frequent Visitor
7 years ago
Solved

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.

 

  • LivioLanzo's avatar
    LivioLanzo
    7 years ago

    You could do something like this:

     

    Initial Tables:

     

    Table1:

     

     

     Table2:

     

     

     

    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: 

     

     

     

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

4 Replies

  • 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

    • MattJessop's avatar
      MattJessop
      Frequent Visitor

      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.

      • LivioLanzo's avatar
        LivioLanzo
        Solution Sage

        You could do something like this:

         

        Initial Tables:

         

        Table1:

         

         

         Table2:

         

         

         

        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: 

         

         

         

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