简体   繁体   English

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

[英]Extract data from power BI pbix file using SQL and R

I want to extract data from a Power BI pbix file using R. The recently released pbixr package seems to offer the functions to do this but unfortunately there is hardly any documentation on how the extract data tables from a pbix file.我想使用 R 从 Power BI pbix 文件中提取数据。最近发布的pbixr 包似乎提供了执行此操作的功能,但不幸的是,几乎没有任何关于如何从 pbix 文件中提取数据表的文档。 I experimented with several functions and managed to retrieve the data tables.我尝试了几个函数并设法检索了数据表。 However, I fail to extract tables that are generated by Power BI using formulas (eg SUM etc).但是,我无法使用公式(例如 SUM 等)提取 Power BI 生成的表。 These are marked in Power BI with a calculator symbol in front.这些在 Power BI 中标有前面的计算器符号。

At the office we have a simple piece of software that works very similar to the pbixr package when linking to and collecting data from a pbix file (ie using localhost: correct_port in combination with an SQL command), that is able to extract all data including the Power BI generated ones.在办公室,我们有一个简单的软件,当链接到 pbix 文件并从 pbix 文件收集数据时,它的工作方式与 pbixr 包非常相似(即使用 localhost:correct_port 结合 SQL 命令),它能够提取所有数据,包括Power BI 生成的。 This makes me think that this also should be possible by using the pbixr package but perhaps I am wrong.这让我认为这也应该可以通过使用 pbixr 包来实现,但也许我错了。

Possibly the OlapR package from Microsoft also might do the trick but as this is not freeware and probably needs a specific version of RI do not want to use it.可能来自 Microsoft 的 OlapR 包也可以解决这个问题,但因为这不是免费软件并且可能需要特定版本的 RI 不想使用它。 But I do welcome any other approaches to access the content of a pbix file.但我欢迎任何其他访问 pbix 文件内容的方法。

A reproducible example:一个可重现的例子:

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")

At this point Power BI should be open and the example file sample_vig.pbix opened.此时 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 only contains the pure data tables but not the Power BI generated ones such as Avg Rating , Avg Votes , etc. get_tables仅包含纯数据表,但不包含 Power BI 生成的表,例如Avg RatingAvg Votes等。

The author of the pbixr package has provided a solution on the issues tracker section of the package website. pbixr 包的作者在包网站的问题跟踪器部分提供了解决方案。 See here .这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM