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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
lukmtb08
Helper III
Helper III

Power Query Date checks

Hi community, 
I have the following columns. Could you please define the Power Query M code to create the column "Check Start"?

Actual StartPlanned StartCheck Start
nullnull0
null2/2/2023 (could be any date before today, or today)0
null2/2/2023 (could be any date after today)1
2/2/20242/2/20240
2/2/20241/2/20241
2/2/2024null0
1 ACCEPTED SOLUTION

Hi, @lukmtb08 

Thank you very much for your reply. Sorry for the late reply. Has your problem been solved now? Maybe you can try the following code:

test = Table.AddColumn(#"Changed Type","Check start1",each 
    if [Actual Start] = null and [Planned Start] =null then 0
    else if [Actual Start] = null and [Planned Start]<= Date.From(DateTime.LocalNow()) then 1
    else if [Actual Start] > [Planned Start] and [Planned Start] <> null then 1
    else if [Actual Start] <> null and [Planned Start] = null then 0
    else if [Actual Start] = null and [Planned Start] > Date.From(DateTime.LocalNow()) then 0
    else if [Actual Start] = null and [Planned Start] < Date.From(DateTime.LocalNow()) then 0
    else 0
    )

 

 

Best Regards

Jianpeng Li

View solution in original post

12 REPLIES 12
lukmtb08
Helper III
Helper III

Thanks again! 🙂 

I still have an error, when Actual Start = null and Planned Start > Today. 

Hi, @lukmtb08 

Thank you very much for your reply, I added this condition to the original M code, and the latest M code is below:

test = Table.AddColumn(#"Changed Type","Check start1",each 
    if [Actual Start] = null and [Planned Start] =null then 0
    else if [Actual Start] = null and [Planned Start]<= Date.From(DateTime.LocalNow()) then 1
    else if [Actual Start] > [Planned Start] and [Planned Start] <> null then 1
    else if [Actual Start] <> null and [Planned Start] = null then 0
    else if [Actual Start] = null and [Planned Start] > Date.From(DateTime.LocalNow()) then 0
    else 0
    )

vjianpengmsft_0-1709887402976.png

vjianpengmsft_1-1709887417926.png

 

 

lbendlin
Super User
Super User

What's the logic behind that column?

If Actual Start > Planned Start: 1
If Actual Start = null and Planned Start <= Todays date: 1
Elso 0

Your explanation and your sample column don't seem to match

 

lbendlin_0-1709909145028.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WyivNyVHSgVEGSrE6cDEjfSA0MDLGIW4KZBqCxaECJgg5E7geJAFDJCa6HML+WAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Actual Start" = _t, #"Planned Start" = _t, #"Check Start" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Actual Start", type date}, {"Planned Start", type date}, {"Check Start", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Check", each if ([Actual Start]??Date.From(DateTime.FixedLocalNow())) > ([Planned Start]??Date.From(DateTime.FixedLocalNow())) then 1 else 0)
in
    #"Added Custom"

Hi, @lukmtb08 

Based on your description, I use the following Sample data:

vjianpengmsft_1-1709871437327.png

I add a new column with the following M code:

    test = Table.AddColumn(#"Changed Type","Check start1",each 
    if [Actual Start] = null and [Planned Start] =null then 0
    else if [Actual Start] = null and [Planned Start]<= Date.From(DateTime.LocalNow()) then 1
    else if [Actual Start] > [Planned Start] and [Planned Start] <> null then 1
    else 0
    )

vjianpengmsft_2-1709871551266.png

The execution result is as follows:

vjianpengmsft_3-1709871648991.png

I have provided the PBIX file below, it would be great if it could help you.

 

 

 

How to Get Your Question Answered Quickly

If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data) .

Best Regards

Jianpeng Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Dear @v-jianpeng-msft, thank you very much for supporting me - highly appreciated!
Generally it works, but for some rows I get the following error in my dataflow: 

Expression.Error: We cannot convert the value null to type Logical.

Details
Reason = Expression.Error

Hi, @lukmtb08 

Thank you very much for your reply, I used the following data, you can copy and paste into Excel, then save your Excel, and set it up as below in Power BI Desktop:

Actual Start Planned Start Check Start
null null 0
null 2/2/2023 0
null 2/2/2023 1
2/2/2024 2/2/2024 0
2/2/2024 1/2/2024 1
2/2/2024 null 0

vjianpengmsft_0-1709883355089.png

vjianpengmsft_1-1709883442609.png

thanks! In the dataflow, I get for some rows the follwing error: 


Expression.Error: We cannot convert the value null to type Logical.

Details
Reason = Expression.Error

Hi, @lukmtb08 

Thank you very much for your reply, this issue may be due to a missing condition. I rewrote an M code that returns 0 when the start of the plan is equal to empty and the actual start is not equal to empty:

 test = Table.AddColumn(#"Changed Type","Check start1",each 
    if [Actual Start] = null and [Planned Start] =null then 0
    else if [Actual Start] = null and [Planned Start]<= Date.From(DateTime.LocalNow()) then 1
    else if [Actual Start] > [Planned Start] and [Planned Start] <> null then 1
    else if [Actual Start] <> null and [Planned Start] = null then 0
    else 0
    )

vjianpengmsft_0-1709886074675.png

vjianpengmsft_1-1709886089813.png

 

Unfortunately, I have the same issue 

Hi, @lukmtb08 

Thank you very much for your reply. Sorry for the late reply. Has your problem been solved now? Maybe you can try the following code:

test = Table.AddColumn(#"Changed Type","Check start1",each 
    if [Actual Start] = null and [Planned Start] =null then 0
    else if [Actual Start] = null and [Planned Start]<= Date.From(DateTime.LocalNow()) then 1
    else if [Actual Start] > [Planned Start] and [Planned Start] <> null then 1
    else if [Actual Start] <> null and [Planned Start] = null then 0
    else if [Actual Start] = null and [Planned Start] > Date.From(DateTime.LocalNow()) then 0
    else if [Actual Start] = null and [Planned Start] < Date.From(DateTime.LocalNow()) then 0
    else 0
    )

 

 

Best Regards

Jianpeng Li

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Kudoed Authors