Forum Discussion
How to create table from list while specifying column name as well as its type
- 4 years ago
Hi Anonymous
Using Table.SelectColumns is still the simplest method.
In addition, to construct a single-column table with #table, it should be
let Source = #table( type table [ From = text ], { {"aa"}, {"bb"} } ) in SourceWhen you use RenameColumnsMappings[From] to extract the column, it returns a result like {"aa", "bb"}. While the expected result in #table() should be {{"aa"},{"bb"}}. So it didn't construct the table correctly.
To convert {"aa", "bb"} into {{"aa"},{"bb"}}, you can use List.Transform - PowerQuery M
List.Transform(#"Changed Type"[From], each {_})Full code
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSkxU0lGqqFCK1YlWSkoCsisrlWJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [From = _t, To = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"From", type text}, {"To", type text}}), Custom1 = List.Transform(#"Changed Type"[From], each {_}), newTable = #table( type table [ From = text ], Custom1 ) in newTableHope it helps
Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it. - 4 years ago
Hi Anonymous
Your question is valid, and you can indeed specify the column name and type within the Table.FromList function. Let me give you an example. The below code creates a single column table with the name "Product" and the data type Text.
= Table.FromList( {"Apple", "Prume" }, null, type table[Product=Text.Type] )You can achieve something similar with the methods:
- #table
- Table.FromRecords
- Table.FromColumns
- Table.FromRows
Hope this helps!
Rick
--------------------------------------------------
@ me in replies or I'll lose your thread
Master Power Query M? -> https://powerquery.how
Read in-depth articles? -> BI Gorilla
Youtube Channel: BI Gorilla
If this post helps, then please consider accepting it as the solution to help other members find it more quickly.
Hi Anonymous
Your question is valid, and you can indeed specify the column name and type within the Table.FromList function. Let me give you an example. The below code creates a single column table with the name "Product" and the data type Text.
= Table.FromList(
{"Apple", "Prume" },
null,
type table[Product=Text.Type]
)
You can achieve something similar with the methods:
- #table
- Table.FromRecords
- Table.FromColumns
- Table.FromRows
Hope this helps!
Rick
--------------------------------------------------
@ me in replies or I'll lose your thread
Master Power Query M? -> https://powerquery.how
Read in-depth articles? -> BI Gorilla
Youtube Channel: BI Gorilla
If this post helps, then please consider accepting it as the solution to help other members find it more quickly.
Thank you Rickmaurinus, this is exactly what I was looking for! It was interesting to see other possible ways how I can achieve the same result, as sugested by other contributors, but this is what I wanted to achieve in the first place. Thank you for showing me the correct syntax!
- Rickmaurinus4 years agoHelper V
No problem, if you really want to learn about all the details of creating tables, I just launched this blogpost:
Creating Tables in Power Query M (40+ Examples) - Gorilla BIIt goes into depth about the intricacies of each and what to watch out for. I had a lot of fun creating it, hope you enjoy absorbing it too!