Forum Discussion
sashakorniakUK
Dec 03, 2020Copper Contributor
Excel formula function or macro? Data Migration for Horizontal Columns to Vertical Columns
Excel formula function or macro? Data Migration for Horizontal Columns to Vertical Columns This must be a common problem? I have old database exported to csv. The Old columns are; First...
SergeiBaklan
Dec 04, 2020Diamond Contributor
Same idea, but without hardcoding column names
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Grouped Rows" = Table.Group(
Source,
{"address"},
{
{"Tables",
each
[ #"Removed Columns" = Table.RemoveColumns(_,{"address"}),
#"Added Index" = Table.AddIndexColumn(
#"Removed Columns",
"Index", 1, 1, Int64.Type
),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(
#"Added Index",
{"Index"}, "Attribute", "Value"
),
#"Merged Columns" = Table.CombineColumns(
Table.TransformColumnTypes(
#"Unpivoted Other Columns",
{{"Index", type text}}, "en-GB"
),
{"Attribute", "Index"},
Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Merged"
),
#"Transposed Table1" = Table.Transpose(#"Merged Columns"),
#"Promoted Headers" = Table.PromoteHeaders(
#"Transposed Table1",
[PromoteAllScalars=true]
)
][#"Promoted Headers"]
},
{"Count", each Table.RowCount(_), Int64.Type}
}
),
// Generate Column Names
MaxRows = List.Max(#"Grouped Rows"[Count]),
RemoveAddress = Table.RemoveColumns(Source,{"address"}),
#"Kept First Rows" = Table.FirstN(RemoveAddress,1),
#"Added Custom" = Table.AddColumn(#"Kept First Rows", "Index", each {1..MaxRows}),
#"Expanded Index" = Table.ExpandListColumn(#"Added Custom", "Index"),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Expanded Index", {"Index"}, "Attribute", "Value"),
GetTogether = Table.CombineColumns(
Table.TransformColumnTypes(
#"Unpivoted Other Columns",
{{"Index", type text}}, "en-GB"),{"Attribute", "Index"},
Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"ColumnNames"
),
ColumnNames = GetTogether[ColumnNames],
#"Expanded Tables" = Table.ExpandTableColumn(#"Grouped Rows", "Tables", ColumnNames)
in
#"Expanded Tables"
sashakorniakUK
Dec 04, 2020Copper Contributor
SergeiBaklan - I cannot download the file?
testing the code shortly.
Any thoughts on the issue with if an address has more than 5 names?
- SergeiBaklanDec 04, 2020Diamond Contributor
1) I repeated the file
2) Nope, no problem if less than 5. They are if you add one more
- sashakorniakUKDec 04, 2020Copper ContributorSorry file does not download from me?
- SergeiBaklanDec 04, 2020Diamond Contributor
sashakorniakUK
Sorry for that, I was not able to download it back as well. One in previous post works with me.