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
Shine_456
Frequent Visitor

Dax query as a column for date between 2W and 1M, 1M and 3M, etc...

Hi All,

 

Can anyone help me on how to get number of users between the date range of 2week and 1Month, 1Month and 3 Month, 3Month and 1 Year others as more than 1 year as abar chart.

I need a query for date ranges as mentioned above.

 

Thank you.

1 ACCEPTED SOLUTION
AllisonKennedy
Super User
Super User

@Shine_456  I would suggest doing this in Power Query rather than DAX. 

Click Transform Data, Add Column > Custom Column. 

 

Then put a code similar to this (replace [Date] with the name of your date column and replace the ranges and text with what you want). 

 

 

if [Date] >= Date.AddWeeks( Date.From( DateTime.FixedLocalNow() ) , -2) then "2 weeks"
else if [Date] >= Date.AddMonths( Date.From( DateTime.FixedLocalNow() ) , -1) then "2 weeks - 1 month"
else if [Date] >= Date.AddMonths( Date.From( DateTime.FixedLocalNow() ) , -3) then "1 - 3 months"
else if [Date] >= Date.AddMonths( Date.From( DateTime.FixedLocalNow() ) , -12) then "3 - 12 months"
else "Older than 1 year"

 

AllisonKennedy_0-1675645273670.png

 


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

View solution in original post

3 REPLIES 3
AllisonKennedy
Super User
Super User

@Shine_456  I would suggest doing this in Power Query rather than DAX. 

Click Transform Data, Add Column > Custom Column. 

 

Then put a code similar to this (replace [Date] with the name of your date column and replace the ranges and text with what you want). 

 

 

if [Date] >= Date.AddWeeks( Date.From( DateTime.FixedLocalNow() ) , -2) then "2 weeks"
else if [Date] >= Date.AddMonths( Date.From( DateTime.FixedLocalNow() ) , -1) then "2 weeks - 1 month"
else if [Date] >= Date.AddMonths( Date.From( DateTime.FixedLocalNow() ) , -3) then "1 - 3 months"
else if [Date] >= Date.AddMonths( Date.From( DateTime.FixedLocalNow() ) , -12) then "3 - 12 months"
else "Older than 1 year"

 

AllisonKennedy_0-1675645273670.png

 


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

Thanks @AllisonKennedy . Your query worked.

I had some null in date. How to replace them from above query?

@Shine_456 , what do you want to replace the null with?

you can either do this as a separate step first using 'replace values' and type 'null' in the value to find/replace box, or you can add a line at the start of the new column query: 

 

if [Date] = null then "NA"

else if [Date] >= Date.AddWeeks( Date.From( DateTime.FixedLocalNow() ) , -2) then "2 weeks"
else if [Date] >= Date.AddMonths( Date.From( DateTime.FixedLocalNow() ) , -1) then "2 weeks - 1 month"
else if [Date] >= Date.AddMonths( Date.From( DateTime.FixedLocalNow() ) , -3) then "1 - 3 months"
else if [Date] >= Date.AddMonths( Date.From( DateTime.FixedLocalNow() ) , -12) then "3 - 12 months"
else "Older than 1 year"


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

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.