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

Community site session details

Session Id :
Power Apps - Building Power Apps
Unanswered

Code problem - Expire date for certification based on age

(0) ShareShare
ReportReport
Posted on by 18

I have this code that I'm getting an error on but I'm not sure what the issue is? I'm in europe so we use semicolon:

If(
!IsBlank(DataCardValue11_1.SelectedDate) && !IsBlank(DataCardValue13_1.SelectedDate);
DateAdd(
DataCardValue13_1.SelectedDate;
Switch(
RoundDown((Today() - DataCardValue11_1.SelectedDate) / 365; 0);
0; 5;
1 to 21 step 5; 5;
22 to 40 step 3; 3;
41 to 62 step 1; 1;
63 to 150 step 0; 1
);
TimeUnit.Years
);
Blank()
)

 

The formula uses the If function to check if both DataCardValue11_1.SelectedDate and DataCardValue13_1.SelectedDate are not blank. If both dates have been selected, the formula uses the DateAdd function to calculate a new date based on the second selected date (DataCardValue13_1.SelectedDate) and the number of years to add.

The number of years to add is determined by the Switch function, which takes the result of the RoundDown function as its first argument. The RoundDown function calculates the number of years between the first selected date (DataCardValue11_1.SelectedDate) and the current date (Today), and rounds down to the nearest whole number. This rounded down number of years is then used as an index for the Switch function.

The Switch function returns a different number of days to add depending on the rounded down number of years. For example, if the rounded down number of years is between 1 and 21 (inclusive), the formula adds 5 days to the second selected date. If the rounded down number of years is between 22 and 40 (inclusive), the formula adds 3 days. If the rounded down number of years is between 41 and 62 (inclusive), the formula adds 1 day. If the rounded down number of years is greater than 62, the formula also adds 1 day.

If either of the two selected dates is blank, the formula returns a blank value.

Categories:
I have the same question (0)
  • JD382 Profile Picture
    114 on at
    Re: Code problem - Expire date for certification based on age

    I'm not familiar with the "to...step" you have. AFAIK switch can only use a single match_value.

     

    https://powerusers.microsoft.com/t5/Building-Power-Apps/Switch-function-Is-it-possible-to-have-a-match-value-equal-to-a/m-p/1961551 

     

    You could create a variable equal to the difference and then use an If statement on the variable's value (i.e. X>=22 && X<=40) to calculate your result.

  • Ryuken3D Profile Picture
    18 on at
    Re: Code problem - Expire date for certification based on age

    Thanks,
    After asking chatGPT to remove step I got something that is working.

    If(
    !IsBlank(DataCardValue11_1.SelectedDate) && !IsBlank(DataCardValue13_1.SelectedDate);
    DateAdd(
    DataCardValue13_1.SelectedDate;
    If(
    RoundDown((Today() - DataCardValue11_1.SelectedDate) / 365; 0) = 0; 5;
    RoundDown((Today() - DataCardValue11_1.SelectedDate) / 365; 0) >= 1 && RoundDown((Today() - DataCardValue11_1.SelectedDate) / 365; 0) <= 21; 5;
    RoundDown((Today() - DataCardValue11_1.SelectedDate) / 365; 0) >= 22 && RoundDown((Today() - DataCardValue11_1.SelectedDate) / 365; 0) <= 40; 3;
    RoundDown((Today() - DataCardValue11_1.SelectedDate) / 365; 0) >= 41 && RoundDown((Today() - DataCardValue11_1.SelectedDate) / 365; 0) <= 62; 1;
    RoundDown((Today() - DataCardValue11_1.SelectedDate) / 365; 0) >= 63 && RoundDown((Today() - DataCardValue11_1.SelectedDate) / 365; 0) <= 150; 1;
    /* Optional default value if no conditions are met: */ Blank()
    );
    TimeUnit.Years
    );
    Blank()
    )

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

Coming soon: forum hierarchy changes

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 803 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 314 Super User 2025 Season 2

#3
MS.Ragavendar Profile Picture

MS.Ragavendar 253 Super User 2025 Season 2

Last 30 days Overall leaderboard