Forum Discussion

ChrisMendoza's avatar
ChrisMendoza
Icon for Resident Rockstar rankResident Rockstar
8 years ago
Solved

Transpose/Transform Multiple Rows into Single Row

Hello,    First thank you all for the work you do. This is my first time posting to the community however, I've been using your posts as guides for awhile. Most recently how to merge multiple rows ...
  • ChrisMendoza's avatar
    ChrisMendoza
    8 years ago

    v-yulgu-msft, 

     

    Well it took a short 2 hour Saturday road trip for me to come up with the realization/solution.

     

    You're absolutely correct, Why do you need to "merge [Manager] & [Email] then splitting them"?. This is exactly what I was thinking too.

     

    Basically, I did not think to Table.Group( ) by more than one field.

     

    To bring closure to the question, sample data provided below:

     

    ManagerNameEmailDeptIDDeptDesc
    Duck,Daffy[email protected]D10001Daffy1Acct
    Duck,Daffy[email protected]D10001Daffy1Acct
    Duck,Daffy[email protected]D10001Daffy1Acct
    Duck,Daffy[email protected]D10001Daffy1Acct
    Duck,Daffy[email protected]D10001Daffy1Acct
    Duck,Daffy[email protected]D10001Daffy1Acct
    Duck,Daffy[email protected]D10002Daffy2Acct
    Duck,Daffy[email protected]D10002Daffy2Acct
    Duck,Daffy[email protected]D10002Daffy2Acct
    Mouse,Minnie[email protected]D90001Minnie1Acct
    Mouse,Minnie[email protected]D90002Minnie2Acct
    Mouse,Minnie[email protected]D90002Minnie2Acct
    Mouse,Minnie[email protected]D90002Minnie2Acct
    Mouse,Minnie[email protected]D90002Minnie2Acct
    Mouse,Minnie[email protected]D90002Minnie2Acct
    Mouse,Minnie[email protected]D90002Minnie2Acct
    Mouse,Mickey[email protected]D30001Mickey1Acct
    Mouse,Mickey[email protected]D30001Mickey1Acct
    Mouse,Mickey[email protected]D30001Mickey1Acct
    Mouse,Mickey[email protected]D30001Mickey1Acct
    Mouse,Mickey[email protected]D30002Mickey2Acct
    Mouse,Mickey[email protected]D30002Mickey2Acct
    Mouse,Mickey[email protected]D30002Mickey2Acct
    Mouse,Mickey[email protected]D30002Mickey2Acct
    Mouse,Mickey[email protected]D30002Mickey2Acct
    Mouse,Mickey[email protected]D30003Mickey3Acct
    McDuck,Scrooge[email protected]D60001Scrouge1Acct

     

     

    let
    
        Source = Excel.CurrentWorkbook(){[Name="MergeTable"]}[Content],
        #"Removed Duplicates" = Table.Distinct(Source),
        #"Changed Type" = Table.TransformColumnTypes(#"Removed Duplicates",{{"ManagerName", type text}, {"Email", type text}, {"DeptID", type text}, {"DeptDesc", type text}}),
        #"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type", "ManagerName", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"LastName", "FirstName"}),
        #"Merged Columns" = Table.CombineColumns(#"Split Column by Delimiter",{"FirstName", "LastName"},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"ManagerName"),
        #"Merged Columns1" = Table.CombineColumns(#"Merged Columns",{"DeptID", "DeptDesc"},Combiner.CombineTextByDelimiter(" - ", QuoteStyle.None),"DeptInfo"),
        // Function that adds an incrementing index number
        AddRanking = (table, column, newColumn) =>
            Table.AddIndexColumn(Table.Sort(table, {{column, Order.Ascending}}), newColumn, 1, 1),
        #"Grouped Rows" = Table.Group(#"Merged Columns1", {"ManagerName", "Email"}, {{"Data", each _, type table}}),
        Transformed = Table.TransformColumns(#"Grouped Rows", {{"Data", each AddRanking(_, "DeptInfo", "Rank")}}),
        #"Expand Data" = Table.ExpandTableColumn(Transformed, "Data", {"DeptInfo", "Rank"}, {"DeptInfo", "Rank"}),
        #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Expand Data", {{"Rank", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Expand Data", {{"Rank", type text}}, "en-US")[Rank]), "Rank", "DeptInfo")
    
    in
        #"Pivoted Column"

     

     

    Expected Table results for an MSWord Email Merge where each manager gets one email with a list of their department's.

     

    ManagerNameEmail123
    Daffy Duck[email protected]D10001 - Daffy1AcctD10002 - Daffy2Acct 
    Mickey Mouse[email protected]D30001 - Mickey1AcctD30002 - Mickey2AcctD30003 - Mickey3Acct
    Minnie Mouse[email protected]D90001 - Minnie1AcctD90002 - Minnie2Acct 
    Scrooge McDuck[email protected]D60001 - Scrouge1Acct