【问题标题】:Pass a segment value from Power BI to its data source Script R将段值从 Power BI 传递到其数据源脚本 R
【发布时间】:2022-01-20 06:12:15
【问题描述】:

我在 Power BI 中有一个报表,其中 Data Source = "Script R"。

在 R 脚本中,我连接到数据库,以提取以下格式的数据:

CustomerID    date_visit   date_order    ProductType   DeviceNumber
 1456778      2020-01-02   2020-03-01      Shoes           XX1
 1456778      2020-04-02   2020-04-11      Pants           XX2
 1456778      2020-05-12   2020-06-22      Shoes           XX2
 2345111      2020-01-22   2020-02-02      Shoes           YY2
 2345111      2020-03-03   2020-04-01      Pants           YY1

然后我对数据框进行一些计算,例如:

devicesPerClient <- fulldata %>% 
  group_by(CustomerID, ProductType) %>% 
  summarise(devices_count = n_distinct(DeviceNumber))

给出结果:

CustomerID    ProductType    devices_count
1456778        Shoes            2
1456778        Pants            1
2345111        Shoes            1
2345111        Pants            1

然后将此数据框导入 Power BI 并显示如下:

视觉类型:Segment

字段:ProductType

这允许根据产品类型过滤整个报告。

视觉类型:Card

字段:devices_count 的平均值

该报告最终使我们能够查看每种产品类型使用的平均设备数量。


目前在所有数据集上完成此操作,包括所有日期。

我想改进我的 Power BI 报表以添加按日期范围筛选。

然后,所选日期将根据在 Power BI 中选择的日期范围为date_order在 R 中创建一个子集数据框

例如:从 2020 年 1 月到 2020 年 2 月,每种产品类型平均使用了多少台设备?

我的问题是按日期过滤,每次过滤器更改日期范围时,都必须在 R 中重做计算。

有没有办法将段值从 Power BI 传递到我的 R 代码,以便在每次值更改时重新计算

我不知道我的问题是否清楚。我仍然有空。 谢谢

【问题讨论】:

  • 我完全不明白为什么需要涉及 R。将您的数据库表加载到 Power BI 中,然后使用日期表和度量与它进行更动态的交互。
  • 因为我正在 R 中执行诸如:devices_count、设备计数四分位数等操作。所以这些计算将取决于在 PowerBI 中选择的日期。 @AlexisOlson
  • @HelpASisterOut - 您可以在 Power BI 中执行所有这些操作。 R 这里没有给你任何价值,你使解决方案过于复杂。
  • @RADO 我正在尝试查找如何在 PowerBI 中计算四分位数,但我没有找到一个简单的解决方案来解决我的问题。我可能需要重新考虑我的整个项目。
  • 经典 XY 问题...

标签: r powerbi


【解决方案1】:

对此有两个选项 - 参数或数据集。我发现数据集方法更灵活,因为您可以通过最少的代码更改轻松传递多个列。

关键的变化是重建您的查询,使其从常规 Power Query 步骤开始,生成一个单行表,其中您的日期范围值作为列,可能是开始日期和结束日期。

然后您将一个 R 脚本步骤添加到该查询中。默认情况下,它将作为“数据集”将上一步的表传递给 R。

这里有一个相当简单的例子:

https://querypower.com/2017/03/11/r-execute-the-swiss-army-knife-of-power-query/

这有更复杂的主题,涵盖参数等等:

https://www.thebiccountant.com/2017/08/25/tips-and-tricks-for-r-scripts-in-the-query-editor-in-power-bi/

就我个人而言,我会尝试将“从 SQL 获取数据”步骤从 R 移到 Power Query - 这是一个非常有效的工具,并且有许多用于日期过滤的内置选项,可能会给你一个无代码该要求的解决方案。

然后 R 接收的数据集将类似于您示例中的第一个表,而您剩余的 R 脚本将只运行 R 特定的要求,例如分位数。

【讨论】:

    【解决方案2】:

    假设在这个给定的数据帧中,您要编写一个返回 date_visit&gt;2020-02-15 子集的过滤器,它应该只返回 2 行。

    如果您想使用 R 进行过滤,但在 Power BI 中,您将这样做

    # 'dataset' holds the input data for this script
    dataset$date_visit<-as.Date(dataset$date_visit)
    dataset$date_order<-as.Date(dataset$date_order)
    filter<-subset(dataset,date_visit>"2020-02-15")
    filter
    

    完整的M在这里

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fY8xCoAwEAT/ktpAbnNRnyHYJIQUFoKVFvp/PAhJFMTqmGKY2xgVseuHYVSdgoHRhrRBBSssMG/Hesr1nlTq/h0SYIFp2a/s4NNxmlqn18Crkx1YdkT06KA5yNHihPDtyARbgfOe8lsIsifd", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [CustomerID = _t, date_visit = _t, date_order = _t, ProductType = _t, DeviceNumber = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"CustomerID", Int64.Type}, {"date_visit", type text}, {"date_order", type text}, {"ProductType", type text}, {"DeviceNumber", type text}}),
        #"Run R script" = R.Execute("# 'dataset' holds the input data for this script#(lf)src<-data.frame(dataset)#(lf)dataset$date_visit<-as.Date(dataset$date_visit)#(lf)dataset$date_order<-as.Date(dataset$date_order)#(lf)filter<-subset(dataset,date_visit>""2020-02-15"")#(lf)filter",[dataset=#"Changed Type"]),
        filter = #"Run R script"{[Name="filter"]}[Value]
    in
        filter
    

    如果您想为date_visit 传递参数化值(通过一些动态 M 查询/您创建的参数/硬编码值生成),您需要像下面这样传递

    let
       val ="2020-02-15", /*this is the parameterized value either dynamically generated or harcoded to be passed on to R */
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fY8xCoAwEAT/ktpAbnNRnyHYJIQUFoKVFvp/PAhJFMTqmGKY2xgVseuHYVSdgoHRhrRBBSssMG/Hesr1nlTq/h0SYIFp2a/s4NNxmlqn18Crkx1YdkT06KA5yNHihPDtyARbgfOe8lsIsifd", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [CustomerID = _t, date_visit = _t, date_order = _t, ProductType = _t, DeviceNumber = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"CustomerID", Int64.Type}, {"date_visit", type text}, {"date_order", type text}, {"ProductType", type text}, {"DeviceNumber", type text}}),
        #"Run R script" = R.Execute("# 'dataset' holds the input data for this script#(lf)src<-data.frame(dataset)#(lf)src$date_visit<-as.Date(src$date_visit)#(lf)src$date_order<-as.Date(src$date_order)#(lf)filter<-subset(src,date_visit>"""&val&""")#(lf)filter",[dataset=#"Changed Type"]),
        filter = #"Run R script"{[Name="filter"]}[Value]
    in
        filter
    

    它会生成以下内容

    如果您在 PBI 中像这样动态过滤数据帧,请记住一个方面,dataType Date 在 R 和 PBI 之间非常棘手。必须先用as.Date 进行转换,然后才能在 R 端应用任何过滤。

    但如果您在ProductType="Shoes" 上进行过滤,您可以简单地这样做

    let
       val ="Shoes",
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fY8xCoAwEAT/ktpAbnNRnyHYJIQUFoKVFvp/PAhJFMTqmGKY2xgVseuHYVSdgoHRhrRBBSssMG/Hesr1nlTq/h0SYIFp2a/s4NNxmlqn18Crkx1YdkT06KA5yNHihPDtyARbgfOe8lsIsifd", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [CustomerID = _t, date_visit = _t, date_order = _t, ProductType = _t, DeviceNumber = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"CustomerID", Int64.Type}, {"date_visit", type text}, {"date_order", type text}, {"ProductType", type text}, {"DeviceNumber", type text}}),
        #"Run R script" = R.Execute("# 'dataset' holds the input data for this script#(lf)src<-dataset#(lf)filter<-subset(src, ProductType=="""&val&""")",[dataset=#"Changed Type"]),
        filter = #"Run R script"{[Name="filter"]}[Value]
    in
        filter
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多