Forum Discussion

WilliamAzevedo's avatar
WilliamAzevedo
Advocate II
2 years ago
Solved

New column based in multiple If statements in Power Query

Hello.

 

I'm trying to create a column based on three date columns, if the first is null, then look for the second and if that is also null look for the third. Here's the example:

 

M - InícioM - TérminoT - InícioT - TérminoN - InícioN - Término
09/04/2023 08:03:0009/04/2023 12:00:0009/04/2023 13:30:0009/04/2023 17:46:00nullnull
09/04/2023 08:00:0009/04/2023 12:00:0009/04/2023 13:30:0009/04/2023 17:30:00nullnull
09/04/2023 08:08:0009/04/2023 14:38:00nullnullnullnull
26/03/2023 08:02:0026/03/2023 12:02:0026/03/2023 13:33:0026/03/2023 17:33:00nullnull
nullnullnullnull04/04/2023 17:48:0004/04/2023 19:33:00
nullnullnullnull05/04/2023 17:48:0005/04/2023 21:48:00
01/04/2023 07:30:0001/04/2023 12:00:0001/04/2023 13:30:0001/04/2023 17:58:00nullnull
nullnullnullnull05/04/2023 17:18:0005/04/2023 22:12:00
nullnullnullnull05/04/2023 17:18:0005/04/2023 22:12:00
15/04/2023 08:00:0015/04/2023 12:00:0015/04/2023 12:00:0015/04/2023 17:45:00nullnull
16/04/2023 08:10:0016/04/2023 12:00:0016/04/2023 13:33:0016/04/2023 17:30:00nullnull
nullnullnullnull13/04/2023 17:48:0013/04/2023 21:49:00
08/04/2023 08:00:0008/04/2023 12:00:0008/04/2023 12:00:0008/04/2023 17:00:00nullnull
09/04/2023 08:30:0009/04/2023 12:00:0009/04/2023 12:00:0009/04/2023 16:00:00nullnull
nullnull13/05/2023 14:04:0013/05/2023 19:33:00nullnull
nullnull23/06/2023 15:00:0023/06/2023 17:30:0023/06/2023 17:30:0023/06/2023 20:04:00
nullnull23/06/2023 15:00:0023/06/2023 17:30:0023/06/2023 17:30:0023/06/2023 20:04:00
nullnull23/06/2023 15:00:0023/06/2023 17:30:0023/06/2023 17:30:0023/06/2023 20:04:00
nullnull23/06/2023 15:00:0023/06/2023 17:30:0023/06/2023 17:30:0023/06/2023 20:04:00

 

But I would like to do it in Power Query and it isn't working. Follows the code and the result:

 

 

if [#"M - Início"] <> null then [#"M - Início"] else if [#"M - Início"] = null then [#"T - Início"] else if [#"T - Início"] = null then [#"N - Início"] else if [#"N - Início"] = null then [#"T - Início"] else [#"M - Início"]

 

M - InícioM - TérminoT - InícioT - TérminoN - InícioN - TérminoData
09/04/2023 08:03:0009/04/2023 12:00:0009/04/2023 13:30:0009/04/2023 17:46:00nullnull09/04/2023 08:03:00
09/04/2023 08:00:0009/04/2023 12:00:0009/04/2023 13:30:0009/04/2023 17:30:00nullnull09/04/2023 08:00:00
09/04/2023 08:08:0009/04/2023 14:38:00nullnullnullnull09/04/2023 08:08:00
26/03/2023 08:02:0026/03/2023 12:02:0026/03/2023 13:33:0026/03/2023 17:33:00nullnull26/03/2023 08:02:00
nullnullnullnull04/04/2023 17:48:0004/04/2023 19:33:00null
nullnullnullnull05/04/2023 17:48:0005/04/2023 21:48:00null
01/04/2023 07:30:0001/04/2023 12:00:0001/04/2023 13:30:0001/04/2023 17:58:00nullnull01/04/2023 07:30:00
nullnullnullnull05/04/2023 17:18:0005/04/2023 22:12:00null
nullnullnullnull05/04/2023 17:18:0005/04/2023 22:12:00null
15/04/2023 08:00:0015/04/2023 12:00:0015/04/2023 12:00:0015/04/2023 17:45:00nullnull15/04/2023 08:00:00
16/04/2023 08:10:0016/04/2023 12:00:0016/04/2023 13:33:0016/04/2023 17:30:00nullnull16/04/2023 08:10:00
nullnullnullnull13/04/2023 17:48:0013/04/2023 21:49:00null
08/04/2023 08:00:0008/04/2023 12:00:0008/04/2023 12:00:0008/04/2023 17:00:00nullnull08/04/2023 08:00:00
09/04/2023 08:30:0009/04/2023 12:00:0009/04/2023 12:00:0009/04/2023 16:00:00nullnull09/04/2023 08:30:00
nullnull13/05/2023 14:04:0013/05/2023 19:33:00nullnull13/05/2023 14:04:00
nullnull23/06/2023 15:00:0023/06/2023 17:30:0023/06/2023 17:30:0023/06/2023 20:04:0023/06/2023 15:00:00
nullnull23/06/2023 15:00:0023/06/2023 17:30:0023/06/2023 17:30:0023/06/2023 20:04:0023/06/2023 15:00:00
nullnull23/06/2023 15:00:0023/06/2023 17:30:0023/06/2023 17:30:0023/06/2023 20:04:0023/06/2023 15:00:00
nullnull23/06/2023 15:00:0023/06/2023 17:30:0023/06/2023 17:30:0023/06/2023 20:04:0023/06/2023 15:00:00

Where in the Column "Data" is null, I expected to have "N - Início". Can anyone tell me what did I do wrong?

 

Thank you in advance!

  • The very last else if is causing the issue - i think the code there should be

    if [#"N - Início"] <> null then [#"N - Início"] 

    If you're interested, there's the coalesce function in PowerQuery, which might make it a bit easier to read and debug your code:

    https://gorilla.bi/power-query/coalesce/

    So in your case, you can rewrite the if statement as:

    each [#"M - Início"] ?? [#"T - Início"] ?? [#"N - Início"] ?? null

3 Replies

  • The very last else if is causing the issue - i think the code there should be

    if [#"N - Início"] <> null then [#"N - Início"] 

    If you're interested, there's the coalesce function in PowerQuery, which might make it a bit easier to read and debug your code:

    https://gorilla.bi/power-query/coalesce/

    So in your case, you can rewrite the if statement as:

    each [#"M - Início"] ?? [#"T - Início"] ?? [#"N - Início"] ?? null
    • WilliamAzevedo's avatar
      WilliamAzevedo
      Advocate II

      Hi, vicky_!

       

      I changed the if functions so it reads like that:

      #"Personalização Adicionada" = Table.AddColumn(#"Colunas Renomeadas", "Data", each if [#"M - Início"] <> null then [#"M - Início"] else if [#"M - Início"] = null then [#"T - Início"] else if [#"T - Início"] = null then [#"N - Início"] else if [#"N - Início"] <> null then [#"N - Início"] else [#"M - Início"])

      No success, same results. No surprise if I still did something wrong.

       

      In the other hand, the coalesce function worked perfectly, besides looking beautifully neat! Thank you very much for all the help!

      • vicky_'s avatar
        vicky_
        Super User

        Oh haha whoops. I'm glad that at least the second part worked.