Forum Discussion
Concatenate one column per ID
I have a table with multiple IDs and each ID is repeated with another column [Stage] values.
| ID | Stage |
| 1234 | Sent |
| 1234 | Created |
| 1234 | Waiting |
| 1234 | Waiting |
| 9874 | Waiting |
| 9874 | Sent |
| 6542 | Offer |
| 6542 | Waiting |
| 6542 | Sent |
I want to create a column that is a concatenation of stages that occur per ID - but I only care about "Sent" and "Waiting" and "Offer". If other stages occur, leave the custom column blank
| ID | Stage |
| 1234 | Sent, Waiting |
| 1234 | |
| 1234 | Sent, Waiting |
| 1234 | Sent, Waiting |
| 9874 | Sent, Waiting |
| 9874 | Sent, Waiting |
| 6542 | Sent, Waiting, Offer |
| 6542 | Sent, Waiting, Offer |
| 6542 | Sent, Waiting, Offer |
let Source = Table.FromRows( Json.Document( Binary.Decompress( Binary.FromText( "i45WMjQyNlHSUQpOzStRitWB852LUhNLUlOQhcITM0sy89JxCVlamOMQgpttZmpiBOT7p6WlFiELIOuBCkH0xAIA", BinaryEncoding.Base64 ), Compression.Deflate ) ), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Stage = _t] ), #"Replaced Value" = Table.ReplaceValue( Source, each [Stage], (k) => let ld = List.Distinct( Table.SelectRows( Source, each [ID] = k[ID] and List.Contains({"Sent", "Waiting", "Offer"}, [Stage]) )[Stage] ) in if List.Contains(ld, k[Stage]) then Text.Combine(ld, ", ") else null, Replacer.ReplaceValue, {"Stage"} ) in #"Replaced Value"How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.
Hi deannapi, another solution:
Before
After
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNlHSUQpOzStRitWB852LUhNLUlOQhcITM0sy89JxCVlamOMQgpttZmpiBOT7p6WlFiELIOuBCkH0xAIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Stage = _t]), Stages = List.Buffer({"Sent", "Waiting", "Offer"}), GroupedRows = Table.Group(Source, {"ID"}, {{"All", each [ a = List.Intersect({Stages, [Stage]}, Comparer.OrdinalIgnoreCase), b = Text.Combine(a, ", "), c = Table.ReplaceValue(_, each [Stage], each if List.Contains(a, [Stage]) then b else null, Replacer.ReplaceValue, {"Stage"}) ][c], type table}}), CombinedAll = Table.Combine(GroupedRows[All]) in CombinedAll
2 Replies
- lbendlinSuper User
let Source = Table.FromRows( Json.Document( Binary.Decompress( Binary.FromText( "i45WMjQyNlHSUQpOzStRitWB852LUhNLUlOQhcITM0sy89JxCVlamOMQgpttZmpiBOT7p6WlFiELIOuBCkH0xAIA", BinaryEncoding.Base64 ), Compression.Deflate ) ), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Stage = _t] ), #"Replaced Value" = Table.ReplaceValue( Source, each [Stage], (k) => let ld = List.Distinct( Table.SelectRows( Source, each [ID] = k[ID] and List.Contains({"Sent", "Waiting", "Offer"}, [Stage]) )[Stage] ) in if List.Contains(ld, k[Stage]) then Text.Combine(ld, ", ") else null, Replacer.ReplaceValue, {"Stage"} ) in #"Replaced Value"How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done". Once you examined the code, replace the Source step with your own source.
- dufoq3Community Champion
Hi deannapi, another solution:
Before
After
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNlHSUQpOzStRitWB852LUhNLUlOQhcITM0sy89JxCVlamOMQgpttZmpiBOT7p6WlFiELIOuBCkH0xAIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Stage = _t]), Stages = List.Buffer({"Sent", "Waiting", "Offer"}), GroupedRows = Table.Group(Source, {"ID"}, {{"All", each [ a = List.Intersect({Stages, [Stage]}, Comparer.OrdinalIgnoreCase), b = Text.Combine(a, ", "), c = Table.ReplaceValue(_, each [Stage], each if List.Contains(a, [Stage]) then b else null, Replacer.ReplaceValue, {"Stage"}) ][c], type table}}), CombinedAll = Table.Combine(GroupedRows[All]) in CombinedAll