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

How to Calculate DateDiff On Button click.

Hello Experts,

 

I have two Field"Activity start date" and "Activity End Date", i want to calculate and store the difference on button click. so i am writing below formula on button On select 

 

UpdateContext({WDvar:DateDiff(ThisItem.'Activity End Date',ThisItem.'Activity Start Date',Hours)})

 

 But it is giving error , please advise what should i correct in formula.

1 ACCEPTED SOLUTION

Accepted Solutions

Hi @Sachin31 ,

I see the issue now - you will not get a value with that formula as the data has not yet been written to the source - you need to refer to the controls in the Activity Start and End date. You have not given me the names of these, but you will get the idea from the below for the Default of DataCardValue5

DateDiff(
   dpStartDate.SelectedDate + Time(
        Value(ddStartHour.Selected.Value),
        Value(ddStartMinute.Selected.Value),
        0
    ),
    dpEndDate.SelectedDate + Time(
        Value(ddEndHour.Selected.Value),
        Value(ddEndMinute.Selected.Value),
        0
    ),
    Seconds
)

Working example below

DateTimeDiff.png

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

17 REPLIES 17
Super User III
Super User III

Hi @Sachin31 ,

That should work as long as the button is inside the form or gallery - you can use a custom card on the form. If not in the form, ThisItem will not work.

UpdateContext(
   {
      WDvar:
      DateDiff(
         ThisItem.'Activity End Date',
         ThisItem.'Activity Start Date',
         Hours
      )
   }
)

I also assume that the fields are date/time fields.

 

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.

 

Post Patron
Post Patron

@Sachin31 Where is your button? Is it on gallery? If Ok, you can also add a label on gallery write your formula there.

DateDiff(ThisItem.'Activity End Date',ThisItem.'Activity Start Date',Hours)
Helper I
Helper I

@WarrenBelz @RameshMukka , Thank you for the response i have added a simple text field "Totalworkdone" in my SP list and that field i am fetching in App which is appearing as Data card. now at the time of form submission i want work done to recorded as per the DateDiff Formula.

So where should i write this formula ? onselect of the Icon/Button or  Data card of field or anywhere else ? Button and icon are outside the form , i dont wan to add extra button in form for workdone only 

Please advise 

@Sachin31 You can write the formula on default property of your newly added data card.

@Sachin31 

If you are already using a Variable, put the Default of the Text Box to

If(
   WDvar > 0
   WDvar,
   Parent.Default
)

then put the formula on the OnChange of your two date/time controls. Lastly, put this on the OnVisible of the screen

UpdateContext({WDVar:0})

 

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.

Hi @Sachin31 ,

Just checking if you got the result you were looking for on this thread. Happy to help further if not.

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.

Super User II
Super User II

Hi @Sachin31 

A simple question: Why bother saving a calculated result when the elements for doing the calculation are already saved in the database?

Hello @WarrenBelz , Thank you so much for the concern , unfortunately issue not yet solved. I am not using variable now as on form submissions i  just want to calculate and submit the result to SP list.

As suggested by Ramesh i am writing   below formula on default property of  datacardvalue field but on SP list its not saving any result after form submission. Please advise if i need to do anything else 

DateDiff(ThisItem.'Activity End Date',ThisItem.'Activity Start Date',Seconds)

Please noted  my ActivityStartDate and ActivityendDate field display date on start and end button click respectively  

Hello @Drrickryp , I am not sure if i got your question right. Actually i want to calculate Total workdone by the user before i was trying to achieve this on SP list side by putting the formula on the respected column but it was not working. so i thought of calculating this on App side and store the result in SP list. 

Please let me know if you were asking something else  

Helpful resources

Announcements
secondImage

Experience what’s next for Power Apps

Join us for an in-depth look at the new Power Apps features and capabilities at the free Microsoft Business Applications Launch Event.

Power Apps Community Call

Power Apps Community Call: February

Did you miss the call? Check out the Power Apps Community Call here.

New Power Super Users

Congratulations!

We are excited to announce the Power Apps Super Users!

New Badges

New Solution Badges!

Check out our new profile badges recognizing authored solutions!

Top Solution Authors
Top Kudoed Authors
Users online (88,666)