微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Excel Power Query:如何使用 Power Query 动态使用选择表来自 Access 数据库?

如何解决Excel Power Query:如何使用 Power Query 动态使用选择表来自 Access 数据库?

我连接了一个带有 Access 数据库的 Excel 仪表板。在 Access 数据库中,我有多个表,每年一个(见截图)。数据格式100%相同,只是参考年份有所变化。

enter image description here

每当您运行仪表板时,都会要求用户指定开始年份。假设用户选择 2019 年,然后将日期范围设为 2019 年到 2020 年。现在,我的 Power Query 提取所有数据,即使是 2018 年,即使这些不是必需的。这是时间密集型的,因为它会消耗计算能力。

因此,我的想法是遍历 Access 数据库中的所有表名(请参阅下面屏幕截图中最左侧的列),然后仅从 Access 数据库中选择相关表。我使用此代码提取以下屏幕截图信息: Source = Access.Database(File.Contents(PATH),[CreateNavigationProperties=true]),

enter image description here

在这里我遇到了一个问题。我不知道如何遍历“名称”列中的所有条目并检查它们是否属于此列表的一部分。如果它是列表的一部分,我希望将 Access 中的表拉到 Excel 文件中;如果它变为 FALSE,则不应包括它,因为每个仪表板用户进行的查询不需要数据。

(loop as number,loop as number,year_table as number) =>
let
    
    Source =  Access.Database(File.Contents("PATH"),//code missing: dynammically define number of rows (=years >> here: 2018 through 2020 which makes 3) >> I call it "year_total"

    end_year = Date.Year(DateTime.LocalNow()),//define current year
    start_year = 2019,//refers to year chosen by dashboard user
    list_years = List.Numbers(2019,end_year-start_year),//defining date range: from chosen start year to current year

    current_loop = loop + 1
    if current_loop >= total_loops
    then year_table = //missing code: would be the content of xth row of "Name" column.
        if output_check = List.Contains(list_years,year_table) = TRUE   //check if given year from table in Access Database is part of list defined by start year and current year
        then //if TRUE,use data for query >> Could be smth like this? Source{[Schema="",Item="2019"]}[Data]
    else @Query1(total_loops,current_loop,year_table)

解决方法

如果我没猜错的话,您的工作簿中有多个具有相同结构的列表对象。组合例如两个表的方法可能看起来像这样

let
    Source1 = Excel.CurrentWorkbook(){[Name="table1"]}[Content],Source2 = Excel.CurrentWorkbook(){[Name="table2"]}[Content],tblCombine =Table.Combine({Source1,Source2})
in
    tblCombine

另一种方法可能是像这样加载工作簿中的所有表

let 
    Source = Excel.Workbook( File.Contents("D:\tmp\xlFileName.xlsx")),filtererTables = Table.SelectRows(Source,each ([Kind] = "Table")),removeColumns = Table.SelectColumns(filtererTables,{"Data"}),extendData = Table.ExpandTableColumn(removeColumns,"Data",{"SP1","SP2"},"SP2"})
in
    extendData

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。