Hi @marcoercolani,
Usually the technique for transforming a table with multiple headers is:
- Make sure all headers are in the rows section of the table, not in the header.
- Transpose table
- Select all header columns, Transform → “Merge columns” with a delimiter
- Transpose the table back
- Use the first row as header
- Select all label (or “row header”) columns,
- Transform → Unpivot columns → Unpivot other columns
- Split “Attribute” column by delimiter
- Set appropriate data types and Rename columns
.
Paste this script into a new blank query, to see the result for your supplied sample.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("xZBBa8MwDIX/ysi5h8ZL3OTawqBsg9KWXUoOSuMVQ2oP1Tm0v75+xmGk0CW3QfSeIjmf5BwOSTL7fdbmm+niuDu6jtV/FarZcKslW2pqMs3EfM9kLj+W3aT8cdqKXDowMbAnPVBW2l39+1adtDVo2M44Rmmnb7jn+2b9kkYX0V+jZ9Hz6DL6InoRRnzqloz1pQ97rokbTfh9jlpMWeqTVwliAckhAkCJqaLwgjEiC6wvZfRR/c3yAUgZmKLPAliWoQnS1p5HOGEszkPkHDQfKXYrkRV5AL21lpUZWSrDV4t5L0CXWDTFJUPIpKru", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t, Column6 = _t, Column7 = _t, Column8 = _t, Column9 = _t, Column10 = _t, Column11 = _t, Column12 = _t]),
TransposeTable = Table.Transpose(Source),
MergeColumns = Table.CombineColumns(TransposeTable,{"Column1", "Column2", "Column3", "Column4"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged"),
TransposeBack = Table.Transpose(MergeColumns),
CleanHeaderNames = Table.ReplaceValue(TransposeBack,";;;","",Replacer.ReplaceText,{"Column1", "Column2", "Column3", "Column4"}),
PromoteHeaders = Table.PromoteHeaders(CleanHeaderNames, [PromoteAllScalars=true]),
UnpivotOtherColumns = Table.UnpivotOtherColumns(PromoteHeaders, {"City", "Region", "Country", "Size"}, "Level", "Value"),
SplitByDelimiter = Table.SplitColumn(UnpivotOtherColumns, "Level", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Level.1", "Level.2", "Level.3", "Level.4"})
in
SplitByDelimiter
I hope this is helpful