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

Move task up and down

I am developing a repeating table in PowerApps that contains a list of tasks (see image).
These tasks have a start date, number of days, and an end date. Creating the task table was not difficult, but, I have a requirement to move tasks up and down and I can't do that. because I need to recalculate date again in the new order given 


image.png

2 ACCEPTED SOLUTIONS

Accepted Solutions

@Ramonjairo31 ,

That should have been your first post - it has taken 20 to get there. You have to help us help you.

You also need to understand your own code

SortByColumns(
   CollectionTask,
   "TaskOrder"
)

is doing exactly what you have asked it to do - sort by Task Order, so when the task order changes, so do the items. If you want the gallery sorted by a date (as I described) , then sort by the date column

SortByColumns(
   CollectionTask,
   "Start Date"
)

 

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.

View solution in original post

Ramonjairo31
Helper II
Helper II

This is the solution

 

Button Up

UpdateContext({varChangeItemONESortNumber: ThisItem.TaskOrder});
UpdateContext({varChangeItemTWOSortNumber: ThisItem.TaskOrder - 1});
Patch(
    CollectionTask,
    ThisItem,
    {TaskOrder: varChangeItemTWOSortNumber}
);
Patch(
    CollectionTask,
    First(
        Filter(
            CollectionTask,
            TaskOrder = varChangeItemTWOSortNumber
        )
    ),
    {TaskOrder: varChangeItemONESortNumber}
)

 

Button Down

UpdateContext({varChangeItemONESortNumber: ThisItem.TaskOrder});
UpdateContext({varChangeItemTWOSortNumber: ThisItem.TaskOrder + 1});
Patch(
    CollectionTask,
    ThisItem,
    {TaskOrder: varChangeItemTWOSortNumber}
);
Patch(
    CollectionTask,
    First(
        Filter(
            CollectionTask,
            TaskOrder = varChangeItemTWOSortNumber
        )
    ),
    {TaskOrder: varChangeItemONESortNumber}
)

on the item property in the gallery 

SortByColumns(CollectionTask,"TaskOrder")

View solution in original post

23 REPLIES 23
WarrenBelz
Super User
Super User

Hi @Ramonjairo31 ,

You can sort a gallery in date order - what do you mean by "recalculate"?

v-siky-msft
Community Support
Community Support

Hi @Ramonjairo31 ,

Could you please share more details about your last sentence, 'recalculate date again '?

You can not adjust the row order arbitrarily, the gallery can only be sorted on columns in Ascending or Descending order.

If you want to move some tasks up or down, the only way is to modify the index column to meet the sort order requirement.

Hope this helps.

Sik

@v-siky-msft,@WarrenBelz 

 

The task works like this “recalculate”

 

I’m have a overall start date, input that into the start date of the first line and then add the number of days to get the end date and then use this end date as the start date for the next line and so on, this is working fine but now I need to move task up and down and do this math on the new orden given 

@Ramonjairo31 ,

Can you please share the code you are using to "recalculate", in particular how are you identifying what is the "next row"

I'm using this formula to get the new day, however, this formula just works line by line 
Basically the variable "VarCountDate" takes the last day in the variable and recalculate the new date for the next line
 
Set(
VarCountDate,
DateAdd(
DatePickerStartDate_1.SelectedDate,
Value(TextInputCount_1.Text) + RoundDown(
Value(TextInputCount_1.Text) / 5,
0
) * 2 + Switch(
Weekday(DatePickerStartDate_1.SelectedDate),
5,
If(
Mod(
Value(TextInputCount_1.Text),
5
) > 0,
2,
0
),
4,
If(
Mod(
Value(TextInputCount_1.Text),
5
) > 1,
2,
0
),
3,
If(
Mod(
Value(TextInputCount_1.Text),
5
) > 2,
2,
0
),
2,
If(
Mod(
Value(TextInputCount_1.Text),
5
) > 3,
2,
0
),
1,
If(
Mod(
Value(TextInputCount_1.Text),
5
) > 4,
2,
0
)
)
)
);

Thanks @Ramonjairo31 ,

I looked at your code and getting my mind around it (sort of) come up with this

With(
   {
      vAdd:
      Mod(
         Value(TextInputCount_1.Text),
         5
      ),
      vBase:
      Value(TextInputCount_1.Text) + 
      RoundDown(
         Value(TextInputCount_1.Text) / 5,
         0
      ) * 2,
      vDay:
      Weekday(DatePickerStartDate_1.SelectedDate)
   },
   Set(
      VarCountDate,
      DateAdd(
         DatePickerStartDate_1.SelectedDate,
         vBase + 
         If(
            vAdd>(5-vDay), 
            vAdd,
            0
         )
      )
   )
)

So now you come up with varDateCount, how do you use it to create a new record?

 

Thank you very much @WarrenBelz 

 

Basically, I'm using this in the save button for a new lines

Spoiler
If(
VarEditPressed,
false,
Collect(
CollectionTask,
{
TaskOrder: Value(Last(CollectionTask).TaskOrder + 1),
TaskLine: "",
Status: "",
StartDate: Text(VarCountDate),
Days: "",
EndDate: "",
AssignedTo: "",
ChargeAccount: "",
ShowButtons: true
}
)
);

then new lines come up with the last day 

 

as attachment, I put a copy of the app I have so far using your code but now will be great to move a task up and down a recalculate  

@Ramonjairo31 ,

The code I gave was just a much shorter way of achieving the same results - I hope it had helped you understand a bit more about other options.

I cannot open the app (I am getting "cannot open in this version" error), however in the below code

If(
   VarEditPressed,
   false,
   Collect(
      CollectionTask,
      {
         TaskOrder: Value(Last(CollectionTask).TaskOrder + 1),
         TaskLine: "",
         Status: "",
         StartDate: Text(VarCountDate),
         Days: "",
         EndDate: "",
         AssignedTo: "",
         ChargeAccount: "",
         ShowButtons: true
      }
   )
)

 why are you using StartDate: Text(VarCountDate), into a Date field? Also if the Items of the gallery are sorted by date, they should automatically adjust when the new item is added . What is the Items property of this gallery?

thanks Mr @WarrenBelz  you code was really useful also taught me about the function "WITH" I will use it in my apps.

 

I'm using text  under StartDate because that field inside the collection is text type, however, when I patch all this into SharePoint I do the conversion into date no problem so far with it, but I'm an open book

 

Let's supposed that a user created a few tasks and saved all of them into the database so far everything is fine but then he's back to the system and decided to move the Task that was in order number 1 to be in order number 3 and the task that was on order number 3 to be on order number 1, now the problem comes

 

It´s the following: Task should be organized in this way again:  overall start date, input that into the start date of the first line and then add the number of days to get the end date and then use this end date as the start date for the next line and so on,

 

so basically, I'm doing this math when the user is adding a new task in the system "on Button Save task Line"

 

So if the user changes the order on any tasks the math that was made before, won't work anymore.

I want to create a button outside the gallery which is able to do this math not matter the order given

 

Explanation.png

Helpful resources

Announcements
Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Top Solution Authors
Top Kudoed Authors
Users online (4,122)