Forum Discussion
nok
Advocate II
4 months agoSplit and rearrange columns
Hi! I have a table in Excel that follows this format:
| ID | Project | Cz[01/01/2026] | Cz[01/02/2026] | Cz[01/03/2026] | Fz[01/01/2026] | Fz[01/02/2026] | Fz[01/03/2026] | Fz[01/04/2026] | Fz[01/05/2026] | Fz[01/06/2026] | Fz[01/07/2026] | Fz[01/08/2026] | Az[01/01/2026] | Az[01/02/2026] |
| 1 | Project A | 300 | 400 | 500 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 55 | 63 |
| 2 | Project B | 45 | 57 | 91 | 22 | 13 | 124 | 120 | 759 | 375 | 1287 | 123 | 22 | 124 |
As you can see, the excel columns follow the format "Text[date]" for every possible date for Cz, Fz and Az values. I want to split the text and the date and generate different columns. The end result would be a table like this:
| ID | Project | Date | Cz | Fz | Az |
| 1 | Project A | 01/01/2026 | 300 | 99 | 55 |
| 1 | Project A | 01/02/2026 | 400 | 100 | 63 |
| 1 | Project A | 01/03/2026 | 500 | 101 | |
| 1 | Project A | 01/04/2026 | 102 | ||
| 1 | Project A | 01/05/2026 | 103 | ||
| 1 | Project A | 01/06/2026 | 104 | ||
| 1 | Project A | 01/07/2026 | 105 | ||
| 1 | Project A | 01/08/2026 | 106 | ||
| 1 | Project A | 01/09/2026 | |||
| 1 | Project A | 01/10/2026 | |||
| 1 | Project A | 01/11/2026 | |||
| 1 | Project A | 01/12/2026 | |||
| 2 | Project B | 01/01/2026 | 45 | 22 | 22 |
| 2 | Project B | 01/02/2026 | 57 | 13 | 124 |
| 2 | Project B | 01/03/2026 | 91 | 124 | |
| 2 | Project B | 01/04/2026 | 120 | ||
| 2 | Project B | 01/05/2026 | 759 | ||
| 2 | Project B | 01/06/2026 | 375 | ||
| 2 | Project B | 01/07/2026 | 1287 | ||
| 2 | Project B | 01/08/2026 | 123 | ||
| 2 | Project B | 01/09/2026 | |||
| 2 | Project B | 01/10/2026 | |||
| 2 | Project B | 01/11/2026 | |||
| 2 | Project B | 01/12/2026 |
How can I do this?
Hi nok ,
In power query do the following steps:
- Select the ID and Project Column
- Transform - Unpivot columns - Unpivot Others
- Select the attribute column
- Split Column by delimeter
- Use the [ as delimiter on the split
- On attribute 2 just replace the ] by blank
- Then rename columns and format has needed
- Select text column
- Pivot -> Value -> Don't aggregate
Check the full code below:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TY27DoAgDEV/hXRmgJaHjPoF7oTJuLiYGP8/tncgMhyaPs7tnSJ52p/7Oo/XrW4+7UoIygRmsDVFRBlDBBkUMIEZLHZkZREavhP/YjbT2ixXk5qI4YGGoWELqdkCpULJS8Unc103x/gA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"ID " = _t, #"Project " = _t, #"Cz[01/01/2026] " = _t, #"Cz[01/02/2026] " = _t, #"Cz[01/03/2026] " = _t, #"Fz[01/01/2026] " = _t, #"Fz[01/02/2026] " = _t, #"Fz[01/03/2026] " = _t, #"Fz[01/04/2026] " = _t, #"Fz[01/05/2026] " = _t, #"Fz[01/06/2026] " = _t, #"Fz[01/07/2026] " = _t, #"Fz[01/08/2026] " = _t, #"Az[01/01/2026] " = _t, #"Az[01/02/2026] " = _t]), #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"ID ", "Project "}, "Attribute", "Value"), #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByDelimiter("[", QuoteStyle.Csv), {"Attribute.1", "Attribute.2"}), #"Replaced Value" = Table.ReplaceValue(#"Split Column by Delimiter","]","",Replacer.ReplaceText,{"Attribute.2"}), #"Changed Type" = Table.TransformColumnTypes(#"Replaced Value",{{"Attribute.2", type date}}), #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Attribute.2", "Date"}, {"Attribute.1", "Text"}}), #"Pivoted Column" = Table.Pivot(#"Renamed Columns", List.Distinct(#"Renamed Columns"[Text]), "Text", "Value"), #"Changed Type1" = Table.TransformColumnTypes(#"Pivoted Column",{{"Cz", Int64.Type}, {"Fz", Int64.Type}, {"Az", Int64.Type}}) in #"Changed Type1"
2 Replies
- MFelix
Super User
Hi nok ,
In power query do the following steps:
- Select the ID and Project Column
- Transform - Unpivot columns - Unpivot Others
- Select the attribute column
- Split Column by delimeter
- Use the [ as delimiter on the split
- On attribute 2 just replace the ] by blank
- Then rename columns and format has needed
- Select text column
- Pivot -> Value -> Don't aggregate
Check the full code below:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("TY27DoAgDEV/hXRmgJaHjPoF7oTJuLiYGP8/tncgMhyaPs7tnSJ52p/7Oo/XrW4+7UoIygRmsDVFRBlDBBkUMIEZLHZkZREavhP/YjbT2ixXk5qI4YGGoWELqdkCpULJS8Unc103x/gA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"ID " = _t, #"Project " = _t, #"Cz[01/01/2026] " = _t, #"Cz[01/02/2026] " = _t, #"Cz[01/03/2026] " = _t, #"Fz[01/01/2026] " = _t, #"Fz[01/02/2026] " = _t, #"Fz[01/03/2026] " = _t, #"Fz[01/04/2026] " = _t, #"Fz[01/05/2026] " = _t, #"Fz[01/06/2026] " = _t, #"Fz[01/07/2026] " = _t, #"Fz[01/08/2026] " = _t, #"Az[01/01/2026] " = _t, #"Az[01/02/2026] " = _t]), #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"ID ", "Project "}, "Attribute", "Value"), #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByDelimiter("[", QuoteStyle.Csv), {"Attribute.1", "Attribute.2"}), #"Replaced Value" = Table.ReplaceValue(#"Split Column by Delimiter","]","",Replacer.ReplaceText,{"Attribute.2"}), #"Changed Type" = Table.TransformColumnTypes(#"Replaced Value",{{"Attribute.2", type date}}), #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Attribute.2", "Date"}, {"Attribute.1", "Text"}}), #"Pivoted Column" = Table.Pivot(#"Renamed Columns", List.Distinct(#"Renamed Columns"[Text]), "Text", "Value"), #"Changed Type1" = Table.TransformColumnTypes(#"Pivoted Column",{{"Cz", Int64.Type}, {"Fz", Int64.Type}, {"Az", Int64.Type}}) in #"Changed Type1" - Ashish_Mathur
Super User
Hi,
This M code works
let Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content], #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"ID ", "Project "}, "Attribute", "Value"), #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Other Columns", "Attribute", Splitter.SplitTextByDelimiter("[", QuoteStyle.Csv), {"Attribute.1", "Date"}), #"Replaced Value" = Table.ReplaceValue(#"Split Column by Delimiter","]","",Replacer.ReplaceText,{"Date"}), #"Changed Type" = Table.TransformColumnTypes(#"Replaced Value",{{"ID ", Int64.Type}, {"Project ", type text}, {"Attribute.1", type text}, {"Date", type date}, {"Value", Int64.Type}}), #"Pivoted Column" = Table.Pivot(#"Changed Type", List.Distinct(#"Changed Type"[Attribute.1]), "Attribute.1", "Value") in #"Pivoted Column"Hope this helps.