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
joshcomputer1
Helper V
Helper V

Dax question on last 30 days/year

I am new to Power BI.....

 

I added a custom column in the query editor and all I want to do is add a filter for last 30 days, last 90 days, last 180 days. I figured there would be 3 columns to do this so I am working on the first one. 

 

if today()- [start date] <30 then 1 else 0

 

I have done this with the start date column being a date and with it being text and both throw an error "the name 'today' wasn't recognized. Make sure it's spelled correctly.

 

Any help on this is appreciated. 

 

 

1 ACCEPTED SOLUTION

@joshcomputer1

it looks like that IF statement compares Duration to Days

So try this instead...

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

EDIT:

https://msdn.microsoft.com/en-us/library/mt253516.aspx

You have to compare the same Data Types

So the column in Red calculates the Days correctly but displays them as Duration

that's why the previous attempt was failing

all we did here is first convert the red column to days and then check - is it less than 30

Duration.Days.png

Hope this helps! Smiley Happy

View solution in original post

6 REPLIES 6
Greg_Deckler
Super User
Super User

You're mixing DAX with your M, try DateTime.LocalNow


@ 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...

Thanks for the quick response.  I typed it in as 

=if DateTime.LocalNow() - [Start Date]<30 then 1 else 0

 

*Start Date is a date data type*

 

I am now getting this error:

 

Expression.Error: We cannot apply operator - to types DateTime and Date.
Details:
Operator=-
Left=4/5/2017 10:44:15 AM
Right=1/2/2017

@joshcomputer1

 

Try with little modification:

 

=if DateTime.Date(DateTime.LocalNow()) - [Start Date]<30 then 1 else 0




Lima - Peru

I am getting the following error with this formula.

=if DateTime.Date(DateTime.LocalNow()) - [Start Date]<30 then 1 else 0

 

Expression.Error: We cannot apply operator < to types Number and Duration.
Details:
Operator=&lt;
Left=30
Right=93.00:00:00

 

I have changed the data type to date, date time for both columns, but still get errors. Is there an easier way to do this. I just want a 1 if less than 30 days ago from [Start Date] (which is 1/2/2017 in the column)

@joshcomputer1

it looks like that IF statement compares Duration to Days

So try this instead...

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

EDIT:

https://msdn.microsoft.com/en-us/library/mt253516.aspx

You have to compare the same Data Types

So the column in Red calculates the Days correctly but displays them as Duration

that's why the previous attempt was failing

all we did here is first convert the red column to days and then check - is it less than 30

Duration.Days.png

Hope this helps! Smiley Happy

@joshcomputer1

 

Ok. 

 

Use this in the Custom Column:

 

 

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




Lima - Peru

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.