Greg_Deckler's avatar
Greg_Deckler
Community Champion
1 year ago

Map Lines

Came out of this conversation: Create Origin To Destination Map In BI Desktop - Microsoft Fabric Community. Here's a way to create origin to destination lines on a map given a table of cities.

CityLatitudeLongitude

Columbus, OH39.9611111-82.9988889
London, UK51.5-0.116667
Kansas City39.099912-94.581213
St. Louis38.627089-90.200203

 

From and To = 
    VAR __NumSteps = 100
    VAR __Steps = SELECTCOLUMNS( GENERATESERIES( 1, __NumSteps, 1 ), "Step", [Value] )
    VAR __BaseTable =
        FILTER( 
            GENERATE( 
                SELECTCOLUMNS( 'Cities', "From City", [City], "From Long", [Longitude], "From Lat", [Latitude] ),
                SELECTCOLUMNS( 'Cities', "To City", [City], "To Long", [Longitude], "To Lat", [Latitude] )
            ),
            [From City] <> [To City]
        )
    VAR __String = CONCATENATEX( __BaseTable, [From City] & "^" & [From Long] & "^" & [From Lat] & "^" & [To City] & "^" & [To Long] & "^" & [To Lat], "|" )
    VAR __Count = COUNTROWS( __BaseTable )
    VAR __Table = 
        ADDCOLUMNS(
            GENERATESERIES( 1, __Count ),
            "__Data", SUBSTITUTE( PATHITEM( __String, [Value] ), "^", "|" )
        )
    VAR __Result = 
        FILTER(
            SELECTCOLUMNS(
                ADDCOLUMNS(
                    GENERATE(
                        __Table,
                        __Steps
                    ),
                    "Latitude", PATHITEM( [__Data], 3 ) +  ( [Step] / __NumSteps ) * ( PATHITEM( [__Data], 6 ) - PATHITEM( [__Data], 3 ) ),
                    "Longitude", PATHITEM( [__Data], 2 ) + ( [Step] / __NumSteps) * ( PATHITEM( [__Data], 5 ) - PATHITEM( [__Data], 2 ) )
                ),
                "Latitude", [Latitude],
                "Longitude", [Longitude],
                "From", PATHITEM( [__Data], 1 ),
                "To", PATHITEM( [__Data], 4 )
            ),
            [From] <> [To]
        )
RETURN
    __Result