Forum Discussion

CiceroBC's avatar
CiceroBC
Advocate II
6 years ago
Solved

Add row numbers to each text file from a folder

I'm looking for a way to inject a row number when using the folder source optoin in power query (in Excel). I have a large amount of text files with unique and different file extensions based on the ...
  • Anonymous's avatar
    Anonymous
    6 years ago

    Hi CiceroBC ,

     

    this part is reasonably simple:

    1. Import files from a folder (a case may be from an online folder):

    2. Filter, if necessary, and then click on the button in the "Content" header:

    3. Confirm Ok on the next step. PBI will generate some code to import your data in both the current query and in a separate group. The group will look something like this:

    4. Select the Transform Sample File query and add an index column:

     

    Once it is added in the template query, PBI will update a function linked to this query and once you come back to your original query you should see something like this:

     

     

    Hope this helps :).

     

    This is the code generated by PBI:

    Main query:

    let
        Source = Folder.Files("D:\"),
        #"Filtered Rows" = Table.SelectRows(Source, each ([Name] = "123.abc" or [Name] = "123.xyz")),
        #"Filtered Hidden Files1" = Table.SelectRows(#"Filtered Rows", each [Attributes]?[Hidden]? <> true),
        #"Invoke Custom Function1" = Table.AddColumn(#"Filtered Hidden Files1", "Transform File (2)", each #"Transform File (2)"([Content])),
        #"Renamed Columns1" = Table.RenameColumns(#"Invoke Custom Function1", {"Name", "Source.Name"}),
        #"Removed Other Columns1" = Table.SelectColumns(#"Renamed Columns1", {"Source.Name", "Transform File (2)"})
    in #"Removed Other Columns1"

     

    And the transform function called from the main query (in my case the function is called Transform File (2)😞

    let
        Source = (Parameter2 as binary) => let
            Source = Table.FromColumns({Lines.FromBinary(Parameter2, null, null, 1252)}),
            #"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1)
        in
            #"Added Index"
    in
        Source

     

    Kind regards,

    JB