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 / Issues with patching a...
Power Apps
Answered

Issues with patching a sequence of records between two dates.

(1) ShareShare
ReportReport
Posted on by 7
I have been wracking my head trying to figure out the logic behind patching a record for each day between two dates. Essentially I'm trying to get this to automatically create a new record for each day starting with a StartDate and creating the last record for the EndDate. It checks to see if there is already a record created for that date, with the selected department. If it is found, it will update the record if its not found it will create the record.
 
When I do the simple Patch everything works, so I can reasonably assume that my issue is with the Date Range / Sequence. I just for the life of me can not figure out the logic in Powerapps to pull this off.
 
What I currently have is below. Thank you in advance if anyone has any pointers!
 
With(
    {
        startDate: StartDate.SelectedDate,
        endDate: EndDate.SelectedDate
    },
    ClearCollect(
        dateRange,
        AddColumns(
            Sequence(If(IsBlank(EndDate.SelectedDate), 1, DateDiff(startDate, endDate, TimeUnit.Days) + 1, 0, 1)),'DateCol',DateAdd(startDate, Value, TimeUnit.Days)
        )
    )
);
ForAll(
    dateRange,
//Working Below
    If(
        IsBlank(LookUp(Sched_Tbls, Sched_Date = sDate_var && Sched_Dept = DepartmentSelect.Selected.Dept_Name)),
        Patch(
            Sched_Tbls,
            Defaults(Sched_Tbls),
            {Sched_Dept_Lookup: DepartmentSelect.Selected},
            {Sched_Dept: DepartmentSelect.Selected.Dept_Name},
            {Sched_S_Date: StartDate.SelectedDate},
            {Sched_E_Date: EndDate.SelectedDate},
            {Sched_Date: DateCol},
            {Sched_P_Contact_Lookup: PrimeContactLookup.Selected},
            {Sched_S_Contact_Lookup: SecondContactLookup.Selected},
            {Sched_Notes: SchedNotes.Value}
        ),
        Patch(
            Sched_Tbls,
            LookUp(Sched_Tbls, Sched_Date = sDate_var && Sched_Dept = DepartmentSelect.Selected.Dept_Name),
            {Sched_Dept_Lookup: DepartmentSelect.Selected},
            {Sched_Dept: DepartmentSelect.Selected.Dept_Name},
            {Sched_S_Date: StartDate.SelectedDate},
            {Sched_E_Date: EndDate.SelectedDate},
            {Sched_Date: DateCol},
            {Sched_P_Contact_Lookup: PrimeContactLookup.Selected},
            {Sched_S_Contact_Lookup: SecondContactLookup.Selected},
            {Sched_Notes: SchedNotes.Value}
        )
    )
)
 
Categories:
I have the same question (0)
  • Verified answer
    Power Platform 1919 Profile Picture
    1,890 on at
    HI @,
    based on provided powerfx formula, i can see few syntax issues, 
    org: 
    With(
        {
            startDate: StartDate.SelectedDate,
            endDate: EndDate.SelectedDate
        },
        ClearCollect(
            dateRange,
            AddColumns(
                Sequence(If(IsBlank(EndDate.SelectedDate), 1, DateDiff(startDate, endDate, TimeUnit.Days) + 1, 0, 1)),'DateCol',DateAdd(startDate, Value, TimeUnit.Days)
            )
        )
    );
    Corrected:
    With(
      { startDate: StartDate.SelectedDate, endDate: EndDate.SelectedDate },
      ClearCollect(
        dateRange,
        AddColumns(
          Sequence(
            If(
              IsBlank(endDate),
              1,
              DateDiff(startDate, endDate, Days) + 1
            ),      // ← only the count
            0,      // ← sequence starts at zero
            1       // ← steps of one day
          ),
          "DateCol", // u can keep 'DateCol' or "DateCol"
          DateAdd(startDate, Value, Days)
        )
      )
    );
    and in the second part can you please provide where is this sDate_var coming from ? my assumption is "DateCol" from previous step.
    I have updated the formula to reduce the repetitive steps (note: code not tested):
    With(
        {
            startDate: StartDate.SelectedDate,
            endDate: EndDate.SelectedDate
        },
        With(
            {
                dateRange: AddColumns(Sequence(If(IsBlank(endDate),1,DateDiff(startDate,endDate,TimeUnit.Days) + 1),0,1),'DateCol',DateAdd(startDate,Value,TimeUnit.Days))
            },
            ForAll(
                dateRange As Dr,
                Patch(
                    Sched_Tbls,
                    Coalesce(
                        LookUp(
                            Sched_Tbls,
                            Sched_Date = Dr.DateCol && Sched_Dept = DepartmentSelect.Selected.Dept_Name
                        ),
                        Defaults(Sched_Tbls)
                    ),
                    {
                        Sched_Dept_Lookup: DepartmentSelect.Selected,
                        Sched_Dept: DepartmentSelect.Selected.Dept_Name,
                        Sched_S_Date: StartDate.SelectedDate,
                        Sched_E_Date: EndDate.SelectedDate,
                        Sched_Date: Dr.DateCol,
                        Sched_P_Contact_Lookup: PrimeContactLookup.Selected,
                        Sched_S_Contact_Lookup:  SecondContactLookup.Selected,
                        Sched_Notes: SchedNotes.Value
                    }
                )
            )
        )
    );
    

    Please give the mentioned solution a try and let me know whether it works for you.
    If it solves your issue, feel free to mark this thread as answered.
    Please make sure to like the response!
    Thanks!
    - Hemanth Sai

  • lbendlin Profile Picture
    8,479 Super User 2025 Season 2 on at
    Please check if this documentation can help you with the bulk patching
     
  • AJ-17072235-0 Profile Picture
    7 on at
    Thank you so much @Power Apps 1919 !
     
    Your explanation was very helpful in understanding how to properly use Sequencing and Patching. I did over complicate things while trying to get it to work. The variable  sDate_var was defined on the Sched_S_Date date picker OnChange I was trying to set the current date to be used later in the script, but your way is MUCH clearer, cleaner, and streamlined. Everything worked exactly as its supposed to! 
     
    Thank you for taking the time to explain step by step how all this should work! You are a Life Saver!

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

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 320 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard