cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
sasrsc
Responsive Resident
Responsive Resident

UpdateContext one value behind?

I am building a Gantt chart from scratch and receiving 2 date picker dates as inputs.
I initially set the default dates in the screen.onVisible property.
All good... critically to note here I also set a DateDiff value in the range of the dates.

UpdateContext(
    {
        DatePickerStart: Now(),
        DatePickerEnd: Date(
            Year(Now()),
            12,
            31
        ),
        DayRange: DateDiff(
            DatePickerStart,
            DatePickerEnd,
            Days
        )})

When the screen displays I have the correct dates displayed and range displayed.
In each datepicker onChange property I do this...(this is where I change the "DateStart" datepicker (there's a DateStart and a DateEnd" picker)

UpdateContext({
DatePickerStart:DateStart.SelectedDate,
DayRange: DateDiff(DatePickerStart,DatePickerEnd,Days)
})

I add a label to the screen to show the DatePickerStart, DatePickerEnd and the DateRange.
I then change the DateStart datepicker and the range doesn't change.
I do it again and it reflects the value of what it should have been on the previous value.
I do it again and it's always one value behind.
So in the onChange value it's performing and setting the DatePickerStart value properly (I know this because of the labels).
But in the formula above I'm using the DatePickerStart value for the DateDiff calculation. So when it performs the DateDiff it's not registering the brand new value that was set on the line before....
I can guess why. I can also solve it by doing ...it as 2 UpdateContext lines or by referencing the DateStart.SelectedDate in the DateDiff function instead of the DatePickerStart function.
In my simple mind, I just figured that if in line 1 it sets the DatePickerStart value to match that in the actual DatePicker therefore if I reference DatePickerStart on line 2 then it would be OK... but it isn't.
Can someone explain this in lay person's terms to me... 
It may help others too... thanks.


 
 

1 ACCEPTED SOLUTION

Accepted Solutions
iAm_ManCat
Super User
Super User

If you are updating multiple context values, it will try do them concurrently to be more efficient, so they will be evaluated at the same time, not one after the other 🙂

 

So yes, separate them into two separate updateContext if you want the one to rely on the updated result of the other 🙂

@iAm_ManCat


Please 'Mark as Solution' if someone's post answered your question and always 'Thumbs Up' the posts you like or that helped you!


Thanks!
You and everyone else in the community make it the awesome and welcoming place it is, keep your questions coming and make sure to 'like' anything that makes you 'Appy
Sancho Harker, MVP


View solution in original post

1 REPLY 1
iAm_ManCat
Super User
Super User

If you are updating multiple context values, it will try do them concurrently to be more efficient, so they will be evaluated at the same time, not one after the other 🙂

 

So yes, separate them into two separate updateContext if you want the one to rely on the updated result of the other 🙂

@iAm_ManCat


Please 'Mark as Solution' if someone's post answered your question and always 'Thumbs Up' the posts you like or that helped you!


Thanks!
You and everyone else in the community make it the awesome and welcoming place it is, keep your questions coming and make sure to 'like' anything that makes you 'Appy
Sancho Harker, MVP


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 (2,993)