Geography.FromWellKnownText

D

T

The M Code Behind the Power Query M function Geography.FromWellKnownText

In this article, we will dive into the M code behind the Geography.FromWellKnownText function and understand how it works.

Understanding Well-Known Text (WKT)

Before we dive into the M code behind the Geography.FromWellKnownText function, let’s first understand what Well-Known Text (WKT) is.

WKT is a text representation of a geometric object that is commonly used in GIS (Geographic Information Systems) applications. It describes the shape, size, and location of the object using a set of coordinates.

Here’s an example of a WKT representation of a point:


POINT (-122.34900 47.65100)


This represents a point with longitude -122.34900 and latitude 47.65100.

The M Code Behind Geography.FromWellKnownText

Now that we understand what WKT is, let's take a look at the M code behind the Geography.FromWellKnownText function.

The function takes a single parameter, which is a text value that represents a WKT representation of a geometric object. Here's the syntax of the function:


Geography.FromWellKnownText(wktText as text) as nullable geography


The function returns a nullable geography value, which represents the geography object that corresponds to the WKT representation.

Here's an example usage of the function:


let

wktText = “POINT (-122.34900 47.65100)”,

geographyValue = Geography.FromWellKnownText(wktText)

in

geographyValue


This will return a geography value that represents the point with longitude -122.34900 and latitude 47.65100.

Breaking Down the M Code

Now let's break down the M code behind the Geography.FromWellKnownText function.

The function is defined using the let keyword, which is used to define variables in M. The wktText variable is defined as a text value that represents the WKT representation of the geometric object.


let

wktText = “POINT (-122.34900 47.65100)”,


The next line calls the Geography.FromWellKnownText function with the wktText variable as the parameter. This returns a nullable geography value, which is stored in the geographyValue variable.


geographyValue = Geography.FromWellKnownText(wktText)


The final line returns the geographyValue variable, which is the result of the function.


in

geographyValue


Handling Invalid WKT

One thing to keep in mind when using the Geography.FromWellKnownText function is that it may return null if the WKT representation is invalid.

For example, if we pass in an invalid WKT representation like this:


let

wktText = “INVALID WKT”,

geographyValue = Geography.FromWellKnownText(wktText)

in

geographyValue


The function will return null.

To handle this scenario, we can use the try keyword in M, which will return a default value if the expression throws an error. Here's an example:


let

wktText = “INVALID WKT”,

geographyValue = try Geography.FromWellKnownText(wktText) otherwise null

in

geographyValue


This will return null if the function throws an error.

In this article, we looked at the M code behind the Power Query M function Geography.FromWellKnownText. We learned how the function works and how to use it to convert a WKT representation of a geometric object into a geography value.

We also learned how to handle invalid WKT representations using the try keyword.

Overall, the Geography.FromWellKnownText function is a powerful tool for working with geographic data in Power Query, and understanding its M code can help you to use it more effectively in your data transformations.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)