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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

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
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.