cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Anonymous
Not applicable

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 @Anonymous ,

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
WarrenBelz
Super User
Super User

Hi @Anonymous ,

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.

 

RameshMukka
Continued Contributor
Continued Contributor

@Anonymous 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)
Anonymous
Not applicable

@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 

RameshMukka
Continued Contributor
Continued Contributor

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

@Anonymous 

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 @Anonymous ,

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.

Drrickryp
Super User
Super User

Hi @Anonymous 

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

Anonymous
Not applicable

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  

Anonymous
Not applicable

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
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Top Solution Authors
Top Kudoed Authors
Users online (1,951)