A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
This is a very simplified version of this thread. We have an Input Table that looks like:
where each Group of colored columns needs to be transformed in a separate Table (so 3 in this case) and at the end we want the 3 Tables to be Appended (Combined according to Power Query M language)
Challenge
The number of Groups may vary (i.e. from 1 to say 10) from time to time. So we need a query that is flexible enough to handle this variation
Note
In the above example the 1st Group has 3 columns, the 2nd has 4 and the last has 5. To properly Append/Combine Tables they all must have the same number of columns. That aspect of the problem is not detailed in the below Power Query M snipet.
How To
Power Quey M language doesn't offer an easy way to loop. This example uses the List.Generate function to do that the appropriate number of times to build each Table and then Append/Combine them
let
...
GroupsList = Record.ToList(Table.FirstN(Source, 1){0}),
NbGroups = List.Count(List.Distinct(GroupsList)),
// Loop (actually NbGroups of times) to create a Table for each "Group"
// At the end we get a List that contains Table(s)
The next Step will Append/Combine all Table(s)
TablesToAppend = List.Generate(
()=> [i = -1, j = 0, OutputTable = #table({"Col","Index","Index2"},{})],
each [i] < NbGroups,
each
[
j = [j] +1,
..., // step1 to build the OutputTable
..., // step2 to build the OutputTable
TableIdx = ..., // step3 to build the OutputTable
OutputTable = Table.AddIndexColumn(TableIdx, "Index2", j, 1),
i = [i] +1
],
each [OutputTable]
),
// Append all tables from above List
TablesAppended = Table.Combine(List.Transform(TablesToAppend, each (_))),
...
in
...