web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Calculated date field ...
Power Apps
Answered

Calculated date field and leap years

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

I'm experiencing an issue when using a calculated date field to determine total years of service for a user group. Here's the scenario: 

 

  • User A logs accesses the app which is connected to a SQL table containing their employee information, including their service date.
  • Using another date field, the user indicates their *potential exit.
  • A calculated field then displays the user's total number of service years.

 

The behavior I'm seeing is as follows:

 

  • User A has a service date of 7/21/2003 and they indicate 7/16/2022 as their *potential exit date.
  • The calculated field indicates their total years of service is 19 years. 
  • (If User A indicates 7/15/2022 as their *potential exit date, the total years of service indicated is 18.)

 

I would not expect 19 years to be displayed unless the *potential exit date was indicated as 7/21/2022. I'm guessing this has something to do with leap years, but not completely sure, nor can I figure out what function is necessary to correct this behavior. Any help is appreciated. Thanks in advance.

 

Mike

 

 

19 Years.jpg
19 Years v2.jpg
18 Years.jpg
Categories:
I have the same question (0)
  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @Anonymous 

    What is the formula you are using to do the calculation?

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    @RandyHayes 

     

    RoundDown(DateDiff(DateTimeValue(Service_Date_Field.Text), Last_Day_Worked_Date_Box.SelectedDate,Days)/365,0)

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @Anonymous 

    There is no need for you to do your own calculation on the amount of days.  You are only accounting for 365, which is not accurate for leap years.

     

    Change your formula to: 

    DateDiff(DateValue(Service_Date_Field.Text), Last_Day_Worked_Date_Box.SelectedDate, Years)

     

    Now the real question remains...your formula indicates you are rounding down with no fraction.  Is that something you want or do you want fractions of a year?

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Thanks for the reply. For the purpose of this calculator, there are not 'partial' years. (No fractions) So using the same dates as above, User A's service date is 7/21/2003, any date entered into the Last_Day_Worked_Date_Box that is BEFORE 7/21/2022 should render 18 years of service, and any date entered that is 7/21/2022 or later should render 19 years of service.  I confirmed with my comp team that service years are determined by the actual day of the month, not necessarily every 365 days. Hope this helps.

     

    Mike

  • Verified answer
    RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @Anonymous 

    Thanks!  That adds the true requirement then if it is based on the month and day as well as the count of years.

    So really, doing the year count and using the days is not helpful in this case.  What is needed is the year difference and then a comparison to the month/day of the year. For that last part, I would use a leap year as the determining year so that if a person has a start date of 2/29, then the calculations would be correct.

     

    The formula would be:

    With({_start: DateValue(Service_Date_Field.Text), _end: Last_Day_Worked_Date_Box.SelectedDate},
     Year(_end) - Year(_start) -1 + 
     If(DateDiff(Date(Year(2020), Month(_end), Day(_end)), Date(Year(2020), Month(_start), Day(_start)))>0, 0, 1)
    )

     

    Note in the above, 2020 is used as it is a leap year.  It need not ever change, it is just reference for the month/day part.

  • Community Power Platform Member Profile Picture
    Microsoft Employee on at

    Thank you so much for this solution. It appears to be working as expected for me and I have sent it to my client for User Acceptance Testing. Again, I really appreciate the insight and quick response. 

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @Anonymous 

    No problem!  Please choose which of the responses in this thread solved the issue for you and accept it as a solution to close this out.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 534

#2
WarrenBelz Profile Picture

WarrenBelz 416 Most Valuable Professional

#3
Valantis Profile Picture

Valantis 306

Last 30 days Overall leaderboard