Forum Discussion

nc1985's avatar
nc1985
New Member
9 months ago
Solved

How to expand column of mixed data

Hi, I have this column with records, lists and empty cells.  I can't expand the column like I could if it was just all lists or all records.  So how do I get the data out of the lists and records? ...
  • MarkLaf's avatar
    9 months ago

    I'm guessing that the lists are lists of records of same structure as the records at top level of the column? If yes, then the trick is to 1) remove blanks, 2) wrap single records into a 1-item list, 3) combine all lists of records into a single list of records, 4) convert your list of records into a table with Table.FromRecords.

     

    I'll walk through an example.

     

    Let's say we are starting with something like the below where we have a mix of single and lists of records of general structure [Field1=text,Field2=text] :

    Table.FromColumns(
        {{
            [Field1="a",Field2="b"],
            [Field1="c",Field2="d"],
            {
                [Field1="e",Field2="f"],
                [Field1="g",Field2="h"]
            },
            [Field1="i",Field2="j"],
            "",
            "",
            [Field1="k",Field2="l"]
        }}, 
        type table [Answers=any]
    )

     

    Sample

     

    The below M code will perform the steps I outlined at top:

     

    let
        Source = Sample,
        RemoveText = List.Select( Source[Answers], each not ( _ is text ) ),
        WrapSingleRecords = List.Transform( RemoveText, each if _ is list then _ else {_} ),
        CombineRecordLists = List.Combine( WrapSingleRecords ),
        ToTable = Table.FromRecords( CombineRecordLists, type table [ Field1=text, Field2=text ] )
    in
        ToTable

     

     

    Notes:

    1) If the lists are totally different kind of data, then you'll need to think about and articulate what is the common info if any that you want to extract from the lists/records and then perform conditional transform (eg: if _ is record then <record-specific transform> else if _ is list then <list-specific transform> else null/fallback)

     

    2) If you are dealing with records inside the lists but the records are not uniform (ie different fields are present) then I'd recommend using the following as an alternative in the ToTable step. This will give you a record column that you can then expand (can use "Load more" button in expand UI to explore all fields)

     

    ToTable = Table.FromColumns( { CombineRecordLists }, type table [Answers=record] )​