Forum Discussion

Syndicate_Admin's avatar
Syndicate_Admin
Icon for Administrator rankAdministrator
1 year ago

Expression.SyntaxError: Token Eof esperado.

Obtención de Expression.SyntaxError: Token Eof esperado. error en el siguiente código. Lo he comprobado todo y me parece correcto. ¿Cuál podría ser el problema aquí?

= 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 esperado.

5 Replies

  • Hola @anwernuman ,

    Se espera que Expression.SyntaxError: Token Eof. El error en Power Query suele sugerir un carácter que falta o está mal colocado, como un paréntesis de cierre que falta, una palabra clave que falta o una referencia de variable con formato incorrecto. Uno de los posibles problemas del código es que la clave de variable no está definida. En la línea:

    apiKey = key,
    

    si la clave no está asignada en ninguna parte, Power Query producirá un error. Asegúrese de que la clave esté definida correctamente o reemplácela por una cadena de clave de API real.

    Otro problema es el = antes de let al principio del código. La instrucción let no debe tener un = antes. La sintaxis correcta es definir la función directamente sin un signo igual:

    let
    

    En lugar de

    = let
    

    Además, asegúrese de que Web.Contents(url) devuelva un JSON válido. Si no tiene el formato correcto, se producirá un error en Json.Document(Web.Contents(url). Puedes probar la respuesta de la API mediante Text.FromBinary(Web.Contents(url)) para inspeccionar la respuesta sin procesar.

    Esta es una versión corregida de su código:

    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
    

    Esta versión elimina el = innecesario, garantiza que apiKey se asigne correctamente y mantiene la sintaxis correctamente estructurada. Pruebe esto y verifique si el problema se resuelve.

    Saludos

  • Esta línea de código está causando un problema. M no tiene un equivalente directo del operador IN en DAX.

    "rows" in attemptedSource

    Si está comprobando si attemptedSource tiene un campo llamado filas, use

    Record.HasFields(attemptedSource, "rows"),