使用SQL和R从Power BI pbix文件中提取数据

时间:2020-10-01 20:20:12

标签: sql r powerbi

我想使用R从Power BI pbix文件中提取数据。最近发布的pbixr package似乎提供了执行此操作的功能,但是不幸的是,几乎没有任何文档说明如何从pbix文件中提取数据表。 。我尝试了几种功能,并设法检索了数据表。但是,我无法使用公式(例如SUM等)提取Power BI生成的表。这些在Power BI中用前面的计算器符号标记。

在办公室,我们有一个简单的软件,当链接到pbix文件并从pbix文件中收集数据时,其工作原理与pbixr软件包非常相似(例如,将localhost:correct_port与SQL命令结合使用),该软件可以提取所有数据,包括Power BI生成的数据。这使我认为通过使用pbixr包也应该可以实现,但是也许我错了。

可能来自Microsoft的OlapR软件包也可以解决这个问题,但是由于它不是免费软件,并且可能需要特定版本的R,所以我不想使用它。但我确实欢迎任何其他方法来访问pbix文件的内容。

可复制的示例:

install.packages("pbixr")
library(pbixr)

# Download example pbix file (also see pbixr vignette)
temp_dir <- "C:/users/dijk158/temp" # set your own temp dir
dir.exists(temp_dir)
sample_file_name <- "sample_vig.pbix"
path_file_sample <- file.path(temp_dir, sample_file_name)
url_pt1 <- "https://github.com/KoenVerbeeck/PowerBI-Course/blob/"
url_pt2 <- "master/pbix/TopMovies.pbix?raw=true"
url <- paste0(url_pt1, url_pt2)
req <- download.file(url, destfile = path_file_sample, mode = "wb")

这时应该打开Power BI,并打开示例文件sample_vig.pbix。

# Find port
connections_open <- f_get_connections()
connections_open$pbix <- gsub(" - Power BI Desktop", "",
                              connections_open$pbix_name)
correct_port <- as.numeric(connections_open$ports)

# Create the connection
connection_db <- paste0("Provider=MSOLAP.8;Data Source=localhost:",
                        correct_port, ";MDX Compatibility=1")

# Read tables
sql_table <- "evaluate TopMovies"
get_tables <- f_query_datamodel(sql_table, connection_db)
names(get_tables)
summary(get_tables)

get_tables仅包含纯数据表,而不包含Power BI生成的数据表,例如Avg RatingAvg Votes等。

1 个答案:

答案 0 :(得分:0)

pbixr软件包的作者在软件包网站的问题跟踪器部分提供了一种解决方案。参见here

相关问题