+ Reply to Thread
Results 1 to 5 of 5

PQ Trim table down to one row per identity

  1. #1
    Forum Contributor
    Join Date
    04-30-2010
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    215

    PQ Trim table down to one row per identity

    Hello all

    I have a table with identities, that have different accounts with different attributes on different applications. Some identities also have more than one account on the same application.

    I have made a demo setup and it looks like this:
    OneRowPerIdentity1.png

    So for instance, it shows that Identity1 has 2 accounts on the AppX application, which has 3 attributes and that those attributes have different values on the 2 accounts. Please note, that the attributes are also named differently on the applications in my actual setup, but for clarity, I have called them Att1, Att2 and Att3 on all applications in this demo.

    It is generic how many accounts an identity might have on an application - some applications might have identities that have 3 accounts, while other applications might only have 1 or 2 account per identity.

    What I would like to do, is to trim this down to one row per identity, so all the applications and attributes for each identity are shown horizontally, like this:
    OneRowPerIdentity2.png

    To make this generic, it will have to make columns for each attribute times the max number of accounts any identity has on that given application, for instance indicated by a prefixed number 1. and 2. etc. (eg 2.AppX.AccountID and 2.AppX.Att1, for the second account). I hope this makes sense

    I have tried to mess around with this in powerquery for a while now with grouping and pivoting etc., but I simply cant get it to work, and I would really appreciate any help with this.

    Thanks in advance and best regards
    Imbizile
    Attached Files Attached Files

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

    Re: PQ Trim table down to one row per identity

    Please try

    PHP Code: 
    let
        Source 
    Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
        
    GroupIDId Table.Group(Source, {"Identity""AccountID"}, {"R"each 
            Record
    .FromList({[AccountID]{0}}&[AccountAttributesValue],  List.Transform({"AccountID"}&[AccountAttributes], 
            (
    f)=> Text.Combine({Text.End([AccountID]{0},1),[Application]{0},f},"." ))  ) }),
        
    GroupIden Table.Group(GroupIDId"Identity", {"R"each Record.Combine([R]) }),
        
    Expand Table.ExpandRecordColumn(GroupIden"R", List.Sort(Record.FieldNames(Record.Combine(GroupIden[R])),each let t Text.Split(_,"."in t{1}&t{0}&t{2} ))
    in
        Expand 
    Attached Files Attached Files
    Last edited by Bo_Ry; 02-12-2023 at 03:33 PM.

  3. #3
    Forum Contributor
    Join Date
    04-30-2010
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    215

    Re: PQ Trim table down to one row per identity

    Thanks so much for your help Bo_Ry - your powerquery skills are truly incredible!!

    But I just tried to add a new application to Identity3 called NewSystem with 4 attributes:
    OneRowPerIdentity3.png

    Everything works great, except for the numbering of accounts. I would expect to get the columnnames respectively prefixed with 1. and 2. (since the max number of accounts any identity has on this application is 2), but instead it gets prefixed with a. and d.:
    OneRowPerIdentity4.png

    I might have been unclear about the names in the demo, but I only made them like AppY and 1Y2 etc. in order to make the setup understandable. In my actual setup I have 50.000+ identities on 15 different applications and some applications have 50+ different attribute and there is no strict namingconvention across those

    Would really appreciate help with the prefixing issue

    Best regards
    Imbizile
    Attached Files Attached Files

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

    Re: PQ Trim table down to one row per identity

    Please try

    PHP Code: 
    let
        Source 
    Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
        
    GroupIDId Table.Group(Source,{"Identity","Application"},{"R",(T)=> let t Table.AddIndexColumn(Table.Group(T,"AccountID",{"A"each _} ),"N",1in 
            Record
    .Combine(Table.TransformRows(t, (u)=> Record.FromList({u[AccountID]}& u[A][AccountAttributesValue], 
                List.
    Transform({"AccountID"}&u[A][AccountAttributes], each  Text.From(u[N])&"."&T[Application]{0} &"."&)) )) }),
        
    GroupIden Table.Sort(Table.Group(GroupIDId"Identity", {"R"each Record.Combine([R]) }),{"Identity",0}),
        
    Res Table.ExpandRecordColumn(GroupIden"R", List.Sort(Record.FieldNames(Record.Combine(GroupIden[R])),each let t Text.Split(_,"."in t{1}&t{0}&t{2} ))
    in
        Res 
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    04-30-2010
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    215

    Re: PQ Trim table down to one row per identity

    Thanks a bunch for your help Bo_Ry!!

    I have tried to apply your code to my actual data, which contains a table with more than 2.5 million rows and it works perfectly. Though some attributes are multivalued (ie different values on more rows for the same attribute) and I do have to filter those out first in order for it to work for now. I will need to combine those multivalued attributes rows into one row where it is listed with a delimiter. But you have previously helped me with something similiar in this thread https://www.excelforum.com/excel-gen...ts-column.html and I will see if I can apply that solution into this somehow.

    So again, thank you very much for your effort

+ 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. Expand / Trim Table based on Pivot Table size adjacent
    By NewYears1978 in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 08-11-2022, 02:07 AM
  2. [SOLVED] trim to match two sets of input-trim spaces around a character
    By nigelog in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-21-2017, 12:33 PM
  3. VLOOKUP + TRIM, TRIM Lookup table array...
    By mangesh.mehendale in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 10-19-2016, 02:10 AM
  4. identity issue
    By Look, More, What in forum Excel General
    Replies: 3
    Last Post: 04-10-2015, 10:54 AM
  5. UK Identity Questions
    By Mordred in forum The Water Cooler
    Replies: 23
    Last Post: 08-03-2011, 03:38 PM
  6. Trying to trim a trailing space char and the Trim function isn't working
    By Psychochook in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-30-2007, 11:28 PM
  7. User identity
    By Tine in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-09-2006, 09:49 AM

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