Forum Discussion

BMM27's avatar
BMM27
Helper I
2 years ago
Solved

Setting Scheduled update on Dynamic source query

Hi, I've created a data source that retrieves files from a folder and performs actions based on the file name. However, when I published it to the Power BI service, scheduled updates are disabled. I...
  • BMM27's avatar
    BMM27
    2 years ago

    Hi, here is my solution. For anyone who is going to have same trouble, here is the code.
    This code connects to a sharepoint folder, search by name different files, and unify all files into one table.

    let
    // Source
    Source = SharePoint.Files("https://SharepointSite", [ApiVersion = 15]),
    FilteredFiles = Table.SelectRows(Source, each Text.StartsWith([Name], "AAA") or Text.StartsWith([Name], "BBB") and [Folder Path] = "https://sharepointfolder"),

    // Data process function
    ProcessFile = (file as record) =>
    let
    name = Text.BeforeDelimiter(file[Name], ".xlsx"),
    content = file[Content],
    workbook = Excel.Workbook(content),

    // Sheet names
    sheetNames = Table.Column(workbook, "Item"),
    origen = if List.Contains(sheetNames, "aa") then "AA" else if Text.StartsWith(name, "bb") then "BB" else "CC",
    sheetName = if origen = "aa" then "AA" else if origen = "aa" then "BB" else "cc",

    sheet = try workbook{[Item=sheetName, Kind="Sheet"]}[Data] otherwise null,
    table = if sheet = null then null else Table.Skip(sheet, 1)
    in
    table,

    // Process each file
    ProcessedFiles = Table.AddColumn(FilteredFiles, "ProcessedContent", each ProcessFile(_)),

    // Filter Null
    NonNullTables = Table.SelectRows(ProcessedFiles, each [ProcessedContent] <> null),

    // Expand and combine
    ExpandedTables = Table.ExpandTableColumn(NonNullTables, "ProcessedContent", {"Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9", "Column10"}),

    #"Removed Columns" = Table.RemoveColumns(ExpandedTables,{"Content", "Name", "Extension", "Date accessed", "Date modified", "Date created", "Attributes", "Folder Path"})
    in
    #"Removed Columns"