+ Reply to Thread
Results 1 to 10 of 10

Power Query: transform a table with sub-items.

  1. #1
    Forum Contributor
    Join Date
    03-05-2018
    Location
    Tbilisi, Georgia
    MS-Off Ver
    2013, 2016
    Posts
    271

    Power Query: transform a table with sub-items.

    Good day,
    I need to transform a table with sub-items via Power Query.
    Desired output in attached file.
    P.S. I've already done it via formulas, but I need solution via Power Query.
    Attached Files Attached Files
    Last edited by T.I.; 07-03-2022 at 10:45 AM.

  2. #2
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Power Query: transform a table with sub-items.

    It looks the format of a pivot table.

    Maybe it is better to change the original data.
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  3. #3
    Forum Contributor
    Join Date
    03-05-2018
    Location
    Tbilisi, Georgia
    MS-Off Ver
    2013, 2016
    Posts
    271

    Re: Power Query: transform a table with sub-items.

    Quote Originally Posted by oeldere View Post
    Maybe it is better to change the original data.
    Unfortunately it is not possible.

  4. #4
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Power Query: transform a table with sub-items.

    Then make the result by pivot table on the original data.

    I suppose there is no need to use Power Query for this.

  5. #5
    Forum Contributor
    Join Date
    03-05-2018
    Location
    Tbilisi, Georgia
    MS-Off Ver
    2013, 2016
    Posts
    271

    Re: Power Query: transform a table with sub-items.

    Original data has a lot more records, so for flexibility I prefer to use Power Query (I believe it's possible).

  6. #6
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,865

    Re: Power Query: transform a table with sub-items.

    Here is a power query solution
    PHP Code: 
    let
        Source 
    Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        
    #"Added Custom" = Table.AddColumn(Source, "CODE.1", each if Text.Contains([Code],[Code]) then [Code] else null),
        #"Replaced Errors" = Table.ReplaceErrorValues(#"Added Custom", {{"CODE.1", null}}),
        #"Filled Down" = Table.FillDown(#"Replaced Errors",{"CODE.1"}),
        #"Added Custom1" = Table.AddColumn(#"Filled Down", "Category", each if Text.Contains([Name],"Category") then [Name] else null),
        #"Filled Down1" = Table.FillDown(#"Added Custom1",{"Category"}),
        #"Filtered Rows" = Table.SelectRows(#"Filled Down1", each ([Revenue] <> null)),
        #"Renamed Columns" = Table.RenameColumns(#"Filtered Rows",{{"CODE.1", "Letter Code"}}),
        #"Reordered Columns" = Table.ReorderColumns(#"Renamed Columns",{"Code", "Letter Code", "Category", "Name", "Revenue"})
    in
        
    #"Reordered Columns" 
    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

  7. #7
    Forum Contributor
    Join Date
    03-05-2018
    Location
    Tbilisi, Georgia
    MS-Off Ver
    2013, 2016
    Posts
    271

    Re: Power Query: transform a table with sub-items.

    Great! Works as expected! Thank you Alan.

  8. #8
    Forum Expert
    Join Date
    12-09-2014
    Location
    Trakai, Lithuania
    MS-Off Ver
    2016
    Posts
    1,287

    Re: Power Query: transform a table with sub-items.

    Another option
    Please Login or Register  to view this content.
    Attached Files Attached Files

  9. #9
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,211

    Re: Power Query: transform a table with sub-items.

    Please try

    PHP Code: 
    let
        Source 
    Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        
    Group Table.Group(Source"Name", {{"T"each (_)}},0,(b,e)=> Number.From(Text.StartsWith(e,"Cate")) ),
        
    AddCol Table.AddColumn(Group"TA"each Table.AddColumn(Table.AddColumn(Table.RemoveFirstN([T]),"Letter Code",(x)=> [T][Code]{0}),"Catagory",(x)=> [T][Name]{0}) ),
        
    Combine Table.Combine(AddCol[TA]),
        
    Reorder Table.ReorderColumns(Combine,{"Code""Letter Code""Catagory""Name""Revenue"})
    in
        Reorder 
    Attached Files Attached Files

  10. #10
    Forum Contributor
    Join Date
    03-05-2018
    Location
    Tbilisi, Georgia
    MS-Off Ver
    2013, 2016
    Posts
    271

    Re: Power Query: transform a table with sub-items.

    Czeslaw Thank you, all works!

    Bo_Ry Thank you, but somehow I have a problems with Query (pls. refer to a screenshot, and as I suppose this is due to my old Excel 2013)
    When I press "Go to Error" it leads me to AddCol step. Update: Works on Excel 2016. Looks like a compatibility issue.
    screen.png

    P.S. Also, I want to thank you for your formula options. Better then mine
    Last edited by T.I.; 07-04-2022 at 08:19 AM.

+ 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. Replies: 0
    Last Post: 04-12-2022, 01:58 PM
  2. [SOLVED] Transform data in Power Query
    By no_Fate in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-19-2022, 06:25 AM
  3. [SOLVED] Power Query to transform current table
    By Vixx1 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-16-2020, 12:28 PM
  4. Power query - transform help
    By N3CREN in forum Excel General
    Replies: 0
    Last Post: 10-23-2020, 09:01 AM
  5. Incremental refresh in Power Query / Get & Transform
    By SHI.NL in forum Excel General
    Replies: 1
    Last Post: 09-17-2019, 05:34 AM
  6. [SOLVED] Power Query transform help
    By ierosadopr in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-09-2019, 05:25 AM
  7. Power Query (Get & Transform) on Mac
    By SHI.NL in forum Excel General
    Replies: 2
    Last Post: 05-18-2018, 09:24 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