In Azure Synapse Analytics, I would like to use the pipeline function to set a variable of type array for the date from 12 months ago to current date.
The date in the variable is the first day of each month.
For example, a variable to be created this month might look like this
['2022-08-01','2022-09-01','2022-10-01','2022-11-01','2022-12-01','2023-01-01','2023-02-01','2023-03-01','2023-04-01','2023-05-01','2023-06-01','2023-07-01','2023-08-01']
And the variables to be created in September will be like this
['2022-09-01','2022-10-01','2022-11-01','2022-12-01','2023-01-01','2023-02-01','2023-03-01','2023-04-01','2023-05-01','2023-06-01','2023-07-01','2023-08-01','2023-09-01']
Any answer would be helped.
Thank you.
2
Answers
You can use for each and append variable activity as below.
First, use
Set variable
to create empty array as below.Empty date array
Next, create
ForEach
activity and addAppend variable
activity inside it.Here, I given the items in range between
0
to13
since you want dates for last 12 months.
If you have this number in any variable or parameter add that in second argument of this range function.
Make sure to enable
Sequential
Next, inside append variable add expression as below.
Here, in the Name field choose the empty array variable you created initially.
And in value field add above expression given.
Next, create new
Set variable
and create new array variable then give the value of the date array variable previously created.Output:
Note:
Give the date according to your requirement,
but make sure not give
29
,30
and31
as these dates will not be in every month.If you have a nearby SQL database of any kind, I would probably get it from there. Any good warehouse has a calendar table for just this type of thing. Here’s a simple example using
generate_series
now availabe in SQL Server 2022 and Azure SQL DB:Get the data into the pipeline using a Lookup activity and Query mode.
I also got a method working with a
For Each
andAppend variable
activity, but it takes 16 seconds to run!? NB You can just set ’01’ in the format argument to get the first of the month:Append variable results