cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
rvdhorst
Helper II
Helper II

Datepicker latest date +1 day

I have a SQL table containing a Date,

 

Now I want my datepicker to see what the latest date is in the table and then set the defaultdate + 1.

5 ACCEPTED SOLUTIONS

Accepted Solutions
Mike2500
Super User
Super User

Either of the following can be used to get the max date plus one day:

 

DateAdd(Date(1970,1,1) + Max(datasource, datefield)/(60*60*24*1000),1,Days)

 

DateAdd(First(Sort(datasource,datefield,Descending)).datefield,1,Days)

 

Max isn't delegable, so perhaps stick with the second. Of course, either triggers a query to the database, so perhaps look into using collections, and then using the collection as the datasource.

 

The first uses the max function, which doesn't return a date, but instead returns the value as the number of milliseconds since 1970, and then calculates the actual date value, and adds one day. The second retrieves the first row based on sorting the table by date, descending, and then adds one day. You can set either of the above as the default for the date picker.

 

 

View solution in original post

WarrenBelz
Super User
Super User

Hi @rvdhorst ,

You could also do this and be completely Delegable (close to option #2 from @Mike2500)

With(
  {
      wDates:
      Sort(
         YourTableName,
         YourDateField,
         Descending
      )
   },
   DateAdd(
      First(wDates).YourDateField,
      1,
      Days
   )
)

 

Please click Accept as solution 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 Thumbs Up.

Visit my blog Practical Power Apps

View solution in original post

@rho1967 ,

To skip to the next weekday

With(
  {
      wDates:
      Sort(
         YourTableName,
         YourDateField,
         Descending
      )
   },
   DateAdd(
      First(wDates).YourDateField,
      Switch(
         Weekday(
            First(wDates).YourDateField
         ),
         6,
         3,
         7,
         2,
         1
      ),
      Days
   )
)

 

Please click Accept as solution 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 Thumbs Up.

Visit my blog Practical Power Apps

View solution in original post

@rho1967 ,

If Saturday is a Work day

With(
  {
      wDates:
      Sort(
         YourTableName,
         YourDateField,
         Descending
      )
   },
   With(
      {wDay:First(wDates).YourDateField},
      DateAdd(
         wDay,
         If(
            Weekday(wDay) = 7,
            2,
            1
         ),
         Days
      )
   )
)

 

Please click Accept as solution 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 Thumbs Up.

Visit my blog Practical Power Apps

View solution in original post

@rho1967 ,

Try small d in Weekday (corrected in post)

View solution in original post

8 REPLIES 8
Mike2500
Super User
Super User

Either of the following can be used to get the max date plus one day:

 

DateAdd(Date(1970,1,1) + Max(datasource, datefield)/(60*60*24*1000),1,Days)

 

DateAdd(First(Sort(datasource,datefield,Descending)).datefield,1,Days)

 

Max isn't delegable, so perhaps stick with the second. Of course, either triggers a query to the database, so perhaps look into using collections, and then using the collection as the datasource.

 

The first uses the max function, which doesn't return a date, but instead returns the value as the number of milliseconds since 1970, and then calculates the actual date value, and adds one day. The second retrieves the first row based on sorting the table by date, descending, and then adds one day. You can set either of the above as the default for the date picker.

 

 

WarrenBelz
Super User
Super User

Hi @rvdhorst ,

You could also do this and be completely Delegable (close to option #2 from @Mike2500)

With(
  {
      wDates:
      Sort(
         YourTableName,
         YourDateField,
         Descending
      )
   },
   DateAdd(
      First(wDates).YourDateField,
      1,
      Days
   )
)

 

Please click Accept as solution 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 Thumbs Up.

Visit my blog Practical Power Apps

Thanks for the nice solutions.

If the day+1 is a Sunday, how can I skip it to Monday (so +2)

@rho1967 ,

To skip to the next weekday

With(
  {
      wDates:
      Sort(
         YourTableName,
         YourDateField,
         Descending
      )
   },
   DateAdd(
      First(wDates).YourDateField,
      Switch(
         Weekday(
            First(wDates).YourDateField
         ),
         6,
         3,
         7,
         2,
         1
      ),
      Days
   )
)

 

Please click Accept as solution 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 Thumbs Up.

Visit my blog Practical Power Apps

Sorry for the confusion. Below an example

 

Monday+1

Tuesday+1

Wednesday+1

Thursday+1

Friday+1

Saturday+2

 

How can I make this jump? I was hoping to do a check on wDates.

@rho1967 ,

If Saturday is a Work day

With(
  {
      wDates:
      Sort(
         YourTableName,
         YourDateField,
         Descending
      )
   },
   With(
      {wDay:First(wDates).YourDateField},
      DateAdd(
         wDay,
         If(
            Weekday(wDay) = 7,
            2,
            1
         ),
         Days
      )
   )
)

 

Please click Accept as solution 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 Thumbs Up.

Visit my blog Practical Power Apps

hello warren,

 

Sorry, but I'm getting an error. See Appendix.

@rho1967 ,

Try small d in Weekday (corrected in post)

Helpful resources

Announcements
PA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.

Microsoft Build 768x460.png

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

May UG Leader Call Carousel 768x460.png

What difference can a User Group make for you?

At the monthly call, connect with other leaders and find out how community makes your experience even better.

Top Solution Authors
Top Kudoed Authors
Users online (1,845)