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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
WilliamAzevedo
Helper III
Helper III

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!

1 ACCEPTED SOLUTION
vicky_
Super User
Super User

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

View solution in original post

3 REPLIES 3
vicky_
Super User
Super User

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

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!

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

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.