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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
proavinash
Helper I
Helper I

Dax Function when select a date or months or Quarter we will get Number of days MTD, QTD and YTD

Need Dax Function when select a date or month or 2 months or Quarter we will get Number of days MTD, QTD and YTD according to current date.

 

for an example , 

 

if we select 1-May-2024 from dropdown or from slicer of date range and today is 25-May-2024 then it will give MTD days number is 25 days, QTD days number is 55 days, and YTD days number is near about 145,

 

Selected Date  =  1-May-2024

Today Date = 25-May-2024

 

MTD = 25  ,    QTD = 55   and   YTD = 145

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @proavinash ,

 

I recommend that you use the DATEIFF() function, which is used to calculate the difference between two dates.

We can create three measures.

MTD = 
var _date1=DATE(YEAR(TODAY()), MONTH(TODAY()), 1)
var _date2=TODAY()
RETURN DATEDIFF(_date1,_date2,DAY)+1
QTD = 
var _date1=DATE(YEAR(TODAY()), FLOOR(MONTH(TODAY())-1, 3)+1, 1)
var _date2=TODAY()
RETURN DATEDIFF(_date1,_date2,DAY)+1
YTD = 
var _date1=DATE(YEAR(TODAY()), 1, 1)
var _date2=TODAY()
RETURN DATEDIFF(_date1,_date2,DAY)+1

Then the result is as follows.

vtangjiemsft_0-1732517399201.png

 

Best Regards,

Neeko Tang

If this post  helps, then please consider Accept it as the solution  to help the other members find it more quickly. 

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

Hi @proavinash ,

 

I recommend that you use the DATEIFF() function, which is used to calculate the difference between two dates.

We can create three measures.

MTD = 
var _date1=DATE(YEAR(TODAY()), MONTH(TODAY()), 1)
var _date2=TODAY()
RETURN DATEDIFF(_date1,_date2,DAY)+1
QTD = 
var _date1=DATE(YEAR(TODAY()), FLOOR(MONTH(TODAY())-1, 3)+1, 1)
var _date2=TODAY()
RETURN DATEDIFF(_date1,_date2,DAY)+1
YTD = 
var _date1=DATE(YEAR(TODAY()), 1, 1)
var _date2=TODAY()
RETURN DATEDIFF(_date1,_date2,DAY)+1

Then the result is as follows.

vtangjiemsft_0-1732517399201.png

 

Best Regards,

Neeko Tang

If this post  helps, then please consider Accept it as the solution  to help the other members find it more quickly. 

Thank you so much, it's working.

xifeng_L
Super User
Super User

Hi @proavinash ,

 

You can use the DATESMTD, DATESQTD and DATEYTD functions to calculate the corresponding number of days, for example:

 

MTD Days = COUNTROWS(DATESMTD('Calendar'[Date]=TODAY()))
QTD Days = COUNTROWS(DATESQTD('Calendar'[Date]=TODAY()))
YTD Days = COUNTROWS(DATESYTD('Calendar'[Date]=TODAY()))

 

Then if you want to then control the returned result based on the number of items selected by the user, then you can slip in another IF function.

 

 

Did I answer your question? If yes, pls mark my post as a solution and appreciate your Kudos !

 

Thank you~

Thanks for your help, but this is not working because of i have large database in which near about 20 lacs of line item so countrows not give a proper solution, in a month either 31, 30 or 28 days, and countrows count all rows in a month or year, 

Kedar_Pande
Super User
Super User

@proavinash 

MTD = 
CALCULATE(
COUNTROWS('DateTable'),
'DateTable'[Date] <= TODAY(),
'DateTable'[Date] >= DATE(YEAR(TODAY()), MONTH(TODAY()), 1)
)
QTD = 
CALCULATE(
COUNTROWS('DateTable'),
'DateTable'[Date] <= TODAY(),
'DateTable'[Date] >= DATE(YEAR(TODAY()), FLOOR(MONTH(TODAY())-1, 3)+1, 1)
)
YTD = 
CALCULATE(
COUNTROWS('DateTable'),
'DateTable'[Date] <= TODAY(),
'DateTable'[Date] >= DATE(YEAR(TODAY()), 1, 1)
)

💌 If this helped, a Kudos 👍 or Solution mark would be great! 🎉
Cheers,
Kedar
Connect on LinkedIn

Thanks for your help, but this is not working because of i have large database in which near about 20 lacs of line item so countrows not give a proper solution, in a month either 31, 30 or 28 days, and countrows count all rows in a month or year, 

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.