cancel
Showing results for 
Search instead for 
Did you mean: 
Mdorrani

Flow - Calculate Date Diference

Definition :- ticks(timestamp: string) - Returns the number of ticks (100 nanoseconds interval) since 1 Jan 1601  00:00:00 UT

 

Scenario :-

 

SharePoint list contains following columns:-

 

Title (Single Line of Text)

Contract Executed Date (Date)

 

Capture.PNG

 

 MS Flow :-

 

Get items from list

 

Capture.PNG

 

 

 

 

 

 

 

 

 

 

 

Run Select data operation to calculate number of days between Today's date and the Contract Executed Date

 

 

Capture.PNG

 

 

 

 

 

 

 

 

 

The expression for Days Since Contract Executed :-

div(sub(ticks(formatDateTime(utcNow(),'yyyy-MM-dd')),ticks(item()?['ContractExecutedDate'])),864000000000)
 
ticks(formatDateTime(utcNow(),'yyyy-MM-dd') - Number of Ticks since 1 Jan 1601 and Today's Date
ticks(item()?['ContractExecutedDate']) - Number of Ticks since 1 Jan 1601 and Contract Executed date
sub - subtracting the number of ticks
div - divide the result from 864000000000 (conversion from nanoseconds to days)
 
Output of Select when Flow runs :-
Capture.PNG
 
 
Another use case would be to do date comparisons using Ticks
 
Filter all contracts which have Contract executed date in the last week
 
Capture.PNG
 
ticks(item()?['ContractExecutedDate']) - Number of Ticks since 1 Jan 1601 and Contract Executed date
ticks(formatDateTime(adddays( utcNow(),-7),'yyyy-MM-dd')) - Number of Ticks since 1 Jan 1601 and [Today's Date - 7 ] (in the last week)
 
utcNow() - current date and time
adddays( utcNow(),-7) - adds (-7) days to todays date 
formatDateTime(adddays( utcNow(),-7),'yyyy-MM-dd') - format the date time to date 'yyyy-MM-dd'
 
Check if ticks - Contract Executed date is greater than the ticks  - since Last week
 
Data in Contracts List :-
 
 
Capture.PNG

Flow - Filter using ticks expression
Capture.PNG
 
Flow - Select the filtered operation result and get Tile of contract
 
 
Capture.PNG
 
Result of Flow run for Select -
 
Capture.PNG
 
Only "Contract 2" is returned as it has Contract Executed Date since last week
 
Thanks for reading !!!
Comments
Anonymous

Hi Mdorrani,

 

Maybe you can help me with this:

https://powerusers.microsoft.com/t5/Building-Flows/Copy-item-from-sharepoint-list-if-item-is-created...

 

I build the "Select" part of the flow based on this article, but i can't seem to use the output it gives me?

 

 

 

 

For me, worked this way:

 

add(mul(div(Sub(ticks(utcNow()),ticks(item()?['Estimated_x0020_Delivery_x0020_D'])),864000000000),-1),1)

 

 

 

Hi Guys,

 

I hope this helps, this is how I was able to find a solution concerning this. I defined a variable which checks how many minutes have elapsed since the record was last updated if the value is greater than 1 minute (or 5, depending on what you want), then the flow is allowed to proceed (the part that updates an item), this will stop the loop.

 

div(sub(ticks(utcNow()),ticks(triggerBody()?['Modified'])),600000000)

 

Changes made.PNGchanges2.PNG

@Mdorrani 

 

Great post, I wonder if you could help with this issue. Your way seems to have less steps.

 

https://powerusers.microsoft.com/t5/Building-Flows/Build-a-Flow-based-on-time-passed-and-Choice-Fiel...

This solution is working great for me however does it take leap years into account?  When I've tried this some of the calculations are 1 day off and it appears the ones with a day missing are where there is a leap year between today and the end date?  I'm doing the sum the other way round from the example so End Date - Today.  Thanks

a DateDiff function would be so much easier..

DateDiff is available in Power Apps, which means it should be in PowerFX, so hopefully just a matter of time to get it into Power Automate.

 

That said, for a scenario where  you are using the SharePoint -> Get Items, and you need to filter on something past due (lets say 2 or more days past due) based on a date field in the SharePoint list, this ODATA filter worked for me:

 

myDueDateColumn le '<the expression below>'

 

Expression: formatDateTime(addDays(utcNow(),-2),'yyyy-MM-dd')

 

 

Writing the expressions is a science.  Is there an expression helping tool.  We have just spent hours try to debug this 

div(div(mul(sub(ticks(item()?['responseDate'])!ticks(item()['requestDate'])/(100,100000000],60)

@Mdorrani By the way I love your videos and am trying to keep up!  

@AZ-MOSS-Bear 

 

The best way I have debugged things like that is to make a series of compose statements where each compose uses the output from the previous and does one action of the formula. So the first compose would have the ticks expression, then, the second would have the sub expression, then mul....etc. 

Thank You

I have made a post that thoroughly shows the use of the expression dateDifference() as well as how to use the ticks() expression to find the difference between two datetimes.

https://powerusers.microsoft.com/t5/General-Power-Automate/Using-the-dateDifference-expression-or-ti... 

I am trying to get the time difference between the start and wait for approval's request date and response date. The expression listed above appears to be different than what is in @Mdorrani  YouTube video "SharePoint Approval Timeline..." I tried importing the github flow, but it errors out. Any help would be appreciated.

@AnnetteM It looks like the difference in the video you watched is that the expression he used did an extra conversion. I think he was like using an expression to get hours and then used the same one to get minutes but just added an additional divide by 60. I'm not exactly sure, but the examples above should get the result you want.

 

You can look at my post here that shows about time calculation.

 

I would recommend you go into the Get Help With Power Automate - Building Flows and make a new post asking how to get the time difference in approvals. Title your post something like "Help to Get Time Difference for Approval Request and Response" or similar.

 

Write your question, give a couple of screenshots of your flow, and I can almost guarantee someone will answer your exact question shortly. It might be difficult for you to get flow specific help here on an older blog post comments.

Thanks! I finally figured it out!

div(div(mul(sub(ticks(item()?['responseDate']),ticks(item()?['requestDate'])),100),1000000000),60)

About the Author
  • Experienced Consultant with a demonstrated history of working in the information technology and services industry. Skilled in Office 365, Azure, SharePoint Online, PowerShell, Nintex, K2, SharePoint Designer workflow automation, PowerApps, Microsoft Flow, PowerShell, Active Directory, Operating Systems, Networking, and JavaScript. Strong consulting professional with a Bachelor of Engineering (B.E.) focused in Information Technology from Mumbai University.
  • I am a Microsoft Business Applications MVP and a Senior Manager at EY. I am a technology enthusiast and problem solver. I work/speak/blog/Vlog on Microsoft technology, including Office 365, Power Apps, Power Automate, SharePoint, and Teams Etc. I am helping global clients on Power Platform adoption and empowering them with Power Platform possibilities, capabilities, and easiness. I am a leader of the Houston Power Platform User Group and Power Automate community superuser. I love traveling , exploring new places, and meeting people from different cultures.
  • Read more about me and my achievements at: https://ganeshsanapblogs.wordpress.com/about MCT | SharePoint, Microsoft 365 and Power Platform Consultant | Contributor on SharePoint StackExchange, MSFT Techcommunity
  • Encodian Owner / Founder - Ex Microsoft Consulting Services - Architect / Developer - 20 years in SharePoint - PowerPlatform Fan
  • Founder of SKILLFUL SARDINE, a company focused on productivity and the Power Platform. You can find me on LinkedIn: https://linkedin.com/in/manueltgomes and twitter http://twitter.com/manueltgomes. I also write at https://www.manueltgomes.com, so if you want some Power Automate, SharePoint or Power Apps content I'm your guy 🙂
  • I am the Owner/Principal Architect at Don't Pa..Panic Consulting. I've been working in the information technology industry for over 30 years, and have played key roles in several enterprise SharePoint architectural design review, Intranet deployment, application development, and migration projects. I've been a Microsoft Most Valuable Professional (MVP) 15 consecutive years and am also a Microsoft Certified SharePoint Masters (MCSM) since 2013.
  • Big fan of Power Platform technologies and implemented many solutions.
  • Passionate #Programmer #SharePoint #SPFx #M365 #Power Platform| Microsoft MVP | SharePoint StackOverflow, Github, PnP contributor
  • Web site – https://kamdaryash.wordpress.com Youtube channel - https://www.youtube.com/channel/UCM149rFkLNgerSvgDVeYTZQ/