Forum Discussion

Boja's avatar
Boja
Advocate II
9 years ago
Solved

Import the newest excel file from SharePoint

Hello,   I would need an assistance with M code.   Namely, excel files are stored on the sharedrive and Power Bi only needs the newest file from the folder (the file with the latest date created)...
  • v-ljerr-msft's avatar
    9 years ago

    Hi Boja,

     

    Based on my test, The M code below should work in this scenario. 

    let
        Source = SharePoint.Files("https://mytenant.sharepoint.com/teams/abc", [ApiVersion = 15]),
        #"Sorted Rows" = Table.Sort(Source,{{"Name", Order.Descending}}),
        #"Kept First Rows" = Table.FirstN(#"Sorted Rows",1),
        #"Invoke Custom Function1" = Table.AddColumn(#"Kept First Rows", "Transform File from Query1", each #"Transform File from Query1"([Content])),
        #"Renamed Columns1" = Table.RenameColumns(#"Invoke Custom Function1", {"Name", "Source.Name"}),
        #"Removed Other Columns1" = Table.SelectColumns(#"Renamed Columns1", {"Source.Name", "Transform File from Query1"}),
        #"Expanded Table Column1" = Table.ExpandTableColumn(#"Removed Other Columns1", "Transform File from Query1", Table.ColumnNames(#"Transform File from Query1"(#"Sample File"))),
        #"Changed Type" = Table.TransformColumnTypes(#"Expanded Table Column1",{{"Source.Name", type text}, {"Product", type text}, {"Category", type text}, {"Segment", type text}, {"ManufacturerID", Int64.Type}, {"Manufacturer", type text}})
    in
        #"Changed Type"

    In addition, here is a good article about loading the newest excel file from a Folder which is similar to loading from SharePoint Folder for your reference. :smileyhappy: 

     

    Regards