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
Buddhima_Edi
Regular Visitor

Custom Column adding dates using null values error

Hi All,

 

I have the following table in Power Query:

 

Compliance.JPG

 

I am trying to create a custom column which satisfies the following criteria:

  • If the [Completion Date] <= [Due Date] then the custom column row item is "Compliant" (else "Non-Compliant")
  • If the [Completion Date] = null then the [Response Time] is to be added to the [Inspection Date] and if the resultant date is >= today's date then it is "Compliant" (else Non-Compliant)

I've had a go at it here:

  • if [Completion Date] <= [Due Date]
    then "Compliant"
    else if [Completion Date] = null
           then if [Response Time] + [Inspection Date]*1 >= Date.From(DateTime.LocalNow())
                   then "Compliant"
                   else "Non-Compliant"
           else "Non-Compliant"

When I run the query I get a Custom Column with "Compliant" and "Non-Compliant" values along with error values for every "null" in [Completion Date], I suspect it's got something to do with the way I'm referring to today's date in the query.

 

Please let me know what you think and thank you in advance.

1 ACCEPTED SOLUTION
v-frfei-msft
Community Support
Community Support

Hi @Buddhima_Edi ,

 

Add the custom column as below should work.

 

if [Completion Date] = null then if Date.AddDays([Inspection Date],[Response Time]) >= DateTime.Date(DateTime.LocalNow())
then "Compliant" else "Non-Compliant" else if [Completion Date] <= [Due Date] then "Compliant"
else "Non-Compliant"

M code for your reference.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtW30DcyMLRQ0lEy0zeHMZFEjQ2UYnWIV2cC5CNkEWwTfWMDDCMNkcw0hDGRzTK0RJiFYGM3AsgGucvIEM0MZDeAzYCpMCNWBVXDKBYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Inspection Date" = _t, #"Due Date" = _t, #"Completion Date" = _t, #"Response Time" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Inspection Date", type date}, {"Due Date", type date}, {"Completion Date", type date}, {"Response Time", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [Completion Date] = null then if Date.AddDays([Inspection Date],[Response Time]) >= DateTime.Date(DateTime.LocalNow())
then "Compliant" else "Non-Compliant" else if [Completion Date] <= [Due Date] then "Compliant"
else "Non-Compliant")
in
    #"Added Custom"

Regards,

Frank

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

View solution in original post

2 REPLIES 2
v-frfei-msft
Community Support
Community Support

Hi @Buddhima_Edi ,

 

Add the custom column as below should work.

 

if [Completion Date] = null then if Date.AddDays([Inspection Date],[Response Time]) >= DateTime.Date(DateTime.LocalNow())
then "Compliant" else "Non-Compliant" else if [Completion Date] <= [Due Date] then "Compliant"
else "Non-Compliant"

M code for your reference.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtW30DcyMLRQ0lEy0zeHMZFEjQ2UYnWIV2cC5CNkEWwTfWMDDCMNkcw0hDGRzTK0RJiFYGM3AsgGucvIEM0MZDeAzYCpMCNWBVXDKBYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Inspection Date" = _t, #"Due Date" = _t, #"Completion Date" = _t, #"Response Time" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Inspection Date", type date}, {"Due Date", type date}, {"Completion Date", type date}, {"Response Time", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [Completion Date] = null then if Date.AddDays([Inspection Date],[Response Time]) >= DateTime.Date(DateTime.LocalNow())
then "Compliant" else "Non-Compliant" else if [Completion Date] <= [Due Date] then "Compliant"
else "Non-Compliant")
in
    #"Added Custom"

Regards,

Frank

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

It works! Thank you for taking the time to help me!

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.