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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

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

 

 



Follow on LinkedIn
@ 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!:
Power BI Cookbook Third Edition (Color)

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

 

 



Follow on LinkedIn
@ 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!:
Power BI Cookbook Third Edition (Color)

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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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