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: Newest
  1. Lz365 38,201 Reputation points Volunteer Moderator
    2018-07-20T05:15:15+00:00

    Hi Louise

    In this zipped file 2 files:

    • The INPUT one used for testing. Kind of template you will use for next exports. If you duplicate/modify it make sure the blue table is named TBL_INPUT as this info is hard-coded in the query

    • The OUTPUT one that contains the query, pulling info from the INPUT file. Self explainatory:
      • Enter the path & file name to the INPUT file in I2 (info dynamically passed to the query)
      • Click the macro button on the left to Refresh the query and create a new workbook that will contain a copy (without the table header) of the OUTPUT sheet

    Was this answer helpful?

    0 comments No comments
  2. Lz365 38,201 Reputation points Volunteer Moderator
    2018-07-19T16:51:02+00:00

    Hello Louise

    I was going to provide you the next steps but just discovered something I did not expect. I need to bit of time to workaround this with a couple of VBA lines of code. Expect news by this Friday

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2018-07-19T15:02:15+00:00

    Hello,

    This has uploaded successfully.  :0)  I wasn't sure if all the commas at the end of rows would fail it as (eg) 00EMPLOYEE only has 6 columns and the csv conversion has obviously assumed the maximum number of columns for all record types/rows.  Well done!

    What next?

    Was this answer helpful?

    0 comments No comments
  4. Lz365 38,201 Reputation points Volunteer Moderator
    2018-07-18T10:16:17+00:00

    Louise

    Sample available here

    • The OUTPUT tab is what should be saved as CSV. Before you ask ;-) it is not possible to generate the same thing w/o row 1 (Col1, Col2...). You will have to remove it before saving as CSV
    • Re. the INPUT tab. This is your data (untouched) + 1 row at the top as all data must be stored in a Table
    • If you change data in the INPUT sheet, switch to the OUTPUT sheet > click somewher in the green table > Refresh

    Currently INPUT and OUTPUT are in the same file. This will cause you problem to save the OUTPUT (only) as CSV but don't worry we can do the same thing with separate files. I'll explain...

    Most important thing for now is to check the current OUTPUT is what you need + test the upload to your system

    Was this answer helpful?

    0 comments No comments