Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateJoin us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.
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.
Solved! Go to Solution.
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"),
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"),
You are amazing. This worked
Key is defined correctly but I am still getting the error
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,
Check out the July 2025 Power BI update to learn about new features.
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
User | Count |
---|---|
66 | |
65 | |
57 | |
39 | |
28 |
User | Count |
---|---|
84 | |
60 | |
45 | |
41 | |
39 |