cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
gikido
Helper V
Helper V

Dates with conditions

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

16 REPLIES 16

@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.

@gikido 

 

For the new version you may also need to add this

 

OnVisible of the Screen that the Date Picker is on:

Set(myLastSelectedDate,Blank());Set(myDefaultDate,Blank());
gikido
Helper V
Helper V

@poweractivate  for the last code nothing is displayed 

@gikido 

 

I tested the code and it worked for me

 

When you use all of the below code together in the correct controls and properties, it still does not work?

 

1. For the OnVisible property of the Screen the Date Picker is on, use this formula:

 

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

 

2. For the DefaultDate property of the Date Picker Control, use this formula:

 

myDefaultDate

 

3. For the OnChange property of the Date Picker Control, use this formula:

 

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())
)

 

4. Go to another Screen in the Editor, and then go to the Screen your Date Picker is on.

 

5. Use ALT + CLICK to test the Date Picker directly in the Editor, even without previewing the app. Alternatively, you can preview the app and click normally to test it.

 

Does it still not work @gikido after following above steps?

 

@gikido 

Please note the formulas given also make modifications to the Date control itself. For example if you select October 5, 2022, it will automatically set the Date Picker control to have the date selected on October 7, 2022. If selecting December 8, 2022 on today's date (which is early November), it should automatically set the Date field to Blank and show a red error message. Did you prefer to modify some Label's Text instead when the Date is selected, and generally leave the Date Picker unmodified by the formulas? Or is this way fine for you?

@gikido 

 

have attached a working sample msapp file app_dates_with_conditions.msapp 

if you would like to import a full, working example yourself for your convenience.

 

To use the sample msapp attached, follow these steps:

 

1) Download the msapp file attached to this post (it's at the bottom of this post), to Desktop or a folder of your choice, by clicking on it from this post.

 

2) Create a new, blank Power App Canvas App

 

3) Click on the three dotsellipses (just to the right of Settings)-> then from the menu that appears, click Open

poweractivate_0-1667393291182.png

 

4) Click Browse

poweractivate_1-1667393328619.png

5 ) Navigate to location of .msapp file from Step 1, select it, and press the "Open" button.

6 ) The working example msapp file should load

7 ) Click on Screen2 and then click on Screen1 once to initialize any collections, variables, or context variables, if needed.
8 ) You can use ALT+CLICK to check the Date picker directly in the Editor without previewing the app. Notice it should be working. You can also preview the app and click normally instead.

 

See if this helps @gikido 

 

In this version, I changed the formulas to use Context Variables (UpdateContext).

 

Note if you need to access this particular Date from any other Screen it is better to use Set (i.e. variable) like I first gave it.

Only variables using Set can be accessed from another Screen.

UpdateContext (i.e. context variables) must be accessed only from the same Screen.

 

If you are sure it's only this same Screen, use UpdateContext like I did here.

If you are going to use another Screen later to access this Date, you should use a variable instead, i.e. the formulas as I gave them initially (i.e. with Set rather than UpdateContext).

 

Screen1 OnVisible:

 

UpdateContext({myContextLastSelectedDate:Blank(),myContextDefaultDate:Blank()});

 

 

poweractivate_0-1667393754073.png

 

Label1 Text (for debugging - this can be removed if you want)

 

DateValue(myContextDefaultDate)

 

 

poweractivate_1-1667393804184.png

 

DatePicker DefaultDate

 

myContextDefaultDate

 

poweractivate_2-1667393855304.png

 

DatePicker OnChange

 

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

 

 

poweractivate_3-1667393958343.png

 

See if it helps @gikido 

gikido
Helper V
Helper V

@poweractivate  Thanks a lot

Helpful resources

Announcements
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Community Call Conversations

Introducing the Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Top Kudoed Authors
Users online (5,382)