+ Reply to Thread
Results 1 to 8 of 8

Power Query Re-Shaping Data

  1. #1
    Forum Contributor
    Join Date
    02-08-2005
    MS-Off Ver
    Microsoft 365
    Posts
    839

    Power Query Re-Shaping Data

    Hi,

    I have data in A1:Q7 that I would like to re-shape to look like that in A10 to A16.

    I want to use Power Query to do this.

    Can someone please suggest the M Code to achieve this? Ideally the solution will include a dynamic re-ordering of the columns rather than a drag and drop in the PQ editor. The reason for this is that this is made-up data for this query. The real data covers dozens of dates.

    Can someone please suggest a Power Query solution?

    Thanks!
    Attached Files Attached Files
    Last edited by andrewc; 02-11-2025 at 06:16 PM.

  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,672

    Re: Power Query Re-Shaping Data

    Is the date in Column M correct. If yes, then this causes some issues with Power Query. How important is the sort of Line two to be as shown with Weight first, etc. If the date in M is incorrect and the sort order other than date sequence is not significant, I think I can make this work in Power Query. However, if the order of data is critical, then someone else may be able to help as that is above my skill level.
    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
    Forum Contributor
    Join Date
    02-08-2005
    MS-Off Ver
    Microsoft 365
    Posts
    839

    Re: Power Query Re-Shaping Data

    Sorry, I messed up the dates in the first table!

    I've attached a revised workbook.

    Thanks!
    Attached Files Attached Files

  4. #4
    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,672

    Re: Power Query Re-Shaping Data

    Before I start, will you answer my questions in the post regarding the horizontal sorts.

  5. #5
    Forum Contributor
    Join Date
    02-08-2005
    MS-Off Ver
    Microsoft 365
    Posts
    839

    Re: Power Query Re-Shaping Data

    Hi,

    The sort order of line 2 isn't critical as long as all columns of the same type are kept together e.g. the weights are all together, so those columns don't have Growth Rate or Price among them.

    Hope that makes sense.

    Thanks!

  6. #6
    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,672

    Re: Power Query Re-Shaping Data

    Look at this solution. I think that I have what you are looking for.

    PHP Code: 
    let
        Source 
    Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        
    #"Demoted Headers" = Table.DemoteHeaders(Source),
        #"Transposed Table" = Table.Transpose(#"Demoted Headers"),
        #"Added Custom" = Table.AddColumn(#"Transposed Table", "Custom", each if Text.StartsWith([Column1],"Column") then null else [Column1]),
        #"Reordered Columns" = Table.ReorderColumns(#"Added Custom",{"Custom", "Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7"}),
        #"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"Column1"}),
        #"Filled Down" = Table.FillDown(#"Removed Columns",{"Custom"}),
        #"Filled Up" = Table.FillUp(#"Filled Down",{"Custom"}),
        #"Merged Columns" = Table.CombineColumns(#"Filled Up",{"Custom", "Column2"},Combiner.CombineTextByDelimiter(":", QuoteStyle.None),"Merged"),
        #"Transposed Table1" = Table.Transpose(#"Merged Columns"),
        #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table1", [PromoteAllScalars=true]),
        #"Renamed Columns" = Table.RenameColumns(#"Promoted Headers",{{"1/1/2025:CODE", "CODE"}, {"1/1/2025:NAME", "NAME"}}),
        #"Filtered Rows1" = Table.SelectRows(#"Renamed Columns", each ([CODE] <> "TOTAL")),
        
    ColumnsToKeep = List.Select(Table.ColumnNames(#"Filtered Rows1"), each Text.Contains(_, "CODE") or Text.Contains(_, "NAME") or Text.Contains(_, "WEIGHT") ),
        
    Unpivoted Table.UnpivotOtherColumns(#"Filtered Rows1", ColumnsToKeep, "Attribute", "Value"),
        #"Pivoted Column" = Table.Pivot(Unpivoted, List.Distinct(Unpivoted[Attribute]), "Attribute", "Value", List.Sum)
    in
       
    #"Pivoted Column" 
    Attached Files Attached Files
    Last edited by alansidman; 02-10-2025 at 11:10 AM.

  7. #7
    Forum Contributor
    Join Date
    02-08-2005
    MS-Off Ver
    Microsoft 365
    Posts
    839

    Re: Power Query Re-Shaping Data

    Thank you very much!

  8. #8
    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,672

    Re: Power Query Re-Shaping Data

    Thanks for the rep.

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

+ 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. [SOLVED] Power Query Experts Add file picker and pass data to the query issue
    By Belinea2010 in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 07-29-2024, 04:04 AM
  2. Replies: 1
    Last Post: 01-08-2024, 09:57 PM
  3. [SOLVED] Why is my Excel file so big even though I uploaded the data using Power Pivot/Power Query?
    By Olivia Ludwig in forum Excel Charting & Pivots
    Replies: 12
    Last Post: 09-21-2023, 05:18 AM
  4. [SOLVED] [Power Query] How to filter 0 from two columns in power query editor in one step?
    By daliye in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-16-2023, 09:44 PM
  5. Replies: 1
    Last Post: 07-14-2023, 10:15 AM
  6. Replies: 4
    Last Post: 02-17-2020, 06:03 AM
  7. Sub-Forum for Excel Power Tools (Power Query, Power Pivot & Power BI)
    By chullan88 in forum Suggestions for Improvement
    Replies: 10
    Last Post: 06-28-2018, 02: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