# How to transpose datatable?

**URL:** <https://forum.uipath.com/t/how-to-transpose-datatable/741886>\
**Category:** Studio\
**Tags:** activities-panel, question, studio, system, activities\
**Created:** [May 18, 2024, 5:51pm UTC](https://forum.uipath.com/t/how-to-transpose-datatable/741886 "2024-05-18T17:51:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SunnyJha](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/sunnyjha/32/401187_2.png) [@SunnyJha](https://forum.uipath.com/u/SunnyJha)\
**Post date:** [May 18, 2024, 5:51pm UTC](https://forum.uipath.com/t/how-to-transpose-datatable/741886/1 "2024-05-18T17:51:33Z")

</div>

I have a datatable in which the values in second column needs to be header in the transposed dt and the values from 3rd column onwards needs to be datarow in the transposed dt

---

<div class="post-metadata">

**Author:** ![aravindbalineni123](https://avatars.discourse-cdn.com/v4/letter/a/22d042/32.png) [@aravindbalineni123](https://forum.uipath.com/u/aravindbalineni123)\
**Post date:** [May 18, 2024, 7:45pm UTC](https://forum.uipath.com/t/how-to-transpose-datatable/741886/2 "2024-05-18T19:45:37Z")

</div>

Hi Sunny,

Please use these methods to transponse datatable in UiPath:

> [@How to Transpose Excel data or Data Table?](https://forum.uipath.com/t/how-to-transpose-excel-data-or-data-table/477102):
>
> Hello UiPathans robotHere we discuss how to Transpose the data in Excel or a data table What is meant by Transpose? Input [image] Output point_down [image] Transpose can achieve by two methods → Modern activity- Using Copy/Paste range Activity.(For Excel Processing) → Novigo Solutions. (For Datatable Processing) Modern activity- Copy/Paste range Activity. By Using modern activity we can achieve the transpose method. For classic users can select Show Modern from the Activit…

Let me know if it doesn’t work as expected.

---

<div class="post-metadata">

**Author:** ![Anil\_G](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/anil_g/32/524291_2.png) [@Anil\_G](https://forum.uipath.com/u/Anil_G)\
**Post date:** [May 19, 2024, 4:04am UTC](https://forum.uipath.com/t/how-to-transpose-datatable/741886/3 "2024-05-19T04:04:23Z")

</div>

@SunnyJha

You can follow the steps

1. Delete the first column as you dont need usign delete data row
2. Now use generate datatable activity and pass the string as `String.Join(",",dt.AsEnumerable.Select(function(x) x(0).ToString))` and select comma as row separator and check the header box
3. Delete the column which you want as headers also the second one
4. Now use for loop with loop on all columns dt.Columns
5. Now inside use add data row and in the arrayrow property pass `String.Join(",",dt.AsEnumerable.Select(function(x) x(currentitem.ColumnName).ToString))` and in datatable pass the generated table in step 2

Cheers

---

<div class="post-metadata">

**Author:** ![Jayesh\_678](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/jayesh_678/32/219158_2.png) [@Jayesh\_678](https://forum.uipath.com/u/Jayesh_678)\
**Post date:** [May 19, 2024, 4:27am UTC](https://forum.uipath.com/t/how-to-transpose-datatable/741886/4 "2024-05-19T04:27:13Z")

</div>

Hi @SunnyJha ,

Try as below

#### Step 1: Read Range

- Properties:
  - **SheetName** : “Sheet1” (or your sheet name)
  - **Range** : “A1”
  - **DataTable** : `originalDT`

#### Step 2: Invoke Code

- Properties:
  - **Code** : (Use the [VB.Net](http://VB.Net) code provided above)
  - **Language** : [VB.Net](http://VB.Net)
  - **Arguments** :
    - `originalDT`: InArgument (Type: `System.Data.DataTable`)

- 
  - 
    - `transposedDT`: OutArgument (Type: `System.Data.DataTable`)

#### Step 3: Write Range

- Properties:
  - **SheetName** : “TransposedSheet” (or any desired sheet name)
  - **DataTable** : `transposedDT`

Use below code in invoke code :-

’ Inputs: originalDT (DataTable)  
’ Outputs: transposedDT (DataTable)  
Dim transposedDT As New DataTable

’ Add columns to transposedDT based on the second column of originalDT  
For Each row As DataRow In originalDT.Rows  
transposedDT.Columns.Add(row(1).ToString())  
Next

’ Add rows to transposedDT with data from the third column onwards of originalDT  
For colIndex As Integer = 2 To originalDT.Columns.Count - 1  
Dim newRow As DataRow = transposedDT.NewRow()  
For rowIndex As Integer = 0 To originalDT.Rows.Count - 1  
newRow(rowIndex) = originalDT.Rows(rowIndex)(colIndex).ToString()  
Next  
transposedDT.Rows.Add(newRow)  
Next

’ Output transposedDT  
transposedDT

Thanks,  
Jayesh

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/system/32/182466_2.png) [@system](https://forum.uipath.com/u/system)\
**Post date:** [June 10, 2024, 6:22am UTC](https://forum.uipath.com/t/how-to-transpose-datatable/741886/5 "2024-06-10T06:22:04Z")

</div>

Closed on June 10, 2024, 6:22am UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
