You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Power Query:为合并后的DBF数据表添加文件来源路径列的问题

解决Power Query合并DBF文件时添加源文件路径列的问题

你之前修改fnGetDBF函数的错误在于:把添加列的代码写在了函数定义的外部,这部分逻辑根本不会被执行,函数实际返回的还是原始的DBF表格,自然不会新增路径列。

正确方式1:在函数内部添加路径列

直接在fnGetDBF函数的内部流程里,获取DBF表格后立即添加路径列,再返回修改后的表格。修改后的函数代码如下:

let
    Source = (MyPath as text, MyFile as text) => 
        let
            ODBCSource = Odbc.DataSource("dsn=dBASE Files", [HierarchicalNavigation=true]),
            Database = ODBCSource{[Name=MyPath, Kind="Database"]}[Data],
            DBFTable = Database{[Name=MyFile, Kind="Table"]}[Data],
            // 添加完整的源文件路径列,也可只保留文件夹路径
            TableWithPath = Table.AddColumn(DBFTable, "FromPath", each MyPath & "\" & MyFile & ".dbf")
        in
            TableWithPath
in
    Source

正确方式2:在主查询中添加路径列

如果不想改动现有函数,也可以在主查询里通过关联列的方式,保留每条数据对应的源文件路径:

修改主查询的Get Tables及后续步骤:

let
    Source = Folder.Files("C:\Users\..."),
    #"Filtered Rows" = Table.SelectRows(Source, each ([Extension] = ".dbf") and ([Name] = "Point source.dbf")),
    // 先新增完整文件路径列
    #"Added FullPath" = Table.AddColumn(#"Filtered Rows", "FullPath", each [Folder Path] & [Name]),
    #"Added MyPath" = Table.AddColumn(#"Added FullPath", "MyPath", each Text.TrimEnd([Folder Path],"\")),
    #"Added MyFile" = Table.AddColumn(#"Added MyPath", "MyFile", each Text.Replace([Name],[Extension],"")),
    #"Get Tables" = Table.AddColumn(#"Added MyFile", "DBFTable", each fnGetDBF([MyPath], [MyFile])),
    // 展开表格时保留FullPath列
    #"Expanded DBFTable" = Table.ExpandTableColumn(#"Get Tables", "DBFTable", Table.ColumnNames(fnGetDBF(Table.First(#"Added MyFile")[MyPath], Table.First(#"Added MyFile")[MyFile])), Table.ColumnNames(fnGetDBF(Table.First(#"Added MyFile")[MyPath], Table.First(#"Added MyFile")[MyFile]))),
    // 可选:把路径列移到最前面
    #"Reordered Columns" = Table.ReorderColumns(#"Expanded DBFTable", {"FullPath"} & List.RemoveItems(Table.ColumnNames(#"Expanded DBFTable"), {"FullPath"}))
in
    #"Reordered Columns"

说明

第一种方法更简洁,利用函数已有的路径参数直接处理;第二种方法适合保留原有函数逻辑的场景,通过主查询的列关联实现需求。

内容的提问来源于stack exchange,提问作者djnz0feh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 18:45:00