Swapping Two Array Elements in Azure Data Factory / Synapse Pipeline via Regular Expressions

Problem Statement :

How to Swap 2 Elements of an Array :

Input :

Output :

Prerequisites :

  1. Azure Data Factory / Synapse

Solution :

GitHUB Code

In Synapse / ADF , it is not possible to self reference a variable in Set Variable Activity. Hence, we would need to create a TempArray variable and then over ride the original array with the TempArray one.

  1. To Swap the elements in an array, we can follow the below option of recreating a new array from the Initial Array :
    • a) Get elements from 0 index till N-1 Index , where N is the Minimum Index (Elements from Left side of Minimum Index)
    • b) The Maximum Index position Element would be Element in the Minimum Index Position Element.
    • c) Get elements in between the Minimum Index and the Maximum Index (Middle part between both Index)
    • d) The Minimum Index position Element would be Element in the Maximum Index Position Element.
    • e) Get elements from M+1 index till end, where M is the Maximum Index (Elements from Right side of Maximum Index)
  2. To get Elements from Left side of Minimum Index (#a)
@take(variables('InputArray'), pipeline().parameters.SwapIndex1)

Result :

3. To get Middle part between both Index (#c)

@take(skip(variables('InputArray'),add(pipeline().parameters.SwapIndex1,1)),sub(pipeline().parameters.SwapIndex2,add(pipeline().parameters.SwapIndex1,1)))

Result :

4. To get Elements from Right side of Maximum Index (#e)

@skip(variables('InputArray'), add(pipeline().parameters.SwapIndex2,1))

Result :

5. To generate an Array based on #a to #e mentioned in #1

@createArray(if(empty(variables('LeftHalf')),'',variables('LeftHalf')),variables('InputArray')[pipeline().parameters.SwapIndex2],if(empty(variables('Middle')),'',variables('Middle')),variables('InputArray')[pipeline().parameters.SwapIndex1],if(empty(variables('RightHalf')),'',variables('RightHalf')))

Result :

6. To Override the InputArray variable and get the Array in required format :

@json(concat('[',replace(replace(replace(replace(string(variables('TempArray')), '[', ''),']',''),'"",',''),'""',''),']'))

Final Result :

Note : For below scenario

Results :

If we use the below expression in TempArray Set Variable Activity :

@createArray(variables('LeftHalf'),variables('InputArray')[pipeline().parameters.SwapIndex2],variables('Middle'),variables('InputArray')[pipeline().parameters.SwapIndex1],variables('RightHalf')))

Final Output :

So to avoid the null value, we have to manage the Empty Arrays in case if any for LeftHalf, Middle and RightHalf variables by replacing it Empty Array with ” as with below code in TempArray activity.

@createArray(if(empty(variables('LeftHalf')),'',variables('LeftHalf')),variables('InputArray')[pipeline().parameters.SwapIndex2],if(empty(variables('Middle')),'',variables('Middle')),variables('InputArray')[pipeline().parameters.SwapIndex1],if(empty(variables('RightHalf')),'',variables('RightHalf')))

Output :

And Replace “”, & “” with blank in Final activity

Output :

Published by Nandan Hegde

Microsoft Data MVP |Microsoft Data platform Architect | Blogger | MSFT Community Champion I am a MSFT Data Platform MVP and Business Intelligence and Data Warehouse professional working within the Microsoft data platform eco-system which includes Azure Synapse Analytics ,Azure Data Factory ,Azure SQL Database and Power BI. To help people keep up with this ever-changing landscape, I frequently posts on LinkedIn, Twitter and to his blog on https://datasharkx.wordpress.com. LinkedIn Profile : www.linkedin.com/in/nandan-hegde-4a195a66 GitHUB Profile : https://github.com/NandanHegde15 Twitter Profile : @nandan_hegde15 MSFT MVP Profile : https://mvp.microsoft.com/en-US/MVP/profile/8977819f-95fb-ed11-8f6d-000d3a560942

Leave a comment

Design a site like this with WordPress.com
Get started