Search code examples
powerquerym

Multiple nested if statements error: "Token Eof expected"


I have the following code in PowerQuery and somehow I can't figure it out:

if [DEP_PORT_CODE] = "AKL" then "AKL" else if [DEP_PORT_CODE] = "BCN" then "BCN" else if [DEP_PORT_CODE] = "BOG" then "BOG" else if [DEP_PORT_CODE] = "BOS" then "BOS" else if [DEP_PORT_CODE] = "CDG" then "CDG" else if [DEP_PORT_CODE] = "CUN" then "CUN" else if [DEP_PORT_CODE] = "FCO" then "FCO" else if [DEP_PORT_CODE] = "FRA" then "FRA" else if [DEP_PORT_CODE] = "GIG" and [ARR_PORT_CODE] <> "AEP" or "EZE" then "GRU-BOND" else "GIG" else if [DEP_PORT_CODE] = "GRU" and [FLIGHT_NO] >= 8000 or [FLIGHT_NO] <= 1400 then "GRU-BOND" else "GRU" else if [DEP_PORT_CODE] = "GYE" then "GYE" else if [DEP_PORT_CODE] = "JFK" then "JFK" else if [DEP_PORT_CODE] "LAX" then "LAX" else if [DEP_PORT_CODE] = "LHR" then "LHR" else if [DEP_PORT_CODE] = "LIM" and [FLIGHT_NO] <= 2000 or >= 3000 then "LIM-ROW" else "LIM-LP" else if [DEP_PORT_CODE] "LIS" then "LIS" else if [DEP_PORT_CODE] = "MAD" then "MAD" else if [DEP_PORT_CODE] = "MCO" then "MCO" then if [DEP_PORT_CODE] = "MDE" then "MDE" then if "MEX" then "MEX" else if [DEP_PORT_CODE] = "MXP" then "MXP" else if [DEP_PORT_CODE] = "SCL" then "SCL" else if [DEP_PORT_CODE] = "UIO" then "UIO" else 0 

I need to do some filters in order to separate the treatment of the IATA codes according to the Flight number and the Arrival Airport.

I have revised the code many times, and as far as I'm new doing this I thing I'm a little blocked and can't see the solution as simple as it can be.


Solution

  • Formatting into human-readable code makes the errors obvious:

    1. then cannot be followed by then,
      see if [DEP_PORT_CODE] = "MCO" then "MCO" then
    2. else cannot be followed by else,
      see then "GRU-BOND" else "GIG" else
    3. comparing a column with text requires a condition,
      see if [DEP_PORT_CODE] "LAX" then
    4. the column name has to be repeated in multiple conditions,
      see if [DEP_PORT_CODE] = "GIG" and [ARR_PORT_CODE] <> "AEP" or "EZE" then

    Besides that you could simplify the code by just listing the cases where [DEP_PORT_CODE] is NOT returned and then putting [DEP_PORT_CODE] in the final else clause.

    Here's your formatted code:

    if [DEP_PORT_CODE] = "AKL" then "AKL" else 
    if [DEP_PORT_CODE] = "BCN" then "BCN" else 
    if [DEP_PORT_CODE] = "BOG" then "BOG" else 
    if [DEP_PORT_CODE] = "BOS" then "BOS" else 
    if [DEP_PORT_CODE] = "CDG" then "CDG" else 
    if [DEP_PORT_CODE] = "CUN" then "CUN" else 
    if [DEP_PORT_CODE] = "FCO" then "FCO" else 
    if [DEP_PORT_CODE] = "FRA" then "FRA" else 
    if [DEP_PORT_CODE] = "GIG" and [ARR_PORT_CODE] <> "AEP" or "EZE" then "GRU-BOND" else 
    "GIG" else 
    if [DEP_PORT_CODE] = "GRU" and [FLIGHT_NO] >= 8000 or [FLIGHT_NO] <= 1400 then "GRU-BOND" else 
    "GRU" else 
    if [DEP_PORT_CODE] = "GYE" then "GYE" else 
    if [DEP_PORT_CODE] = "JFK" then "JFK" else 
    if [DEP_PORT_CODE] "LAX" then "LAX" else 
    if [DEP_PORT_CODE] = "LHR" then "LHR" else 
    if [DEP_PORT_CODE] = "LIM" and [FLIGHT_NO] <= 2000 or >= 3000 then "LIM-ROW" else 
    "LIM-LP" else 
    if [DEP_PORT_CODE] "LIS" then "LIS" else 
    if [DEP_PORT_CODE] = "MAD" then "MAD" else 
    if [DEP_PORT_CODE] = "MCO" then "MCO" then 
    if [DEP_PORT_CODE] = "MDE" then "MDE" then 
    if "MEX" then "MEX" else 
    if [DEP_PORT_CODE] = "MXP" then "MXP" else 
    if [DEP_PORT_CODE] = "SCL" then "SCL" else 
    if [DEP_PORT_CODE] = "UIO" then "UIO" else 0