+ Reply to Thread
Results 1 to 8 of 8

Unpivoting columns in pairs

  1. #1
    Forum Contributor
    Join Date
    03-19-2010
    Location
    Calgary, Canada
    MS-Off Ver
    Office 2013, Office 365 Pro Plus
    Posts
    182

    Unpivoting columns in pairs

    Morning everyone,

    I have a table with the following headings:

    Date | Time | Class | Type | Value |Type2 | Value2 | Type3 | Value3

    What I need is to restructure the table into the form

    Date | Time | Class | Type | Value

    I'm currently using Excel 2013 and I've been messing around with Power Query in order to get the output I need, so far no luck. Can anyone help me please? Ideally with a VBA solution?

    Many thanks
    Last edited by jlt199; 07-10-2018 at 12:46 PM.

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,989

    Re: Unpivoting columns in pairs

    Can be done easily with PQ - provide a sample workbook and I'll show you how.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  3. #3
    Forum Contributor
    Join Date
    03-19-2010
    Location
    Calgary, Canada
    MS-Off Ver
    Office 2013, Office 365 Pro Plus
    Posts
    182

    Re: Unpivoting columns in pairs

    Thank you, that would be appreciated. I think I've attached a sample copy of the workbook
    Attached Files Attached Files

  4. #4
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,891

    Re: Unpivoting columns in pairs

    The way I'd do it... Using PowerQuery.
    Make 3 separate queries to source data.
    In each query remove all but 1 Type/Value pair (each retaining different pair).
    Rename each Type/Value pair to have same name.
    Pick 1 query, and append other 2 to it.

    If using VBA... there are many ways to do it.
    1. Use ADO to query and use SQL statement to transform data (i.e. same concept as PQ, but using UNION ALL).
    2. Put range into array. Create empty array with row count x 3 of original, and 5 columns. Loop through original array and fill the empty array.
    etc etc.
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  5. #5
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    MS365 Apps for enterprise
    Posts
    5,891

    Re: Unpivoting columns in pairs

    See attached for sample.
    Attached Files Attached Files

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,989

    Re: Unpivoting columns in pairs

    OK - here's the M code to do what you want in PQ:

    Please Login or Register  to view this content.
    Make 3 separate queries to source data.
    No need for that, really, although it gets the result.

  7. #7
    Forum Contributor
    Join Date
    03-19-2010
    Location
    Calgary, Canada
    MS-Off Ver
    Office 2013, Office 365 Pro Plus
    Posts
    182

    Re: Unpivoting columns in pairs

    Thank you both, I now have something working

  8. #8
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,989

    Re: Unpivoting columns in pairs

    You're welcome!

    PQ or VBA?

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ 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. unpivoting data and creating a pivottable
    By ammartino44 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-14-2015, 11:24 AM
  2. Find Duplicates across 2 pairs of columns (2 columns against 2 columns)
    By erjfly2013 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-09-2015, 06:52 PM
  3. Replies: 6
    Last Post: 05-30-2013, 04:58 PM
  4. [SOLVED] combining two pairs of columns
    By intothewild in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 01-02-2013, 06:11 PM
  5. [SOLVED] Sum product of many pairs of columns
    By KeenKiwi in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-11-2006, 08:10 AM
  6. merge pairs of columns
    By mpreddy in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-25-2006, 06:51 AM
  7. Remove Duplicate Pairs(2 Columns)
    By deathswan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-31-2006, 08:25 PM

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