Discourse 数据探索器 Power BI 集成
我最近开发了一个很棒的 Power BI 函数,可以直接将数据探索器(Data Explorer)的数据获取到 Power BI 中,且开销极小。
因此,如果您像我一样,希望数据能够通过 Power BI 应用程序自动拉取和刷新,请将以下函数代码放入 Power BI 的“高级编辑器”中:
(queryID) => let
resultCount = 1000,
otherNameForPage = 0,
GetPage = (otherNameForPage) =>
let
content1 = "params={""page":""" & Number.ToText(otherNameForPage) & """}",
RawData = Json.Document(Web.Contents(
"https://forumURL/admin/plugins/explorer/queries",
[RelativePath=Number.ToText(queryID) & "/run",
Query=
[
params="{"page":""" & Number.ToText(otherNameForPage) & """}"
],
Headers = [
#"api-username"="yourAPIUsername",
#"api-key"="yourAPIKey",
#"Content-Type" = "application/x-www-form-urlencoded"],
Content = Text.ToBinary(content1)
]
) ),
resultCount = RawData[result_count]
in
if RawData[result_count] = 0 then null else RawData,
Pages = List.Generate(
() => [i = 0, RawData = GetPage(i)],
each [RawData] <> null,
each [i=[i]+1, RawData = GetPage(i)],
each Table.Combine(let raw = [RawData] in List.Transform(raw[rows], each Table.FromRows({_}, raw[columns])))),
Output = Table.Combine(Pages)
in
Output
这里有一个数据探索器查询示例。它包含了必需的分页功能,以便上述函数正常工作(即使您的查询只返回 1 页结果,这也是必需的):
--[params]
-- integer :page = 0
SELECT count(*) from badges
OFFSET :page * 1000
LIMIT 1000
(您可以通过包含上述示例的第一行和最后一行,轻松适配您现有的查询)
此函数将自动继续迭代页面,直到找不到更多结果为止。
如何使其生效?
将函数加载到 Power BI 后,您需要设置两件事:
-
api-username 和 api-key(请参阅上方函数中的占位符):
Headers = [ #"api-username"="yourAPIUsername", #"api-key"="yourAPIKey", -
查询 ID 编号。
要获取您的数据,您只需提供查询的 ID,该 ID 很容易在查询 URL 中找到:

点击函数后,Power BI 中的显示应如下所示:
示例输出
这是示例查询的输出:

就是这样!现在您可以随意塑造您的数据了。










