foreach loop - datediff

arkiboys 9,686 Reputation points
2022-10-14T05:49:26.107+00:00

hello,
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.
10,196 questions
{count} votes

Accepted answer
  1. AnnuKumari-MSFT 32,161 Reputation points Microsoft Employee
    2022-10-14T07:29:16.023+00:00

    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:
      250238-datediff1.gif
    2. When datediff is 2 , Wait activity gets triggered twice and so on:

    250318-datediff2.gif

    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 31,511 Reputation points MVP
    2022-10-14T06:59:41.64+00:00
    0 comments No comments