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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
anwernuman
New Member

Expression.SyntaxError: Token Eof expected.

Getting Expression.SyntaxError: Token Eof expected. error in the below code. I have checked everything and it seems correct to me. What could be the issue here?

 

= let
    GoogleDistance = (origin as text, destination as text) =>
    let
        apiKey = key,
        url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" & Text.Replace(origin, " ", "+") & "&destinations=" & Text.Replace(destination, " ", "+") & "&key=" & apiKey,
        attemptedSource = try Json.Document(Web.Contents(url)) otherwise null,
        isValid = attemptedSource <> null and "rows" in attemptedSource,
        Rows = if isValid then attemptedSource[rows] else null,
        Elements = if Rows <> null and List.Count(Rows) > 0 and Record.HasFields(Rows{0}, "elements") then Rows{0}[elements] else null,
        Distance = if Elements <> null and List.Count(Elements) > 0 and Record.HasFields(Elements{0}, "distance") then Elements{0}[distance][text] else "Unavailable",
        Duration = if Elements <> null and List.Count(Elements) > 0 and Record.HasFields(Elements{0}, "duration") then Elements{0}[duration][text] else "Unavailable"
    in
        [Distance = Distance, Duration = Duration]
in
    GoogleDistance

Expression.SyntaxError: Token Eof expected. 

1 ACCEPTED SOLUTION
danextian
Super User
Super User

This line of code is causing a problem. M doesnt have a direct  equivalent of IN operator in DAX.

 

"rows" in attemptedSource

 

If you're checking whether attemptedSource has a field called rows, use

 

Record.HasFields(attemptedSource, "rows"),

 

 

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

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


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

5 REPLIES 5
danextian
Super User
Super User

This line of code is causing a problem. M doesnt have a direct  equivalent of IN operator in DAX.

 

"rows" in attemptedSource

 

If you're checking whether attemptedSource has a field called rows, use

 

Record.HasFields(attemptedSource, "rows"),

 

 

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

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


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

You are amazing. This worked

anwernuman
New Member

Key is defined correctly but I am still getting the error

anwernuman_0-1738939900546.png

 

DataNinja777
Super User
Super User

Hi @anwernuman ,

 

The Expression.SyntaxError: Token Eof expected. error in Power Query typically suggests a missing or misplaced character, such as a missing closing parenthesis, missing in keyword, or an improperly formatted variable reference. One of the potential issues in your code is that the variable key is undefined. In the line:

apiKey = key,

if key is not assigned anywhere, Power Query will throw an error. Ensure key is properly defined, or replace it with an actual API key string.

Another issue is the = before let at the beginning of your code. The let statement should not have an = before it. The correct syntax is to define the function directly without an equals sign:

let

instead of

= let

Additionally, ensure that Web.Contents(url) is returning valid JSON. If it is not properly formatted, Json.Document(Web.Contents(url)) will fail. You can test the API response by using Text.FromBinary(Web.Contents(url)) to inspect the raw response.

Here is a corrected version of your code:

let
    GoogleDistance = (origin as text, destination as text) =>
    let
        apiKey = "your_api_key_here", // Ensure apiKey is defined properly
        url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" & 
               Text.Replace(origin, " ", "+") & "&destinations=" & 
               Text.Replace(destination, " ", "+") & "&key=" & apiKey,
        attemptedSource = try Json.Document(Web.Contents(url)) otherwise null,
        isValid = attemptedSource <> null and "rows" in attemptedSource,
        Rows = if isValid then attemptedSource[rows] else null,
        Elements = if Rows <> null and List.Count(Rows) > 0 and Record.HasFields(Rows{0}, "elements") then Rows{0}[elements] else null,
        Distance = if Elements <> null and List.Count(Elements) > 0 and Record.HasFields(Elements{0}, "distance") then Elements{0}[distance][text] else "Unavailable",
        Duration = if Elements <> null and List.Count(Elements) > 0 and Record.HasFields(Elements{0}, "duration") then Elements{0}[duration][text] else "Unavailable"
    in
        [Distance = Distance, Duration = Duration]
in
    GoogleDistance

This version removes the unnecessary =, ensures apiKey is assigned correctly, and keeps the syntax properly structured. Try this and check if the issue is resolved.

 

Best regards,

anwernuman_1-1738939939268.png

 

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.