Forum Discussion

fcipriano's avatar
fcipriano
Frequent Visitor
9 years ago
Solved

Load file from folder with different column names

Hi,

 

Hoping for your inputs. I tried to load files from folder. The files have different column name, but has the same column number. Basically the first file has the column names, while the rest only has the values. When the file is loaded, only the first file's values loaded, while the rest did not. For some reason, when I first tries it, it worked, but on the next one it did not.

 

Hoping for your inputs.

 

Thanks,

  • Anonymous's avatar
    Anonymous
    9 years ago

    Hi fcipriano,

     

    You can refer to below steps to merge files from folder:

     

    1. Get data from folder.
    2. Write a custom function to analysis data and merge them.
    3. Use first row as header.

     

    Custom functions:
    
    1. AnalysisFile
    
    let
       AnalysisFile = (FilePath as text, Extension as text) => 
        let
            values = {
            {".xlsx", Excel.Workbook(File.Contents(FilePath), true)},
    	{".csv", Csv.Document(File.Contents(FilePath))},
    	{".txt", Csv.Document(File.Contents(FilePath))}
            {Extension, null}
            },
            Result = List.First(List.Select(values, each _{0}=Extension)){1}
        in
    	   Result
    in
        AnalysisFile
    
    2. LoadFileAndCombine
    
    let
        LoadFileAndCombine= (FilePath as text) as table => 
        let
            Source = Folder.Files(FilePath),
            #"Filtered Rows" = Table.SelectRows(Source, each [Name] = "aaa" or [Name] = "bbb"),//filter the specify files
            Custom = Table.SelectColumns(Table.AddColumn(#"Filtered Rows", "Custom", each AnalysisFile([Folder Path]&[Content],[Extension])),"Custom"),
            Combine= Table.Combine(Custom[Custom])
    in
        Combine
    in 
        LoadFileAndCombine

     

    Use:

    #"Filtered Rows" result.

     

    Invoke the function.

    let
        Source = LoadFileAndCombine("C:\Users\xxxxxx\Desktop\Test"),
        #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true])
    in
        #"Promoted Headers"

     

     

    If above not help, can you share the pibx file to test?

     

    Regards,

    Xiaoxin Sheng

4 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi fcipriano,

     

    You can refer to below steps to merge files from folder:

     

    1. Get data from folder.
    2. Write a custom function to analysis data and merge them.
    3. Use first row as header.

     

    Custom functions:
    
    1. AnalysisFile
    
    let
       AnalysisFile = (FilePath as text, Extension as text) => 
        let
            values = {
            {".xlsx", Excel.Workbook(File.Contents(FilePath), true)},
    	{".csv", Csv.Document(File.Contents(FilePath))},
    	{".txt", Csv.Document(File.Contents(FilePath))}
            {Extension, null}
            },
            Result = List.First(List.Select(values, each _{0}=Extension)){1}
        in
    	   Result
    in
        AnalysisFile
    
    2. LoadFileAndCombine
    
    let
        LoadFileAndCombine= (FilePath as text) as table => 
        let
            Source = Folder.Files(FilePath),
            #"Filtered Rows" = Table.SelectRows(Source, each [Name] = "aaa" or [Name] = "bbb"),//filter the specify files
            Custom = Table.SelectColumns(Table.AddColumn(#"Filtered Rows", "Custom", each AnalysisFile([Folder Path]&[Content],[Extension])),"Custom"),
            Combine= Table.Combine(Custom[Custom])
    in
        Combine
    in 
        LoadFileAndCombine

     

    Use:

    #"Filtered Rows" result.

     

    Invoke the function.

    let
        Source = LoadFileAndCombine("C:\Users\xxxxxx\Desktop\Test"),
        #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true])
    in
        #"Promoted Headers"

     

     

    If above not help, can you share the pibx file to test?

     

    Regards,

    Xiaoxin Sheng

      • Anonymous's avatar
        Anonymous
        Not applicable

        Hi fcipriano,

         

        I test without any issue, perhaps you can try my method.

         

        Use steps:

        1. Move source data to a empty folder and make sure the file which contains the column header are at the first.

        2. Open the query editor and locate to the "LoadFileAndCombine" funtion.

        3. Copy the folder path(include the folder name) to path textbox, and click the invoke button.

         

        Regards,

        Xiaoxin Sheng