我大意了,没有防备,居然在Web.Contents()的第一参数里传变量。 Jacob Dong,公众号:Jacob的BI贴士优化SharePoint List查询速度,刷新时间缩短数倍
当你用Power Query做完网抓,开开心心发布到Power BI Service上。然后点开数据集开始配置计划刷新。突然就傻眼了,怎么……不能配置计划刷新了?
于是打开了解详细信息的页面https://aka.ms/dynamic-data-sources:
看懂了吗?
不管看得懂看不懂,我直接告诉你,这问题就出在Web.Contents()的第一参数上。
就拿昨天的文章举例,我在自定义函数中这么写的:
Web.Contents( "https://" & Domain & ".sharepoint.com/sites/" & SharePointSiteName & "/_api/web/lists/ GetByTitle('" & ListName & "')/ items?$skipToken=Paged=TRUE%26p_ID="& Text.From([Column1]) & "&$top=5000", [Headers = [Accept = "application/json"]] )
好家伙,直接往Web.Contents()的第一参数里传递了4个变量:
Domain,SharePointSiteName,ListName,Text.From(Column1)。
我原本是打算封装起来傻瓜式使用的。但是问题在于,对于已发布的数据集进行刷新时,Power BI服务会先对M代码进行静态分析,确定数据集中的数据源是什么,凭据是否正确。所以变量还没有传就已经在静态分析这一步失败了。
解决方法
Web.Contents()只检查基url,可以把含变量的部分拆分出来,写进RelativePath参数里拼接url。不过对于我这个案例,看来是行不通了,因为domain是基url的一部分。(如果不对希望高手可以指出)。
先写下完整的SharePoint网站地址:
Web.Contents( "https://.sharepoint.com/sites/", [Headers = [Accept = "application/json"]] )
将之后的url部分写进RelativePath:
Web.Contents( "https://.sharepoint.com/sites/", [ Headers = [Accept = "application/json"], RelativePath="_api/web/lists/GetByTitle('')/items?$skipToken=Paged=TRUE%26p_ID="&Text.From([Column1])&"&$top=5000" ] )
这样,这个数据源就可以被刷新了。
参考文章:
https://blog.crossjoin.co.uk/2016/08/16/using-the-relativepath-and-query-options-with-web-contents-in-power-query-and-power-bi-m-code/
https://blog.crossjoin.co.uk/2016/08/23/web-contents-m-functions-and-dataset-refresh-errors-in-power-bi/
https://blog.crossjoin.co.uk/2019/04/25/skip-test-connection-power-bi-refresh-failures/
https://aka.ms/dynamic-data-sources
http://blog.datainspirations.com/2018/02/17/dynamic-web-contents-and-power-bi-refresh-errors/
http://blog.datainspirations.com/2018/05/12/revisiting-dynamic-web-contents/