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
Anonymous
Not applicable

Power Query - custom column - date -2 and another condition

Hi, 

I want to create a new column based on  the condition that hortlistcount is 0 and Duedate is less than two days from now or overdue

If so, it should say 1 else 0. 

 

So I made this:

= Table.AddColumn(#"Changed Type1", "red", each if [DueDate] = Date.AddDays
(Date.From(DateTime.FixedLocalNow()), -2) and [ShortListCount] = 0 then "1"
else 0)

And it gives me 0 every time. 

 

Any idea what I'm doing wrong?

1 ACCEPTED SOLUTION

You have to ask first for null in order to avoid the comparison between null and dates.

if [DueDate] = null then 0 else
if [DueDate] <= Date.AddDays
(DateTime.Date(DateTime.LocalNow()), -2) and [ShortListCount] = 0 then 1
else 0

Be carefull with quotes. If you type "1" the result will be a text 1 and not a number.

 

Regards,


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

Happy to help!

LaDataWeb Blog

View solution in original post

12 REPLIES 12
ibarrau
Super User
Super User

Hi. In the definition you mention a duedate is less than two days from now and the formula is duedate is exactly two days ago.

Are you sure you are not looking for this:

= Table.AddColumn(#"Changed Type1", "red", each if [DueDate] <= Date.AddDays
(Date.From(DateTime.FixedLocalNow()), -2) and [ShortListCount] = 0 then "1"
else 0)

Be sure to show us a row that is not fulfilling this conditions in case you have 0 again.

 

Regards,


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

Happy to help!

LaDataWeb Blog

Anonymous
Not applicable

Hi @ibarrau and thanks for answering.

I tried that as well but then I get an error in all columns:

bilde.png

 

bilde.png

 

This should trigger 1

bilde.png

 

Ok, let's use a Date for now instead Datetime.

= Table.AddColumn(#"Changed Type1", "red", each if [DueDate] <= Date.AddDays
(DateTime.Date(DateTime.LocalNow()), -2) and [ShortListCount] = 0 then "1"
else 0)

This should work.

 

Regards,


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

Happy to help!

LaDataWeb Blog

Anonymous
Not applicable

@ibarrau 

Still the same error, but I get 0 when the duedate is null

 

bilde.png

 

seems to me that id does not like the < sign when using dates?

Can you please show us the error text? because I just test it and I'm sure it is not a date problem this time.


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

Happy to help!

LaDataWeb Blog

Anonymous
Not applicable

@ibarrauah, I managed to get it to work. I messed up the "applied steps" a bit and it broke cause of that.

 

So now I get both 0 and 1, but it still says that there are errors?

bilde.png

 

bilde.png

Anonymous
Not applicable

@ibarrau 

Ok, it shows error when duedate is null. That's logic 🙂 But is it a way to handle that? show 0 if duedate is null

Hi, sure you can add and if [duedate] = null then ......... else [start the other condition]

 

You can decide if you want a value for this cases like 0 or 1 and add it there.

 

Regards,


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

Happy to help!

LaDataWeb Blog

Anonymous
Not applicable

I'm still getting errors on that one.

I have this one now:

if [DueDate] <= Date.AddDays
(DateTime.Date(DateTime.LocalNow()), -2) and [ShortListCount] = 0 then "1"
else if [DueDate] = null then 0
else 0

get this error:

bilde.png

You have to ask first for null in order to avoid the comparison between null and dates.

if [DueDate] = null then 0 else
if [DueDate] <= Date.AddDays
(DateTime.Date(DateTime.LocalNow()), -2) and [ShortListCount] = 0 then 1
else 0

Be carefull with quotes. If you type "1" the result will be a text 1 and not a number.

 

Regards,


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

Happy to help!

LaDataWeb Blog

Anonymous
Not applicable

hi @ibarrau . 

Seems like the formula is a bit buggy. I wanted it to set the value to 1 if the duedate is three days from now or less and shortlist is 0. I now have a case where duedate is tomorrow and shortlist is 0, and the value is still 0.  

 

Do you see any obvius reasons for it not to be 1?

Anonymous
Not applicable

@ibarrauthanks a lot!

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 Solution Authors