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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Formulas - how much ca...
Power Apps
Unanswered

Formulas - how much can I do here...

(0) ShareShare
ReportReport
Posted on by 678
I have a modern date picker and for the onChange property I want to do numerous things....

I also have some buttons that want to set the datepicker selected date to the next / prev week and also a page on visible function around this, so that is 4 places where I would rather not repeat my code. But there's a variable that changes each time... I may set _dateSelected to Today() or use a DateAdd function...so that needs to allow for changes.

My current code is this.
 
Set(_dateSelected, DatePickerCanvas6.SelectedDate);
Set(_weekNo,ISOWeekNum(_dateSelected));
 
Set(_startOfWeek,
     
      DateAdd(_dateSelected,-(Weekday(_dateSelected,StartOfWeek.MondayZero)),TimeUnit.Days)
 );
 
 Set(_endOfWeek,
     
      DateAdd(_dateSelected,6-(Weekday(_dateSelected,StartOfWeek.MondayZero)),TimeUnit.Days)
 );
 ClearCollect(
    WeeklyCalendar,
    {
        Day: "Mon", Date: DateAdd(_dateSelected, -(Weekday(_dateSelected, StartOfWeek.MondayZero)), TimeUnit.Days)
    },
    {
        Day: "Tue", Date: DateAdd(_dateSelected, 1 - Weekday(_dateSelected, StartOfWeek.MondayZero), TimeUnit.Days)
    },
    {
        Day: "Wed", Date: DateAdd(_dateSelected, 2 - Weekday(_dateSelected, StartOfWeek.MondayZero), TimeUnit.Days)
    },
    {
        Day: "Thu", Date: DateAdd(_dateSelected, 3 - Weekday(_dateSelected, StartOfWeek.MondayZero), TimeUnit.Days)
    },
    {
        Day: "Fri", Date: DateAdd(_dateSelected, 4 - Weekday(_dateSelected, StartOfWeek.MondayZero), TimeUnit.Days)
    },
    {
        Day: "Sat", Date: DateAdd(_dateSelected, 5 - Weekday(_dateSelected, StartOfWeek.MondayZero), TimeUnit.Days)
    },
    {
        Day: "Sun", Date: DateAdd(_dateSelected, 6 - Weekday(_dateSelected, StartOfWeek.MondayZero), TimeUnit.Days)
    }
)

My question is ...
a) how much of this can I put into a app.formula instead so I don't repeat myself, and
b) must I break it up into smaller chunks.

I'm sorry but I don't quite "get" formulas yet.


 
Categories:
I have the same question (0)
  • Suggested answer
    abc 123 Profile Picture
    784 Moderator on at
    I've just learned about App.Formula, and my one attempt was too complex for it, so I gave up. 
     
    But your example looks pretty straightforward, to where you would have the first line in the OnChange (and perhaps the other places you mentioned),  then the rest of the code would be in the App.Formula.
  • Suggested answer
    timl Profile Picture
    36,325 Super User 2025 Season 2 on at
    Hi sasrsc
     
    I would keep _dateSelected as a variable. The other parts could then be rewritten as named formulas like so.
     
    WeekNo = ISOWeekNum(_dateSelected);
    StartOfWeek = DateAdd(_dateSelected, - (Weekday(_dateSelected, StartOfWeek.MondayZero)), TimeUnit.Days);
    EndOfWeek = DateAdd(_dateSelected, 6 - (Weekday(_dateSelected, StartOfWeek.MondayZero)), TimeUnit.Days);
    
    WeeklyCalendar = Table(
        {
            Day: "Mon", Date: DateAdd(_dateSelected, -(Weekday(_dateSelected, StartOfWeek.MondayZero)), TimeUnit.Days)
        },
        {
            Day: "Tue", Date: DateAdd(_dateSelected, 1 - Weekday(_dateSelected, StartOfWeek.MondayZero), TimeUnit.Days)
        },
        {
            Day: "Wed", Date: DateAdd(_dateSelected, 2 - Weekday(_dateSelected, StartOfWeek.MondayZero), TimeUnit.Days)
        },
        {
            Day: "Thu", Date: DateAdd(_dateSelected, 3 - Weekday(_dateSelected, StartOfWeek.MondayZero), TimeUnit.Days)
        },
        {
            Day: "Fri", Date: DateAdd(_dateSelected, 4 - Weekday(_dateSelected, StartOfWeek.MondayZero), TimeUnit.Days)
        },
        {
            Day: "Sat", Date: DateAdd(_dateSelected, 5 - Weekday(_dateSelected, StartOfWeek.MondayZero), TimeUnit.Days)
        },
        {
            Day: "Sun", Date: DateAdd(_dateSelected, 6 - Weekday(_dateSelected, StartOfWeek.MondayZero), TimeUnit.Days)
        }
    );
     
  • Verified answer
    WarrenBelz Profile Picture
    152,859 Most Valuable Professional on at
    Another option using With() - also cuts down variables and some duplicated code
    With(
       {
          _dateSelected: DatePickerCanvas6.SelectedDate,
          _weekNo: ISOWeekNum(DatePickerCanvas6.SelectedDate),
          _day: Weekday(DatePickerCanvas6.SelectedDate, StartOfWeek.MondayZero)
       },
       With(
          {
             _startOfWeek: DateAdd(_dateSelected, -_day, TimeUnit.Days),
             _endOfWeek: DateAdd(_dateSelected, 6 - _day, TimeUnit.Days)
          },
          ClearCollect(
             WeeklyCalendar,
             {
                Day: "Mon",
                Date: DateAdd(_dateSelected, -_day, TimeUnit.Days)
             },
             {
                Day: "Tue",
                Date: DateAdd(_dateSelected, 1 - _day, TimeUnit.Days)
             }, 
             {
                Day: "Wed",
                Date: DateAdd(_dateSelected, 2 - _day, TimeUnit.Days)
             },
             {
                Day: "Thu",
                Date: DateAdd(_dateSelected, 3 - _day, TimeUnit.Days)
             },
             {
                Day: "Fri",
                Date: DateAdd(_dateSelected, 4 - _day, TimeUnit.Days)
             },
             {
                Day: "Sat",
                Date: DateAdd(_dateSelected, 5 - _day, TimeUnit.Days)
             },
             {
                Day: "Sun",
                Date: DateAdd(_dateSelected, 6 - _day, TimeUnit.Days)
             }
          )
       )
    )
     
    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    Buy me a coffee
  • sasrsc Profile Picture
    678 on at
    Thank you all. All helped... and yes it works better than it did.

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

Forum hierarchy changes are complete!

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 759 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 310 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 228

Last 30 days Overall leaderboard