Forum Discussion
Split a cell values in a column to multiple columns by value
- 9 years ago
A solution in Power Query would be to:
- add an Index column,
- split the Q column in a new column,
- expand this new column,
- add a new column with prefix "V",
- pivot (I included an exotic sort (on the numeric number part of the prefixed value),
just because it can and because it is Saturday today), - sort back to the original sort
- remove the Index column
This video takes you through all the steps:
let Source = Table1, Indexed = Table.AddIndexColumn(Source, "Index", 0, 1), Splitted = Table.AddColumn(Indexed, "Splitted", each Text.Split([Q], " ")), Expanded = Table.ExpandListColumn(Splitted, "Splitted"), Prefixed = Table.AddColumn(Expanded, "Inserted Prefix", each "V" & [Splitted], type text), Pivoted = Table.Pivot(Prefixed, List.Sort(List.Distinct(Prefixed[#"Inserted Prefix"]), (x,y) => Value.Compare(Number.From(Text.Middle(x,1)), Number.From(Text.Middle(y,1)))), "Inserted Prefix", "Splitted"), OriginalSort = Table.Sort(Pivoted,{{"Index", Order.Ascending}}), RemovedIndex = Table.RemoveColumns(OriginalSort,{"Index"}) in RemovedIndex - 9 years ago
"I have 2 questions" followed by a list of 5...
Anyhow, if you have mixed data, then I would suggest to sort the data before pivotting.
Now the pivot step looks like:
and the entire query code:
let Source = Table1, Indexed = Table.AddIndexColumn(Source, "Index", 0, 1), Splitted = Table.AddColumn(Indexed, "Splitted", each Text.Split([Q], " ")), Expanded = Table.ExpandListColumn(Splitted, "Splitted"), AddedSortColumn = Table.Buffer(Table.AddColumn(Expanded, "SortColumn", each try Number.From([Splitted]) otherwise [Splitted])), Sorted = Table.Sort(AddedSortColumn,{{"SortColumn", Order.Ascending}}), RemovedSortColumn = Table.RemoveColumns(Sorted,{"SortColumn"}), Prefixed = Table.AddColumn(RemovedSortColumn, "Inserted Prefix", each "V" & [Splitted], type text), Pivoted = Table.Pivot(Prefixed, List.Distinct(Prefixed[#"Inserted Prefix"]), "Inserted Prefix", "Splitted"), OriginalSort = Table.Sort(Pivoted,{{"Index", Order.Ascending}}), RemovedIndex = Table.RemoveColumns(OriginalSort,{"Index"}) in RemovedIndexThe solution is dynamic as it adds all required columns, also if future data require more/less columns.
So with your latest example added, I got the following result with column "Vother" automatically added:
Hi mahmoud
How many options will you have? Your sample data only shows numbers up to 12. If that is the case, you could hand build DAX formulas to create calculated columns and only populated if it finds a key value in the Q column.
Something along the lines of
V12 =
Var myVal = "12"
RETURN IF(FIND(" " & myVal & " "," " & 'Table1'[Q] & " ",,0)>0,myVal,blank())which you can repeat
A solution in Power Query would be to:
- add an Index column,
- split the Q column in a new column,
- expand this new column,
- add a new column with prefix "V",
- pivot (I included an exotic sort (on the numeric number part of the prefixed value),
just because it can and because it is Saturday today), - sort back to the original sort
- remove the Index column
This video takes you through all the steps:
let
Source = Table1,
Indexed = Table.AddIndexColumn(Source, "Index", 0, 1),
Splitted = Table.AddColumn(Indexed, "Splitted", each Text.Split([Q], " ")),
Expanded = Table.ExpandListColumn(Splitted, "Splitted"),
Prefixed = Table.AddColumn(Expanded, "Inserted Prefix", each "V" & [Splitted], type text),
Pivoted = Table.Pivot(Prefixed,
List.Sort(List.Distinct(Prefixed[#"Inserted Prefix"]),
(x,y) => Value.Compare(Number.From(Text.Middle(x,1)),
Number.From(Text.Middle(y,1)))),
"Inserted Prefix",
"Splitted"),
OriginalSort = Table.Sort(Pivoted,{{"Index", Order.Ascending}}),
RemovedIndex = Table.RemoveColumns(OriginalSort,{"Index"})
in
RemovedIndex