将.XLS报表自动化到Power BI

时间:2019-11-19 00:02:59

标签: powerbi powerquery

我正在尝试将Power BI数据集与Microsoft Flow结合使用以进行每日更新。这是我目前的情况:

  1. 每天早上将报告以.xls格式导出​​到SharePoint
  2. 我打开Excel刷新电源查询,该电源查询将报告中的数据提取为仪表板和MS Flow的格式 (我尝试将更新直接安排到Power BI,但似乎无法将更新安排到.xls)
  3. MS Flow&Power BI从包含查询数据的.xlsx文件中刷新

有没有办法避免打开Excel并按Power Query刷新的手动步骤?

let
    Source = SharePoint.Files("https://xxxxxxxxxx", [ApiVersion = 15]),
    FileContent = Source{[Name = "Individual Status.xls"]}[Content],
    ParsedTable = ParseXls(FileContent, "Individual Status")
in
    Source

Expression.Error: We cannot convert the value "Individual Status" to type Binary.
Details:
Value=Individual Status.xls
Type=[Type]

1 个答案:

答案 0 :(得分:0)

可能的解决方法是直接在Power BI中使用Python或R脚本解析.xls文件。

这是使用Python解析.xls的示例函数。将以下代码复制并粘贴到空白查询中,然后将其重命名为“ ParseXls”或其他有意义的名称。

(workbook as binary, sheet as text, optional header as logical) as table =>
    let dataset = Table.FromRecords({[
        content = Binary.ToText(workbook, BinaryEncoding.Base64),
        sheet = sheet,
        header = if header = null then true else header
    ]}),
    result = Python.Execute("import pandas as pd#(lf)from base64 import b64decode#(lf)from io import BytesIO#(lf)params = dataset.loc[0]#(lf)content = BytesIO(b64decode(params['content']))#(lf)sheet = params['sheet']#(lf)header = 0 if params['header'] else None#(lf)result = pd.read_excel(content, sheet, header=header)", [dataset = dataset]){[Name = "result"]}[Value]
    in result

然后,您可以将此功能与从SharePoint获得的二进制文件内容一起使用。整个查询将如下所示。

let
    Source = SharePoint.Files("https://yourcompany.sharepoint.com/...", [ApiVersion = 15]),
    FileContent = Source{[Name = "Book1.xls"]}[Content],
    ParsedTable = ParseXls(FileContent, "Sheet1"),
    ...