假设在这个给定的数据帧中,您要编写一个返回 date_visit>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