使用 Power BI 中的编辑查询和 R 对多个表/数据集进行操作

2024-01-05

我有两张桌子tbl_A and tbl_B在我想使用以下命令进行转换和分析的 Power BI 文件中Run R Script功能在Edit Queries.

这将包括处理缺失值和连接表。然而,当启动 R 时,似乎我一次只能对一张表进行操作。这是因为Run R Script功能仅从单击时处于活动状态的表导入数据Run R Script按钮。然后该数据被存储在dataset多变的。

如果这是正确的,那么在我看来,R` 在 Power BI 中的实际使用将非常有限。我知道我可以加入桌子before我释放了 R。对于像这样的简单情况来说,这将是一个可行的解决方案,但对于更复杂的数据结构来说肯定不是。关于如何进行操作的任何建议R 的多个表在 Power BI 中?


简洁版本:

In Edit Queries, when inserting an R script, just add [dataset = "Renamed Columns", dataset2 = tbl_A] in the Formula bar. In this case Renamed Columns refers to the state of your table (under APPLIED STEPS) where you're inserting your R script, and tbl_A refers to another table that is available to you. And check all your settings with regards to Privacy.


长版

继我的评论之后,这是一个基于建议的解决方案商业智能博客 http://www.thebiccountant.com/2017/08/25/tips-and-tricks-for-r-scripts-in-the-query-editor-in-power-bi/和贡献PowerBI论坛 https://powerbi.microsoft.com/en-us/documentation/powerbi-desktop-r-in-query-editor/:

First you'll have to edit a few settings. Go to Options and Settings | Options. Under Privacy, select Always ignore Privacy Level settings. On your own risk of course...

Now, go to Options and Settings | Data Source Settings. Select source and click Edit permissons. Set it to Public:


现在我们可以开始了:

我将从头开始,因为我不知道任何其他数据加载方法会在 PowerBI 中触发什么怪癖。我有两个单独的 Excel 文件,每个文件包含一个名为tbl_A and tbl_B, 分别。 两个表的数据如下所示:

tbl_A 数据

Date        Price1  Price2
05.05.2016  23,615  24,775
04.05.2016  23,58   24,75
03.05.2016  0       24,35
02.05.2016  22,91   24,11
29.04.2016  22,93   24,24

tbl_A 截图

tbl_B 数据

Date        Price3  Price4
02.06.2016  19,35   22,8
01.06.2016  19      22,35
31.05.2016  19,35   22,71
30.05.2016  15,5    21,85
27.05.2016  19,43   22,52

tbl_B 截图


In the main window in PowerBI, load tbl_A using Get Data:

Do the same thing with tbl_B so that you end up with two separate tables under the Fields menu:

Click Edit Queries under the Home tab and make sure that the Formula Bar is visible. If not, you can activate it under View:

Depending on how your tables are loaded, PowerBI will add a few steps in the process. Those steps are visible under Query Settings:

Among other things, PowerBI changes the data type of dates to, you guessed it, Date. This can trigger problems https://stackoverflow.com/questions/41823277/r-script-in-power-bi-returns-date-as-microsoft-oledb-date later. To avoid this, we can change the data type for date in both tables to Text:

After you've done this for both tables, make sure tbl_B is active, and have a look at the Query Settings. You'll se that a new step Changed Type has been added in the data loading process:

我们将添加另一个步骤,以使即将推出的 R 脚本尽可能简单。在该脚本中,我们将使用以下方式连接表rbind()功能。除非不同表中的列名相同,否则这将触发错误。因此,继续将 B 列中的名称从Price3 and Price4 to Price1 and Price2, 分别:

Now, the Applied steps under Query settings should look like this:

最后一步的名称至关重要,因为您必须引用重命名的列(或者任何你想称呼它的其他名称)当你编写 R 脚本时。最终我们可以做到这一点。

Under Transform, click Run R Script. As the picture below describes, the variable dataset will contain the original data for your script. In this case, it will be tbl_B in the form of a dataframe if tbl_B was the active table when you clicked Run R Script:

For now, leave the script as it is, click OK, and have a look at the formula bar:

The picture above tells us two important things. First, we can see that the process has gone smoothly so far and that we have an empty table. Second, we can see that dataset refers to tbl_B in the state that we left it after the step Renamed Columns. And this is the part that can be confusing if you've read about these things elsewhere. In the Formula bar, you can enter a second dataset by adding , dataset2=tbl_A, so that the formula now looks like this:

Hit Enter

Under Query Settings, you will now see that there's a new step where you can edit your R script:

单击它返回 R 并添加这个小片段:

df_B <- dataset
df_A <- dataset2
df_temp <- rbind(df_A, df_B)

output <- df_temp

When you click OK, this is what you'll see:

Nevermind that the formula bar looks like a mess, just go ahead and click Table next to output.

就是这个!!

Go to Home and click Close & Apply to get out of the Query Editor. Now you can inspect the output from your R script under Fields, or in the Data tab like in the picture below:

最终结果将是原始版本tbl_B与列tbl_A添加到其中。不太花哨,但现在您已经在 R 脚本中组合了两个数据集,您可以将 R 的更大部分释放到您的工作流程中。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Power BI 中的编辑查询和 R 对多个表/数据集进行操作 的相关文章

  • 使用 ggplot 添加到投影地图时 R 饼图扭曲

    我想使用 ggplot 将饼图绘制到投影地图上 然而 饼图可能由于投影而变得扭曲 有谁知道如何在不变形的情况下绘制饼图 示例代码如下 谢谢 lib c ggplot2 scatterpie lapply lib library charac
  • 从每小时中提取/子集分钟值

    我的数据框包含以下格式的日期值YYYY MM DD HH MM SS跨越 125000 多行 按分钟细分 每行代表一分钟 1 2018 01 01 00 04 00 2 2018 01 01 00 05 00 3 2018 01 01 00
  • 如何从R中串扰的filter_select中删除(全部)?

    我遵循图 16 7 的示例https plotly r com client side linking html https plotly r com client side linking html并且无法弄清楚为什么有一个名为 全部 的
  • 距数据帧中最近的非 NA 值的距离

    我有以下数据帧 df 我想添加一列 其中包含与每行最接近的非 NA 值的距离 df lt data frame x 1 20 df c 1 3 4 5 11 14 15 16 x lt NA 换句话说 我正在寻找以下值 df distanc
  • 在 R 中安全地计算算术表达式?

    Edit 好吧 由于似乎有很多混乱 我将稍微简化一下问题 您可以尝试回答下面的原始问题 或者您可以解决此版本并忽略该行下面的所有内容 我的目标是采用任意表达式并在极其受限的环境中对其进行评估 该环境将仅包含具有以下类型值的变量 数值向量 接
  • 如何使用 ggplot2 对曲线下的区域进行着色

    我一直在尝试使用 ggplot2 生成类似于此 R 图形的绘图 xv lt seq 0 4 0 01 yv lt dnorm xv 2 0 5 plot xv yv type l polygon c xv xv lt 1 5 1 5 c y
  • 从受密码保护的站点读取信息

    我一直在 R 教程中使用 readLines 从网站上抓取信息 我现在希望从我自己的网站提取数据 特别是 awstats 数据 但是该域受密码保护 有没有一种方法可以通过用户名和密码传递我需要的特定 awstats 数据的 url url
  • 根据 R 中的字符串模式选择行

    假设我有以下数据 df lt data frame name c TO for Turnover for people HC people Hello world beenie man apple pears TO is number c
  • 使用faceting()时如何连接geom_point()和geom_line?

    我有一个问题 但我在互联网上没有找到任何相关信息 我很高兴得到一些提示 我有一个数据集 其中 x 轴是离散的 但我想将这些点相互连接 我可以做到 我的问题是当我添加分面选项时 我无法再将这些点相互链接起来 我找到了一个替代方案 但看起来不太
  • Shiny :针对所有错误显示一条消息

    我在 R 的 Shiny 中有一个应用程序 我想处理消息 以便用户看不到发生了什么错误 我知道通过 tags style type text css shiny output error visibility hidden shiny ou
  • 使用 R 下载压缩数据文件、提取并导入 .csv

    我正在尝试使用以下方法从网页下载并提取 csv 文件R 这个问题是重复的使用 R 下载压缩数据文件 提取和导入数据 https stackoverflow com questions 3053833 using r to download
  • 如何不显示 ggplot 轴上的所有标签?

    I m trying to using ggplot2 to plot this But as you can see on the x axis you can t read anything 那么如何在 x 轴上显示每 10 年的值呢
  • 构造奎因(自我复制功能)

    有没有人构建过 quine 生成自己源文本的副本作为其完整输出的程序 http www nyx net gthompso quine htm http www nyx net gthompso quine htm 在 R 中 quine 标
  • 使用 R 将日期格式的字符串列表/向量转换为 posix 日期类

    我有一个日期格式的字符串列表 我想将其转换为可以使用 R 操作的 posix 日期列表 我该怎么做 这就是我所拥有的 但我最终得到了一个列表 a lt c 2009 01 01 00 00 00 2009 01 01 00 00 00 z
  • R 3.5 - read.csv 无法读取 UTF-16 csv 文件

    我的代码如下 read csv http asic gov au Reports YTD 2018 RR20180420 001 SSDailyYTD csv skip 1 fileEncoding UTF 16 sep t header
  • 如何对范围内的行进行分组并考虑第三列?

    我有一个遗传数据集 我想对基因组中物理上靠近的遗传变异 行进行分组 我想对每条染色体基因组中某些点范围内的基因进行分组 chrom 我的 点 数据集包含变体 行需要在一定范围内的位置 如下所示 chrom low high 1 500 17
  • 在列标题和配对变量中嵌入数据的数据透视表

    假设我有这样的数据 不幸的是 变量值嵌入在列名称中 library tidyr library dplyr dat lt tribble group var1 var meta1 var2 var meta2 group1 5 2 cat
  • 按名称包含在单个对象中的多个列对 data.frame 进行排序?

    我想排序一个data frame由多列组成 理想情况下使用基础 R 无需任何外部包 尽管如果有必要 就这样吧 读过如何按列对数据框进行排序 https stackoverflow com questions 1296646 how to s
  • Matlab 中是否有相当于 R 的 dput() 的函数?

    Matlab 中是否有相当于 R 的 dput 的函数 dput 将 R 对象的 ASCII 文本表示形式写入文件或连接 UPDATE 1 添加了递归和对单元格的支持 UPDATE 2 添加了对结构的支持 UPDATE 3 增加了对逻辑 整
  • 在 R 中读入原始二进制数据并将其转换为整数

    我有一个二进制文件 其中包含编码为不同长度 主要是 2 4 字节 的有符号或无符号整数的数值 为了处理这些数据 我将文件的所需部分读取为raw向量与readBin 然后尝试将其转换为十进制 问题是 R的内置函数有限制 我不太明白 比如没有l

随机推荐