+ Reply to Thread
Results 1 to 7 of 7

Transpose Batch Excel Data

Hybrid View

  1. #1
    Registered User
    Join Date
    01-05-2016
    Location
    Texas, USA
    MS-Off Ver
    Office 365
    Posts
    13

    Transpose Batch Excel Data

    Hello, I have a spreadsheet with rates per lb that I need to transpose into a different format so I can upload into a rating system. I have attached an excel spreadsheet with the original data on the first tab and the desired result on the second tab. I am trying to figure out an easy way to this as the main file has about 1000 rows that need to be transposed. Hope someone can help.
    Attached Files Attached Files

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,698

    Re: Transpose Batch Excel Data

    With Power Query/Get and Transform which is on the Data Tab. Here is the Mcode resulting from that transformation

    let
        Source = Excel.CurrentWorkbook(){[Name="Table33"]}[Content],
        #"Demoted Headers" = Table.DemoteHeaders(Source),
        #"Changed Type" = Table.TransformColumnTypes(#"Demoted Headers",{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}, {"Column5", type number}, {"Column6", type number}, {"Column7", type number}, {"Column8", type number}, {"Column9", type number}, {"Column10", type number}, {"Column11", type number}, {"Column12", type number}, {"Column13", type number}}),
        #"Transposed Table" = Table.Transpose(#"Changed Type"),
        #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Transposed Table", {{"Column1", type text}, {"Column2", type text}}, "en-US"),{"Column1", "Column2"},Combiner.CombineTextByDelimiter(":", QuoteStyle.None),"Merged"),
        #"Transposed Table1" = Table.Transpose(#"Merged Columns"),
        #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table1", [PromoteAllScalars=true]),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Promoted Headers", {"Origin City:", "Origin State:", "Destination City:", "Destination State:"}, "Attribute", "Value"),
        #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByDelimiter(":", QuoteStyle.Csv), {"Attribute.1", "Attribute.2"}),
        #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Attribute.1", Int64.Type}, {"Attribute.2", Int64.Type}})
    in
        #"Changed Type1"
    Review PQ
    In the attached file
    Click on any cell in the new table
    On the Data Tab, click on Queries & Connections
    In the right window, double click to open Query
    Review PQ steps

    M-code basics:
    - "let" is the start of a query
    - "in" is the closing of a query
    - each transformation step sits in between those 2 lines
    - each step line is ended by a comma, except the last one
    - "Source" is always the first step (Source Data)
    - After "in" you have the last step referenced
    Attached Files Attached Files
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    01-05-2016
    Location
    Texas, USA
    MS-Off Ver
    Office 365
    Posts
    13

    Re: Transpose Batch Excel Data

    It absolutely worked. I added the additional data to the file that you sent and refreshed the Query and it populated all the data I needed. Thank you so much. You saved me a ton of time. I do have a question though, where can I see the Mcode in case I wanted to manipulate it by adding more columns to the Source file?

  4. #4
    Registered User
    Join Date
    01-05-2016
    Location
    Texas, USA
    MS-Off Ver
    Office 365
    Posts
    13

    Re: Transpose Batch Excel Data

    It absolutely worked. I added the additional data to the file that you sent and refreshed the Query and it populated all the data I needed. Thank you so much. You saved me a ton of time. I do have a question though, where can I see the Mcode in case I wanted to manipulate it by adding more columns to the Source file?

  5. #5
    Forum Expert
    Join Date
    05-29-2020
    Location
    NH USA
    MS-Off Ver
    365
    Posts
    2,103

    Re: Transpose Batch Excel Data

    Just in case you want a different option, here is a code process:

    Option Explicit
    
    
    Sub TransformROWSTOCOL()
    
    Dim rsht1 As Long, rsht2 As Long, i As Long, Col As Long
    Dim wsTest As Worksheet, mr As Worksheet, ms As Worksheet
    Dim c As Range
    
    'check if sheet "ouput" already exist
    
    Const strSheetName As String = "Transform"
    
    Set wsTest = Nothing
    On Error Resume Next
    Set wsTest = ActiveWorkbook.Worksheets(strSheetName)
    On Error GoTo 0
     
    If wsTest Is Nothing Then
        Worksheets.Add.Name = strSheetName
    End If
    
    'set the data
                     
    Set mr = Sheets("Original Data") 'this is the name of the source sheet
    Set ms = Sheets("Transform")     'this is the name of the destiny sheet
    Col = 5
    'End set the data
        With ms
         .UsedRange.ClearContents
         .Range("A1:D1").Value = Array("Origin City", "Origin State", "Destination City", "Destination State")
        End With
        rsht2 = ms.Range("A" & Rows.Count).End(xlUp).Row
        With mr
              rsht1 = .Range("A" & .Rows.Count).End(xlUp).Row
              For i = 2 To rsht1
                    Do While .Cells(1, Col).Value <> ""
                    rsht2 = rsht2 + 1
                    ms.Range("A" & rsht2).Value = .Range("A" & i).Value
                    ms.Range("B" & rsht2).Value = .Range("B" & i).Value
                    ms.Range("C" & rsht2).Value = .Range("C" & i).Value
                    ms.Range("D" & rsht2).Value = .Range("D" & i).Value
                    ms.Range("E" & rsht2).Value = .Cells(1, Col).Value
                    ms.Range("F" & rsht2).Value = .Cells(2, Col).Value
                    ms.Range("G" & rsht2).Value = .Cells(i, Col).Value
                    
                    Col = Col + 1
                Loop
                Col = 2
            Next
        End With
        
      With ms
        .Range("F2:F" & .Rows.Count).SpecialCells(Type:=xlCellTypeBlanks).EntireRow.Delete
        .Columns("A:D").EntireColumn.AutoFit
        End With
        
    End Sub
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    01-05-2016
    Location
    Texas, USA
    MS-Off Ver
    Office 365
    Posts
    13

    Re: Transpose Batch Excel Data

    Quote Originally Posted by maniacb View Post
    Just in case you want a different option, here is a code process:

    Option Explicit
    
    
    Sub TransformROWSTOCOL()
    
    Dim rsht1 As Long, rsht2 As Long, i As Long, Col As Long
    Dim wsTest As Worksheet, mr As Worksheet, ms As Worksheet
    Dim c As Range
    
    'check if sheet "ouput" already exist
    
    Const strSheetName As String = "Transform"
    
    Set wsTest = Nothing
    On Error Resume Next
    Set wsTest = ActiveWorkbook.Worksheets(strSheetName)
    On Error GoTo 0
     
    If wsTest Is Nothing Then
        Worksheets.Add.Name = strSheetName
    End If
    
    'set the data
                     
    Set mr = Sheets("Original Data") 'this is the name of the source sheet
    Set ms = Sheets("Transform")     'this is the name of the destiny sheet
    Col = 5
    'End set the data
        With ms
         .UsedRange.ClearContents
         .Range("A1:D1").Value = Array("Origin City", "Origin State", "Destination City", "Destination State")
        End With
        rsht2 = ms.Range("A" & Rows.Count).End(xlUp).Row
        With mr
              rsht1 = .Range("A" & .Rows.Count).End(xlUp).Row
              For i = 2 To rsht1
                    Do While .Cells(1, Col).Value <> ""
                    rsht2 = rsht2 + 1
                    ms.Range("A" & rsht2).Value = .Range("A" & i).Value
                    ms.Range("B" & rsht2).Value = .Range("B" & i).Value
                    ms.Range("C" & rsht2).Value = .Range("C" & i).Value
                    ms.Range("D" & rsht2).Value = .Range("D" & i).Value
                    ms.Range("E" & rsht2).Value = .Cells(1, Col).Value
                    ms.Range("F" & rsht2).Value = .Cells(2, Col).Value
                    ms.Range("G" & rsht2).Value = .Cells(i, Col).Value
                    
                    Col = Col + 1
                Loop
                Col = 2
            Next
        End With
        
      With ms
        .Range("F2:F" & .Rows.Count).SpecialCells(Type:=xlCellTypeBlanks).EntireRow.Delete
        .Columns("A:D").EntireColumn.AutoFit
        End With
        
    End Sub
    Thank you. This does provide me with another option as I have to do a bunch of these every month. I appreciate you taking the time to respond!

  7. #7
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 insider Version 2504 Win 11
    Posts
    24,698

    Re: Transpose Batch Excel Data

    With Power Query open in the editor, there are two places you can see the Mcode.
    1. On Home, click on the Advanced Editor
    2. On View Tab, click on Advanced Editor

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Batch Transpose
    By probuddha in forum Excel General
    Replies: 3
    Last Post: 01-12-2017, 08:05 AM
  2. [SOLVED] Aggregate Batch Data from many word docs into an Excel (i.e.Check Boxes, Text Form Fields)
    By Chrisdudley7 in forum Word Formatting & General
    Replies: 7
    Last Post: 05-13-2014, 01:28 PM
  3. [SOLVED] Formula to round down on a batch and exclude anything under a full batch
    By toomuchbrew in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-15-2012, 11:04 AM
  4. Replies: 0
    Last Post: 06-19-2012, 11:36 AM
  5. [SOLVED] Batch extract PDF data to Excel using regular expressions
    By dwilkie@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-25-2006, 05:15 AM
  6. How to batch create / generate and print invoices from Excel data?
    By helenerz in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-04-2005, 10:05 PM
  7. Excel charts data auto range / batch update ?
    By agb in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 05-22-2005, 10:44 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1