问题
I created a pipeline in data factory and I want to retrieve data from a source for the current month and for the previous month. When I run the pipeline I give the needed parameter named ExtractDate. The format is MM/DD/YYYY . For the current month I used the following expression in 'Set Variable' activity:
@replace(item().Query,'EXTRACTDATE',formatDateTime(variables('ExtractDate'), 'yyyyMM'))
And for the previous month I tried:
@adddays(variables('ExtractDate'),-28)
The problem appears when the user will set when running the pipeline the date 07/31/2019 for example. Then the previous month will still be July. And if I increase the number to 31, then there is a possibility that the user will introduce 03/01/2019 and from March it will skip the month of February.
I tried to think of a solution, but unfortunately there is no 'addmonths' available in data factory.
Any ideas please?...I've spent 2 days on this issue..
回答1:
addMonths and addYears are not supported by ADF so far.Please vote up this thread to push the progress.
My trick is use combination of bulit-in functions in ADF. Please see my test:
This month is very simple:
@concat(substring('07/16/2019',6,4),substring('07/16/2019',0,2))
output:
Last month is little complex.It should check if it is the first month of the year.
@if(equals(substring('07/16/2019',0,2),'01'),
concat(
string(sub(
int(substring('07/16/2019',6,4)),1)),
'12'),
string(sub(
int(concat(substring('07/16/2019',6,4),
substring('07/16/2019',0,2))),1)
)
)
if the input param is 01/16/2019,then the output looks like:
My test is based on the static value,please replace it with your variable.
Just for summarize:
The final working dynamic content should be like as below:
@if( equals(variables('SubstringMonth'),'01'),
concat(string(sub(int(variables('SubstringYear')),1)),'12'),
concat(variables('SubstringYear'),string(if(or(equals(sub(int(variables('SubstringMonth')),1),11),equals(sub(int(variables('SubstringMonth')),1),10)),
sub(int(variables('SubstringMonth')),1) ,
concat('0',string(sub(int(variables('SubstringMonth')),1) )))) ))
回答2:
For the previous month from today you could use
formatDateTime(AddToTime(utcnow(), -1, 'Month'), 'yyyy-MM-dd')
来源:https://stackoverflow.com/questions/57043010/subtract-number-of-days-based-on-provided-date-parameter