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

Query Editor Custom Column If function on Date

Hi, I have to create a custom column in the query editor which will show an indicator for the number of days, based on date.

 

Str.PNG

If using calculated columns the formula will show as below... 

Last Two Months = IF(TODAY()-[Date]<=30,1,IF(TODAY()-[Date]<=60,2,0))

How can I get the same result when creating a custom column in the query editor?

Thank you in advance.

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

In general, your formula will be:

 

= if Duration.Days(DateTime.LocalNow() - [Date]) <= 30 then 1 else if Duration.Days(DateTime.LocalNow() - [Date]) <= 60 then 2 else 0

Couple of things. First, if you have dates in the future, your days between will come out negative (which is <=30) so you might wish to account for that.

 

Second, this assumes a DateTime column type for [Date]. If you do not have a DateTime column type for Date, you can add a step to transform that column to DateTime before you add this custom column. Otherwise, assuming that you have a Date or Text column, your formula is:

 

= if Duration.Days(DateTime.LocalNow() - DateTime.From([Date])) <= 30 then 1 else if Duration.Days(DateTime.LocalNow() - DateTime.From([Date])) <= 60 then 2 else 0

 

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

2 REPLIES 2
Greg_Deckler
Super User
Super User

In general, your formula will be:

 

= if Duration.Days(DateTime.LocalNow() - [Date]) <= 30 then 1 else if Duration.Days(DateTime.LocalNow() - [Date]) <= 60 then 2 else 0

Couple of things. First, if you have dates in the future, your days between will come out negative (which is <=30) so you might wish to account for that.

 

Second, this assumes a DateTime column type for [Date]. If you do not have a DateTime column type for Date, you can add a step to transform that column to DateTime before you add this custom column. Otherwise, assuming that you have a Date or Text column, your formula is:

 

= if Duration.Days(DateTime.LocalNow() - DateTime.From([Date])) <= 30 then 1 else if Duration.Days(DateTime.LocalNow() - DateTime.From([Date])) <= 60 then 2 else 0

 

 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thank you very much! This solved my problem !  Smiley Very Happy

Thank you for spending time on this. 

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.