Forum Discussion
How to expand column of mixed data
- 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 ToTableNotes:
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] )
Mauro89 thanks you for info....
nc1985 :- Here the one more opiton we can explore to do this
Step 1: Add a Custom Column
Go to:
Add Column → Custom Column
Use this M code:
= let
v = [Answers]
in
if v = null then #table({}, {}) // empty table
else if Value.Is(v, type record) then Record.ToTable(v)
else if Value.Is(v, type list) then Table.FromList(v, Splitter.SplitByNothing(), {"Value"})
else #table({}, {}) // fallback
This converts every row to a table object.
Step 2: Expand the new column
After adding the column:
You’ll see a new column with Table values in every row.
Click the expand icon (double-arrow).
Expand into columns.