foreach loop - datediff

arkiboys 9,646 Reputation points

in the datafactory foreach loop, I am using two parameters:
p_StartDate which is a string and I set it to 2022-09-02
p_DaysToGet which is an Int and I set it to the number which is the difference between p_StartDate and today.
i.e. 42
in the expression for the foreach loop, I have this range function:
@Rover (0, pipeline().parameters.p_DaysToGet)

Instead of using an integer number how can I use the datediff function for this so it becomes dynamic?
there does not seem to be a datediff function available.
Thank you

Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
9,696 questions
{count} votes

Accepted answer
  1. AnnuKumari-MSFT 31,561 Reputation points Microsoft Employee

    Hi @arkiboys ,

    Thankyou for using Microsoft Q&A platform and thanks for posting your question here.

    As I understand your ask here, you are trying to get the datediff between the parameter and current date and using the same in forEach activity to loop through the range. Kindly let me know if that is not the case.

    To achieve the requirement , you can use ticks() function which converts the date format into int value where one tick equals 100 nanoseconds. On top of that, you can use sub() function to get the difference between two dates .

    You need only one parameter 'p_StartDate' with string value . Then use the below expression in ForEach Activity: @range(0,int(div(div(sub(ticks(formatDateTime(utcNow(),'yyyy-MM-dd')),ticks(pipeline().parameters.p_StartDate)),36000000000),24)))

    Here , first div() is to convert nanosecond to hours by dividing with 36000000000 and then second div() is to convert into number of days by dividing with 24.

    1. When datediff is 1, Wait activity is triggered once:
    2. When datediff is 2 , Wait activity gets triggered twice and so on:


    Hope this will help. Please let us know if any further queries.


    • Please don't forget to click on 130616-image.png or upvote 130671-image.png button whenever the information provided helps you.
      Original posters help the community find answers faster by identifying the correct answer. Here is how
    • Want a reminder to come back and check responses? Here is how to subscribe to a notification
    • If you are interested in joining the VM program and help shape the future of Q&A: Here is how you can be part of Q&A Volunteer Moderators
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Nandan Hegde 29,901 Reputation points MVP
    0 comments No comments