[已解决] Power Query参数表格,如路径不存在则不查询

  [复制链接]
查看96898 | 回复112 | 2020-9-25 02:49:13 | 显示全部楼层 |阅读模式
各位大大好,我做了一个参数表格,用来动态路径提取三个表格的数据最后Combine合并。表格结构相同。想请教一下,如何让Power Query在路径不存在时不查询子文件数据?

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

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

ParamRow = Table.SelectRows(ParamSource, each ([FACTORY] = ParameterName)),
Value=http://club.excelhome.net/
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}),
回复

使用道具 举报

曼舞 | 2020-9-25 02:58:14 | 显示全部楼层
那么有一个思路 不知可否
1. 保持原有的代码不变
2. 对数据刷新做出设置: 每次打开文件就刷新 然后勾选 “删除来自外部数据区域中的数据” 如附图3. 只是这样一来 所有的数据都不会呈现在表中

10376101820261.jpg
回复

使用道具 举报

leefoo | 2020-9-25 03:00:14 | 显示全部楼层
谢谢楼上~补充一下,实际上是想如果删除了ROUTE里面的路径,可以不进行这个路径文件的查询。
10376101820262.jpg
但是实际删除以后刷新数据则是报错。
10376101820263.jpg
回复

使用道具 举报

henry | 2020-9-25 03:07:14 | 显示全部楼层
把系统自动生成的 #"Changed Type" 这个步骤去掉试一下
回复

使用道具 举报

qeen | 2020-9-25 03:10:14 | 显示全部楼层
把系统自动生成的 #"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=http://club.excelhome.net/
  Type=Type
回复

使用道具 举报

都市清风 | 2020-9-25 03:16:15 | 显示全部楼层
最好能够有类似“如果路径为空则返回空查询”的语句。
回复

使用道具 举报

tangulasan | 2020-9-25 03:22:15 | 显示全部楼层
in后面改成以下代码试试
try#"Promoted Headers" otherwise #table({},{})
回复

使用道具 举报

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

使用道具 举报

mgw-hello | 2020-9-25 03:32:15 | 显示全部楼层
in后面改成以下代码试试
try#"Promoted Headers" otherwise #table({},{})


果然可以了,万分感谢!
回复

使用道具 举报

lkm | 2020-9-25 03:36:16 | 显示全部楼层
楼上正解,try otherwise就是M的容错语句,但是也有其缺陷,会遮盖所有的错误情况,不利于对错误产生原因的检查 ...


貌似这句语言能够看懂。主要是Power query似乎没有多少详细的教程。劳烦可否推荐一下编程语言的学习途径?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则