Skip to main content

Notifications

Power Apps - Power Apps Pro Dev & ISV
Suggested answer

Issue with data calculations

(1) ShareShare
ReportReport
Posted on by 31
I'm trying to calculate a persons age based on 2 different date selectors.  This is what I have
 
DateDiff(
    'DatePicker1'.SelectedDate,
    DPR2.SelectedDate,
    TimeUnit.Years
) - If(
    DPReturnC1.SelectedDate < DateAdd(
        'DatePicker1'.SelectedDate,
        DateDiff(
            'DatePicker1'.SelectedDate,
            DPR2.SelectedDate,
            TimeUnit.Years
        ),
        TimeUnit.Years
    ),
    1,
    0
)
 
and for the most part it works fine !  However if I have a date for DatePicker1  well in the past for example January 10th 1960 and DPR2 is sometime this year it doesn't calculate correctly. It's giving an age of 9 !  Instead of 65 :)
 
Any ideas
  • WarrenBelz Profile Picture
    WarrenBelz 145,475 on at
    Issue with data calculations
    A quick follow-up to see if you received the answer you were looking for or if you need further assistance.

    Please click Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it a Like.
    MVP (Business Applications)    Visit my blog Practical Power Apps    LinkedIn    Buy me a coffee
  • WarrenBelz Profile Picture
    WarrenBelz 145,475 on at
    Issue with data calculations
    Hi @DBNL
    You can try this, however some testing here reveals DateDiff (in fact any Date function) does not like dates before 1/1/1970
    DateDiff(
       DatePicker1.SelectedDate,
       DPR2.SelectedDate,
       TimeUnit.Years
    ) - 
    If(
       Month(DatePicker1.SelectedDate) > Month(DPR2.SelectedDate) || 
       (
         Month(DatePicker1.SelectedDate) = Month(DPR2.SelectedDate)  && 
          Day(DatePicker1.SelectedDate) > Day(DPR2.SelectedDate)
       ),
       1
    )
     
    Please click Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it a Like.
    MVP (Business Applications)    Visit my blog Practical Power Apps    LinkedIn    Buy me a coffee
  • Suggested answer
    SaiRT14 Profile Picture
    SaiRT14 1,926 on at
    Issue with data calculations
    Hi 
     
    Pls try the following:
    DateDiff(
        DatePicker1.SelectedDate,
        DPR2.SelectedDate,
        TimeUnit.Years
    ) - If(
        DateValue(
            Text(
                DatePicker1.SelectedDate,
                "MM/dd"
            ) & "/" & Year(DPR2.SelectedDate)
        ) > DPR2.SelectedDate,
        1,
        0
    )
     
    thanks
     
     

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

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,475

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,767

Leaderboard