ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 5904|回复: 12

[求助] Power Query参数表格,如路径不存在则不查询

[复制链接]

TA的精华主题

TA的得分主题

发表于 2018-12-10 16:44 | 显示全部楼层 |阅读模式
各位大大好,我做了一个参数表格,用来动态路径提取三个表格的数据最后Combine合并。表格结构相同。想请教一下,如何让Power Query在路径不存在时不查询子文件数据?

参数表格动态路径的公式GetData:
(ParameterName as text) =>
let

ParamSource = Excel.CurrentWorkbook(){[Name="REFERENCE"]}[Content],

ParamRow = Table.SelectRows(ParamSource, each ([FACTORY] = ParameterName)),
Value=
if Table.IsEmpty(ParamRow)=true
then null
else Record.Field(ParamRow{0},"ROUTE")

in
Value

其中一个子文件HKI查询的公式,文件路径我用GetData公式引用了Excel中的一个Table。
let
    Source = Excel.Workbook(File.Contents(GetData("HKI")), null, true),
    List_Sheet = Source{[Item="List",Kind="Sheet"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(List_Sheet,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}, {"Column5", type text}, {"Column6", type any}, {"Column7", type text}, {"Column8", type any}, {"Column9", type text}, {"Column10", type text}, {"Column11", type text}, {"Column12", type text}, {"Column13", type text}, {"Column14", type text}, {"Column15", type text}, {"Column16", type text}, {"Column17", type text}, {"Column18", type text}, {"Column19", type any}, {"Column20", type text}, {"Column21", type text}, {"Column22", type text}, {"Column23", type text}, {"Column24", type any}, {"Column25", type any}, {"Column26", type any}, {"Column27", type any}, {"Column28", type any}, {"Column29", type any}, {"Column30", type any}, {"Column31", type any}, {"Column32", type text}, {"Column33", type text}, {"Column34", type text}, {"Column35", type text}, {"Column36", type any}, {"Column37", type text}, {"Column38", type text}, {"Column39", type text}, {"Column40", type text}, {"Column41", type text}, {"Column42", type text}, {"Column43", type text}, {"Column44", type any}, {"Column45", type text}, {"Column46", type text}, {"Column47", type text}, {"Column48", type text}, {"Column49", type text}, {"Column50", type text}, {"Column51", type text}, {"Column52", type text}, {"Column53", type text}, {"Column54", type any}, {"Column55", type text}, {"Column56", type text}, {"Column57", type text}, {"Column58", type text}, {"Column59", type text}, {"Column60", type text}, {"Column61", type text}, {"Column62", type text}, {"Column63", type any}, {"Column64", type text}, {"Column65", type text}, {"Column66", type text}, {"Column67", type text}, {"Column68", type text}, {"Column69", type text}, {"Column70", type text}, {"Column71", type text}, {"Column72", type any}, {"Column73", type text}}),
    #"Removed Blank Rows" = Table.SelectRows(#"Changed Type", each not List.IsEmpty(List.RemoveMatchingItems(Record.FieldValues(_), {"", null}))),
    #"Removed Top Rows" = Table.Skip(#"Removed Blank Rows",8),
    #"Promoted Headers" = Table.PromoteHeaders(#"Removed Top Rows", [PromoteAllScalars=true])
in
    #"Promoted Headers"

Combine了三个文件HKI,HRG,SSC的数据,用
let
    Source = Table.Combine({HKI, HRG, SSC}),

TA的精华主题

TA的得分主题

发表于 2018-12-10 18:03 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
那么有一个思路 不知可否
1. 保持原有的代码不变
2. 对数据刷新做出设置: 每次打开文件就刷新 然后勾选 “删除来自外部数据区域中的数据” 如附图3. 只是这样一来 所有的数据都不会呈现在表中

捕获.JPG

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-12-10 20:32 | 显示全部楼层
谢谢楼上~补充一下,实际上是想如果删除了ROUTE里面的路径,可以不进行这个路径文件的查询。
Capture.JPG
但是实际删除以后刷新数据则是报错。
Capture2.JPG

TA的精华主题

TA的得分主题

发表于 2018-12-10 21:17 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
把系统自动生成的 #"Changed Type" 这个步骤去掉试一下

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-12-10 23:44 | 显示全部楼层
江南月 发表于 2018-12-10 21:17
把系统自动生成的 #"Changed Type" 这个步骤去掉试一下

没有用,现在语句改成:
let
    Source = Excel.Workbook(File.Contents(GetData("SSC")), null, true),
    List_Sheet = Source{[Item="List",Kind="Sheet"]}[Data],
    #"Removed Blank Rows" = Table.SelectRows(List_Sheet, each not List.IsEmpty(List.RemoveMatchingItems(Record.FieldValues(_), {"", null}))),
    #"Removed Top Rows" = Table.Skip(#"Removed Blank Rows",8),
    #"Promoted Headers" = Table.PromoteHeaders(#"Removed Top Rows", [PromoteAllScalars=true])
in
    #"Promoted Headers"

显示错误:
Expression.Error: We cannot convert the value null to type Text.
Details:
    Value=
    Type=Type

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-12-11 11:04 | 显示全部楼层
最好能够有类似“如果路径为空则返回空查询”的语句。

TA的精华主题

TA的得分主题

发表于 2018-12-11 11:56 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
in后面改成以下代码试试
try  #"Promoted Headers" otherwise #table({},{})

评分

1

查看全部评分

TA的精华主题

TA的得分主题

发表于 2018-12-11 14:00 | 显示全部楼层
本帖最后由 江南月 于 2018-12-11 17:36 编辑

楼上正解,try otherwise就是M的容错语句,但是也有其缺陷,会遮盖所有的错误情况,不利于对错误产生原因的检查,其实powerquery的错误提示功能还是很强大的,基本上都讲明了错误的原因和出处,本例中错误语句的实际含义就是表格名称(text类型)不能为null,实际就是在说文件路径不存在,用楼上的语句可以解决,但是如果是文件存在,有其它原因导致出错,就有可能导致错误判断,所以还是运行后知道具体错误所在后再添加该类语句为妥

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-12-12 20:46 | 显示全部楼层
wdx223 发表于 2018-12-11 11:56
in后面改成以下代码试试
try  #"Promoted Headers" otherwise #table({},{})

果然可以了,万分感谢!

TA的精华主题

TA的得分主题

 楼主| 发表于 2018-12-12 20:47 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
江南月 发表于 2018-12-11 14:00
楼上正解,try otherwise就是M的容错语句,但是也有其缺陷,会遮盖所有的错误情况,不利于对错误产生原因的检查 ...

貌似这句语言能够看懂。主要是Power query似乎没有多少详细的教程。劳烦可否推荐一下编程语言的学习途径?
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-3-29 06:57 , Processed in 0.052532 second(s), 15 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表