Forum Discussion
Sylbaryn
2 years agoNew Member
How can I split a column of data into 2 columns, based on the value of another column
Hi. My data only has two columns. A Product Number and a Customer Number. Each product must have either one or two associated customers and a customer can have as many products as they want. My aim ...
- 2 years ago
You can use the Table.Group function. The code below assumes that there is one or two customers per Product. If there might be more, the code can be rewritten to handle any number of customers.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQAAiUdJVMDpVgdJK4hnGsI4hrBuUYgrjEq1wTONQZxTVG5ZnCuCSrXFNUoU6hRsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Product Num..." = _t, #"Customer Nu..." = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Product Num...", Int64.Type}, {"Customer Nu...", Int64.Type}}), #"Grouped Rows" = Table.Group(#"Changed Type", {"Product Num..."}, { {"Customer 1", each [#"Customer Nu..."]{0}, type nullable text}, {"Customer 2", each try [#"Customer Nu..."]{1} otherwise null, type nullable text} }) in #"Grouped Rows" - 2 years ago
Thank you so much. I can pretend to understand at first glance but I'll be delving into your suggestion. Thanks again
ronrsnfld
Super User
2 years agoYou can use the Table.Group function. The code below assumes that there is one or two customers per Product. If there might be more, the code can be rewritten to handle any number of customers.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQAAiUdJVMDpVgdJK4hnGsI4hrBuUYgrjEq1wTONQZxTVG5ZnCuCSrXFNUoU6hRsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Product Num..." = _t, #"Customer Nu..." = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Product Num...", Int64.Type}, {"Customer Nu...", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Product Num..."}, {
{"Customer 1", each [#"Customer Nu..."]{0}, type nullable text},
{"Customer 2", each try [#"Customer Nu..."]{1} otherwise null, type nullable text}
})
in
#"Grouped Rows"
Sylbaryn
2 years agoNew Member
Thank you so much. I can pretend to understand at first glance but I'll be delving into your suggestion. Thanks again