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 / Dates with conditions
Power Apps
Answered

Dates with conditions

(1) ShareShare
ReportReport
Posted on by 1,032

Hello everyone,
I am making a canvas application and there is a date field (which should only include the dates 07/mm/yyyy or 17/mm/yyyy or 27/mm/yyyy ) in which the user must insert a date according to the rules following:
If he entered a date between 01 and 07 then the value must be 07/mm/yyyy
If he entered a date between 08 and 17 then the date must be 17/mm/yyyy
If he entered a date between 18 and 27 then the date must be 27/mm/yyyy
If he entered a date greater than 27 then the date must be 06/mm+1/yyyy

 

how can i do this?
Thanks

Categories:
  • poweractivate Profile Picture
    11,078 Most Valuable Professional on at

    @gikido 

     

    Here's a starting point:

     

    DefaultDate of Date Control:

     

    myDefaultDate

     

    OnChange of Date Control (will work for "If he entered a date between 01 and 07 then the value must be 07/mm/yyyy")

     

    With
    (
     {myCurrentDate:Self.SelectedDate,myDay:Day(Self.SelectedDate)}
     ,If(myDay<=7,Set(myDefaultDate,Date(Year(myCurrentDate),Month(myCurrentDate),7)))
    );
    Reset(Self);

     

     

    For the full working formula ,use this for OnChange of Date Control:

     

    With
    (
     {myCurrentDate:Self.SelectedDate,myDay:Day(Self.SelectedDate)}
     ,If(myDay<=7,Set(myDefaultDate,Date(Year(myCurrentDate),Month(myCurrentDate),7)),If(myDay>=8&&myDay<=17,Set(myDefaultDate,Date(Year(myCurrentDate),Month(myCurrentDate),17)),If(myDay>=18&&myDay<=27,Set(myDefaultDate,Date(Year(myCurrentDate),Month(myCurrentDate),27)),Set(myDefaultDate,DateAdd(Self.SelectedDate,1,Days)))))
    );
    Reset(Self);

     

     

    The above will work, however, it will not respect your last rule exactly, instead, your last rule will instead become the first rule again, which makes it 7th day of the next month, which feels to me like it might make more sense. It might be significantly more difficult to have it set to the day 6 on the last rule -d o you really want it that way?

     

     

  • gikido Profile Picture
    1,032 on at

    @poweractivate  Thanks, it works with the first condition. I forgot another condition, this date must be less than today's date.

  • poweractivate Profile Picture
    11,078 Most Valuable Professional on at

    @gikido 

    If the date selected is greater than today's date, what should happen?

  • gikido Profile Picture
    1,032 on at

    @poweractivate  reset the datepicker and display an error message

  • gikido Profile Picture
    1,032 on at

    @poweractivate  the full code works only for the first condition, it always gives 07/mm/yyyy whatever the date entered

  • poweractivate Profile Picture
    11,078 Most Valuable Professional on at

    @gikido 

     

    For DefaultDate of the Date Picker use this:

     

    If(myDefaultDate>Today(),Today(),myDefaultDate)

     

     

    For OnChange of the Date Picker use this:

     

    With
    (
     {myCurrentDate:Self.SelectedDate,myDay:Day(Self.SelectedDate)}
     ,If(myCurrentDate>Today(),Set(myDefaultDate,DateAdd(Self.SelectedDate,1,Days));Notify("Date must not be later than today's date",NotificationType.Error),If(myDay<=7,Set(myDefaultDate,Date(Year(myCurrentDate),Month(myCurrentDate),7)),If(myDay>=8&&myDay<=17,Set(myDefaultDate,Date(Year(myCurrentDate),Month(myCurrentDate),17)),If(myDay>=18&&myDay<=27,Set(myDefaultDate,Date(Year(myCurrentDate),Month(myCurrentDate),27)),Set(myDefaultDate,DateAdd(Self.SelectedDate,1,Days)))))
    ));
    Reset(Self);

     

     

    If the date  is greater than today, an error is displayed, and the Date Picker is reset to show today's date.

     

    If the day is greater than 27, I have changed your last rule. Instead of specifically setting the day to 6, it will set the day to the day +1. However, it actually will end up keeping on doing this, until it reaches your first rule again (since the 1st day is less than 7), which ends up setting it to 7th day of the following month.

     

    I think it might work better that way. Not only that, but if selecting say the end of October, causes the first rule to be hit to try and set it to November 7, it will actually find out that it went past Today, and should end up setting it to Today. So I think it might work better this way.

     

    In case you really must have the final rule set it to the 6th day of the month and not the 7th, this might be sort of tricky, I'll give the above for now which wraps your last rule back into the first rules, as I feel like it might be working more smoothly that way.

  • poweractivate Profile Picture
    11,078 Most Valuable Professional on at

    @gikido wrote:

    @poweractivate  the full code works only for the first condition, it always gives 07/mm/yyyy whatever the date entered


    Try the full formulas in my most recent post. Note that DefaultDate is a bit different. Also, notice that OnChange this time has all of your rules, including to enforce it not to be greater than Today's date. Note that it was easier if I changed your last rule to end up giving a result that just matches the first rule (of the following month), and also ends up continuing to enforce the rule that the date should not be greater than today's date.

  • gikido Profile Picture
    1,032 on at

    @poweractivate now it works; except that the error notification still appears even if you enter a valid date; and datepicker does not reset on error.

  • poweractivate Profile Picture
    11,078 Most Valuable Professional on at

    @gikido 

     

    Just in case, I have a new version below of the which respects your rules exactly (it will use 6th of the following month if the last day of the month is selected, yet it will use the 7th of the same month if the first rule is triggered), and I think it's better. The rules don't retrigger themselves, they stop once a rule is reached. Also, the date picker is reset to Blank() if the selected date is greater than today's date:

     

    OnVisible of the Screen the Date Picker is on:

     

    Set(myLastSelectedDate,Blank());Set(myDefaultDate,Blank());

     

     

    DefaultDate of the Date Picker:

     

    myDefaultDate

     

     

    OnChange of the Date Picker:

     

    Set(myLastSelectedDate,Self.SelectedDate);
    If(!(myLastSelectedDate = myDefaultDate)
     ,With
     (
     {myCurrentDate:Self.SelectedDate,myDay:Day(Self.SelectedDate)}
     ,If(myCurrentDate>Today(),Set(myDefaultDate,DateTimeValue(Blank()));Notify("Date must not be later than today's date",NotificationType.Error),If(myDay<=7,Set(myDefaultDate,Date(Year(myCurrentDate),Month(myCurrentDate),7)),If(myDay>=8&&myDay<=17,Set(myDefaultDate,Date(Year(myCurrentDate),Month(myCurrentDate),17)),If(myDay>=18&&myDay<=27,Set(myDefaultDate,Date(Year(myCurrentDate),Month(myCurrentDate),27)),Set(myDefaultDate,Date(Year(myCurrentDate),Month(myCurrentDate)+1,6)))))
    )
     );Set(myLastSelectedDate,myDefaultDate);Reset(Self)
     ,Set(myLastSelectedDate,Blank())
    )

     

     

    In the above version, the set date may be later than today's date in some cases - however, if the selected date is later than today's date, it will show an error, and reset the date picker to a Blank value.

     

    See if you like it better like this @gikido 

  • poweractivate Profile Picture
    11,078 Most Valuable Professional on at

    @gikido 

     


    @gikido wrote:

    @poweractivate now it works; except that the error notification still appears even if you enter a valid date; and datepicker does not reset on error.


    I gave a new version for DefaultDate and for OnChange which is respecting the original rules you gave more exactly, and I like it a bit better. I also think it might happen to address the concern you just gave. See if you like it better as well.

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 339 Super User 2026 Season 2

#2
M Bilal Khan Profile Picture

M Bilal Khan 289

#3
Mohsin Ali Profile Picture

Mohsin Ali 228

Last 30 days Overall leaderboard