Power Query - Loop through unknown number of Tables and append them all - solved

Anonymous
2018-07-17T09:29:18+00:00

Hello,

I have an Excel spreadsheet with a lot of columns but I want to save it in csv format with line breaks to put certain columns on different lines.   This is because I can upload different record types into my database but each record type needs its own line in csv with a header type as the first field on that line.

eg this in Excel:

col1   col2   col3   col4   col5   col6   col7   col8   col9   col10

01        b        c        d       e        02         g       h        03         j

01        r         s         t       u        02         v       w        03         x

becomes this in csv:

01, b, c, d, e

02, g, h

03, j

01, r, s, t, u

02, v, w

03, x              

Is this possible?  Easy?  

thanks

Louise

<The thread has been moved to the correct category by forum moderator>

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
Lz365 38,201 Reputation points Volunteer Moderator
2018-08-01T12:23:56+00:00

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

    ...

Was this answer helpful?

2 people found this answer helpful.
0 comments No comments

59 additional answers

Sort by: Most helpful
  1. Lz365 38,201 Reputation points Volunteer Moderator
    2018-07-26T08:58:58+00:00

    Good morning Louise

    Just opened your Word doc. OK, it shows the Input file path as X:.... but I Don't see any error message anywhere on your screenshot :-((

    Without an error message I Don't see I can guide you further. So, could you do the same thing as yesterday with your Input file located on X:\…., click on the Refresh Query… button and drop me in a way or another the error message you get

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2018-07-26T07:51:57+00:00

    Morning,

    Haha, you and me both!  ;0)  I just noticed that my screen shot of the error message didn't paste so here's a link to it.

    https://www.dropbox.com/s/zi0ew0dni8fik1v/Output%20error%20message.docx?dl=0

    I've tried it with the input file open and closed (start with the simple stuff eh!) but have no idea why it isn't working.  I've saved both docs on my drive and you can see from the screen shot I changed the location of the input file where you said.  Not sure what else to try.

    Was this answer helpful?

    0 comments No comments
  3. Lz365 38,201 Reputation points Volunteer Moderator
    2018-07-25T17:20:35+00:00

    Hi Louise

    I am completly lost. Download your file, saw the extra 4 rows you mentioned with a 2 at the end of the Last Name.

    I did the same: copied 4 rows at the bottom, changed the 2 (at the end of the Last Name) with a 3, extended the table by 4 rows and saved the file

    Back in the Output.xslm file I clicked on the Refresh query… button and everything went well (no error) and as far as I can say the output is fine

    So what is this issue?

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2018-07-25T14:50:56+00:00

    Hello,

    I use dropbox so here is my link https://www.dropbox.com/s/zg28i5pjxq8dzh2/INPUT\_File\_For\_CSV\_Export.xlsx?dl=0.  I just copied four lines from the original and added a '2' to the surname.  These rows weren't stripey at first but I went into the table and extended it to include these rows, so that bit seems fine.

    Thanks

    Was this answer helpful?

    0 comments No comments