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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
johnce123
Helper I
Helper I

DAX Date help

I have a situation where i want to count the number of days available in the current year. 

There is a start date column of say 01/01/2000 

End date column of say 31/12/2099

 

This would need to return availability of 366 Days

 

However a start date may be entered as 01/04/2020

End date 31/12/2099

 

This would need to return 275 Days. 

 

Help appreciated

2 ACCEPTED SOLUTIONS
camargos88
Community Champion
Community Champion

@johnce123 ,

 

You can use this measure:

_QtDays = 
SUMX('Table',
        IF(
            YEAR('Table'[StartDate]) <> YEAR(TODAY()), 
            DATEDIFF(DATE(YEAR(TODAY()), 1,1), DATE(YEAR(TODAY()), 12,31), DAY) + 1,
            DATEDIFF('Table'[StartDate], DATE(YEAR('Table'[StartDate]), 12,31), DAY) + 1
        ))

 

Capture.PNG



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



View solution in original post

@johnce123 ,

 

Try this measure:

 

_QtDays = 
SUMX(
    'Table',
    COUNTROWS(FILTER(CALENDAR('Table'[StartDate], 'Table'[EndDate]), YEAR([Date]) = YEAR(TODAY())))
)

 

 

 

Capture.PNG



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



View solution in original post

6 REPLIES 6
AllisonKennedy
Super User
Super User

@johnce123 Do you have a DimDate table? What format is your data in (ie how are the start and end dates entered and in what table and related to DimDate table?)?  You could use: 
Days In Year = 

VAR _StartDate = IF(YEAR(Table[StartDate]) <YEAR(TODAY()), DATE(YEAR(TODAY()), 1, 1), Table[StartDate])

VAR _EndDate = IF(YEAR(Table[EndDate]) > YEAR(TODAY()), DATE(YEAR(TODAY()), 12, 31), Table[EndDate])

RETURN
CALCULATE(COUNTROWS(ALL(DimDate)), DATESBETWEEN(_StartDate, _EndDate))


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

Hi, it's a direct query into an SQL database which is my live management system (business central) and returns a date along with a time. 

Thanks for your help

camargos88
Community Champion
Community Champion

@johnce123 ,

 

You can use this measure:

_QtDays = 
SUMX('Table',
        IF(
            YEAR('Table'[StartDate]) <> YEAR(TODAY()), 
            DATEDIFF(DATE(YEAR(TODAY()), 1,1), DATE(YEAR(TODAY()), 12,31), DAY) + 1,
            DATEDIFF('Table'[StartDate], DATE(YEAR('Table'[StartDate]), 12,31), DAY) + 1
        ))

 

Capture.PNG



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



Would this also take into account if the end date fell before the end of the current year 

 

so if my end date was 31/08/2020 it would calculate from the start date or beginning of year to the 31/08/2020?

@johnce123 ,

 

Try this measure:

 

_QtDays = 
SUMX(
    'Table',
    COUNTROWS(FILTER(CALENDAR('Table'[StartDate], 'Table'[EndDate]), YEAR([Date]) = YEAR(TODAY())))
)

 

 

 

Capture.PNG



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



ChrisMendoza
Resident Rockstar
Resident Rockstar

@johnce123 - You can use DATEDIFF like:

Measure Remaining Days = 
VAR beginDate = DATE(2020,4,1)
RETURN
DATEDIFF(beginDate,MAX('Table'[Date]),DAY )





Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



Helpful resources

Announcements
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.