cancel
Showing results for 
Search instead for 
Did you mean: 

Transpose columns(Dynamic) into rows.

kumar25
New Contributor II

Hi,

I have to convert/transpose columns into rows. Basis the input file the count of columns may increase/decrease. Have to pick all the columns and convert them into rows.

Input:

INV111INV112INV113INV114INV115INV116INV117INV118
TFR089TFR2TFR3TFR34TFR53TFR67TFR222TFR902
TDR1TDR087TDR22TDR44TDR575TDR58TDR96TDR67
TFET9TFET7TFET4TFET22TFET87TFET34TFET099TFET123
VER0VER1VER2VER3VER4VER5VER6VER7
USDUSDUSDUSDUSDUSDUSDUSD
FY24FY24FY24FY24FY24FY24FY24FY24
JanFebMarAprMayJunJulAug
                     4,206.21                   46,557.34                     5,701.96                     5,775.38                   18,677.77                     6,046.35                     4,733.21                     4,764.49

Output:

INV111TFR089TDR1TFET9VER0USDFY24Jan    4,206.21
INV112TFR2TDR087TFET7VER1USDFY24Feb 46,557.34
INV113TFR3TDR22TFET4VER2USDFY24Mar    5,701.96
INV114TFR34TDR44TFET22VER3USDFY24Apr    5,775.38
INV115TFR53TDR575TFET87VER4USDFY24May 18,677.77
INV116TFR67TDR58TFET34VER5USDFY24Jun    6,046.35
INV117TFR222TDR96TFET099VER6USDFY24Jul    4,733.21
INV118TFR902TDR67TFET123VER7USDFY24Aug    4,764.49

 

2 ACCEPTED SOLUTIONS

koryknick
Employee
Employee

@kumar25 - Here is another solution that does not use the Script snap.  I typically avoid the Script snap as it is notoriously difficult to debug and requires a different skill set that some developers may not yet possess.

This solution uses a Gate snap to gather all the data into a single array.  Please be conscious of how large the file is that you're consuming as this will load the entire dataset into memory, which can't be avoided in this case since you want to pivot the entire dataset.

Then in the Mapper, we're using the Object.keys() method to find the key names that were read in - basically just so we can loop through and grab all the values you want to pivot.  Then we're using the Array.map() method to change the value of each array element - this is where we're pivoting the records by re-creating the record from each set of column values.  Within the map() method, we're also using the Object.get() method to retrieve the value associated with the current field-name we're on.

After the Mapper, we have a JSON Splitter snap to pull the array elements into individual documents, completing the pivot.

Hope this helps!

View solution in original post

koryknick
Employee
Employee

Sorry - here is the attachment.

View solution in original post

5 REPLIES 5

SpiroTaleski
Valued Contributor

@kumar25 

The attached pipeline might help you achieve your objective.

BR,

Spiro

 

Thanks @SpiroTaleski 

koryknick
Employee
Employee

@kumar25 - Here is another solution that does not use the Script snap.  I typically avoid the Script snap as it is notoriously difficult to debug and requires a different skill set that some developers may not yet possess.

This solution uses a Gate snap to gather all the data into a single array.  Please be conscious of how large the file is that you're consuming as this will load the entire dataset into memory, which can't be avoided in this case since you want to pivot the entire dataset.

Then in the Mapper, we're using the Object.keys() method to find the key names that were read in - basically just so we can loop through and grab all the values you want to pivot.  Then we're using the Array.map() method to change the value of each array element - this is where we're pivoting the records by re-creating the record from each set of column values.  Within the map() method, we're also using the Object.get() method to retrieve the value associated with the current field-name we're on.

After the Mapper, we have a JSON Splitter snap to pull the array elements into individual documents, completing the pivot.

Hope this helps!

kumar25
New Contributor II

Thanks @koryknick