Hi @Iwona,
Okay so you’ll need the grouped version, I’ve adjusted the ‘grouping’.
Paste this code into a new blank query.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("xVpNb9wgEP0vPkeEGcA21ypSLlUrpR9qFeXYcw/9/1KBwfaAnbIG1pWyy84mCxu/N2+Gh19fh6fhYQD3QCn9APCo4BElShd8AP/Wp8/u6emFfu1fftvi9efnrz/+T3+///T28Dp8jFN8dY/nH4j+tTQCJhpxCu/7YBIaQvD88sXHUkjJ1zv4WRbAbQEz+s/OQuk4zssCgAL4Aj4eb1jgu58o/hejkKWP/HsSpZR/JYxqnMaEaaSumoYxAC9nwEjI68gEAytAUqgODEBknwVYKBZB10LrLLYnSTALwD4kqOMSQ09djh5G9GL+mjV/3ZUO6dSav5ajp4RMUtZQ/rEY4IY1OHqTUCXAb0EPhDaN04RLKGTdt2Ek0HsSjHclgYF3SeBIje0kQD0kKWySFHag6yw+n8JBFdpJIPuQAOq+DSOBuZwEOpLARhLYhQTSEiCtSjBxEsRrxHQ7VvZNx0slda8ElQK80/G5Fb3xcvTCxR0JKDfiip670oFMrejBkKSw5LLgUhYzXcezrZirwqWvdSN6zbk3XY7eTKipiJ7m6CVXthY9laKnMvSMzOKSDO7Ra1ZOEuBi/S9NE6tw3TSMBPPlJAgYTTGFJ57CrulVHUgwchJg2t7BSFssHv+nKtxhNxVJUEdJRgK7J8Gd+3EdwZ9oVLwf17xi1rZiSRVGoRMlyPbsPj6r41Zg3SZ2R4IerRi0b6lBXk4CIPDDtXejyWyOHjwwSUuuaWTJLzMxKG5r9t1Yl3ruKkKdGDAAD1yxO0u5iQBGKTdcyk2HXTUm3RhmG6ppWcMHc9ni2qdw0Ui7DbrKKrxL4Tp7hjHgelcsyKy79opGUCsgmHZftQxIOjqVuWJz6rz5+DwJVN026E46Dq2bMjgw1+4sA5bAD1i4UW/WKPaRAZOSwGQkQDNsMqDO9vSW2NvKgE4yIMvGbpEBB87anWXARAZEOdBbO6fIcGwt4/OQlHHDjRqwVINpQdeY3bJA1sv18VahtSUkDcBmDTiw1e7MgFgAdGSC2VJSdTHY0+MRTeYVZ8C2oCVBOMOAudYM27nrrUcskQGtWzq43poLAFkCSCYpqcgm62rNmcU4WBmgsliXknFPAt2q4J4E2F5PIgmaC8H1Dp8m8KkVsNSOrZioRRY6nrIYUstlTnT1U2bx+YagCw9AqD48MM08uN7kCytKatf9AXliv+g+p22Jz2fSiuBw3/YiLsCzpywzmcXtJGi+8yJ2ha1OL1xv8o0R/EgGzc1dB1gSdzF7x/QuCYe7MsNeHM7wwLa39X13B63HPnjg811gEy2OkNsSpvdGjF1OXVO/d059PgTy9Vhc7NQP7r/oc/TeXFui6V8hK29/AQ==", 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, Column13 = _t, Column14 = _t, Column15 = _t, Column16 = _t, Column17 = _t, Column18 = _t, Column19 = _t, Column20 = _t]),
AddedGroup = Table.FillDown( Table.ReplaceValue( Table.AddIndexColumn(Source, "Group", 1, 1), each [Group], each if [Column1] = "D" then [Index] else null,Replacer.ReplaceValue,{"Group"}), {"Group"}),
GroupRows = Table.Group(AddedGroup, {"Group"}, {{"Grouped", each Table.RemoveColumns(_, {"Group"} ), type table [Column2=nullable text, Column3=nullable text, Column4=nullable text, Column5=nullable text, Column6=nullable text, Column7=nullable text, Column8=nullable text, Column9=nullable text, Column10=nullable text, Column11=nullable text, Column12=nullable text, Column13=nullable text, Column14=nullable text, Column15=nullable text, Column16=nullable text, Column17=nullable text, Column18=nullable text, Column19=nullable text, Column20=nullable text]}}),
GetLevels = Table.AddColumn(GroupRows, "AllRows", each Table.Group( [Grouped], {"Column1"}, {{"AllRows", each Table.RemoveColumns( _, "Column1"), type table [Column1=nullable text, Column2=nullable text, Column3=nullable text, Column4=nullable text, Column5=nullable text, Column6=nullable text, Column7=nullable text, Column8=nullable text, Column9=nullable text, Column10=nullable text, Column11=nullable text, Column12=nullable text, Column13=nullable text, Column14=nullable text, Column15=nullable text, Column16=nullable text, Column17=nullable text, Column18=nullable text, Column19=nullable text, Column20=nullable text]}})),
Result = Table.AddColumn( GetLevels, "nTable", each
let
AddIndex = Table.AddIndexColumn([AllRows], "Index", 0, 1, Int64.Type),
AddColCount = Table.AddColumn(AddIndex, "ColCount", each if [Column1] = "D" then Table.ColumnCount( [AllRows] ) else List.Count(List.RemoveMatchingItems( Record.ToList( [AllRows]{0}? ), {""} ) ), type number),
lColCount = List.Buffer(AddColCount[ColCount]),
UpdatedTables = Table.AddColumn(AddColCount, "Temp", each
let
StartNum = if [Column1] = "D" then 1 else List.Sum( List.FirstN( lColCount, [Index] )),
OldNames = List.Transform( List.Numbers( 2, [ColCount] ), each "Column" & Text.From(_) ),
NewNames = List.Transform( List.Numbers( if [Column1] = "D" then 1 else StartNum +1, [ColCount] ), each "Column" & Text.From(_)),
nTable = Table.RenameColumns( Table.SelectColumns( [AllRows], OldNames), List.Zip( {OldNames, NewNames } ))
in
nTable
)[[Index], [Temp]],
Pivot = Table.Buffer( Table.Pivot(Table.TransformColumnTypes(UpdatedTables, {{"Index", type text}}), List.Distinct(Table.TransformColumnTypes(UpdatedTables, {{"Index", type text}})[Index]), "Index", "Temp")),
Result = List.LastN(
List.Generate(
()=> [ t = Table.ExpandTableColumn(Pivot, Text.From(n), Table.ColumnNames( Record.Field( Pivot{0}, Text.From(n) )), Table.ColumnNames( Record.Field( Pivot{0}, Text.From(n) ))), n = 0],
each [ n ] < Table.ColumnCount( Pivot ),
each [ t = Table.ExpandTableColumn( [t], Text.From(n), Table.ColumnNames( Record.Field( Pivot{0}, Text.From(n) )), Table.ColumnNames( Record.Field( Pivot{0}, Text.From(n) ))), n = [n]+1 ],
each [ t ]
), 1){0}
in
Result
)[[nTable]],
ExpandnTable = Table.ExpandTableColumn(Result, "nTable", List.Distinct( List.Combine( List.Transform( Result[nTable], each Table.ColumnNames( _ )))), List.Distinct( List.Combine( List.Transform( Result[nTable], each Table.ColumnNames( _ )))) )
in
ExpandnTable
I hope this is helpful