识别一条记录是否有很多产品
Identify whether a record has many products
我同时使用 Access 和 Power BI 来解决这个问题,但无济于事。
原则上其实很简单,但我需要以特定的方式来做,以便后续的定价计算。
本质上,我有一个 table 交易和服务 ID。此服务 ID 将在一个或多个产品中出现多次。如果它只是一个产品,那么我需要用 "Single Product" 填充一列,否则 "Multiple Product"。见下图:
Serviceid Product Type
1 A Multiple Products
1 B Multiple Products
2 A Single Product
3 A Single Product
3 A Single Product
3 A Single Product
您可以使用 IIF()
和 EXISTS
来填充新列
select
t.serviceid,
t.product,
iif(
exists (
select 1 from tablename where serviceid = t.serviceid and product <> t.product
),
'Multiple Products',
'Single Product'
) as type
from tablename as t
在 Power BI 中,您可以在 Power Query 中执行此操作:
let
Source = SourceTable,
fnDistinct = (MyTable, MyServiceid) =>
let
#"Filtered Rows" = Table.SelectRows(MyTable, each ([Serviceid] = MyServiceid)),
#"Distinct Products" = List.Count(List.Distinct(#"Filtered Rows"[Product]))
in
#"Distinct Products",
#"Added M Type" = Table.AddColumn(Source, "M Type", each if fnDistinct(Source, [Serviceid]) > 1 then "Multiple Products" else "Single Product", type text)
in
#"Added M Type"
或使用 DAX:
DAX Type =
VAR CountProducts =
CALCULATE(
DISTINCTCOUNT ( Table1[Product] ),
ALLEXCEPT ( Table1, Table1[Serviceid] )
)
RETURN
SWITCH (
CountProducts,
1, "Single Product",
"Multiple Products"
)
工作示例 PBIX 文件:https://pwrbi.com/so_55918190/
性能测试 DAX 解决方案,250,000 行:
使用简单的子查询:
Select
Serviceid,
Product,
IIf((Select Count(*)
From YourTable As T
Group By Serviceid, Product
Having T.Serviceid = YourTable.Serviceid And T.Product = YourTable.Product) = 1, "Single Product", "Multiple Products") As [Type]
From
YourTable
Group By
Serviceid,
Product
我同时使用 Access 和 Power BI 来解决这个问题,但无济于事。 原则上其实很简单,但我需要以特定的方式来做,以便后续的定价计算。
本质上,我有一个 table 交易和服务 ID。此服务 ID 将在一个或多个产品中出现多次。如果它只是一个产品,那么我需要用 "Single Product" 填充一列,否则 "Multiple Product"。见下图:
Serviceid Product Type
1 A Multiple Products
1 B Multiple Products
2 A Single Product
3 A Single Product
3 A Single Product
3 A Single Product
您可以使用 IIF()
和 EXISTS
来填充新列
select
t.serviceid,
t.product,
iif(
exists (
select 1 from tablename where serviceid = t.serviceid and product <> t.product
),
'Multiple Products',
'Single Product'
) as type
from tablename as t
在 Power BI 中,您可以在 Power Query 中执行此操作:
let
Source = SourceTable,
fnDistinct = (MyTable, MyServiceid) =>
let
#"Filtered Rows" = Table.SelectRows(MyTable, each ([Serviceid] = MyServiceid)),
#"Distinct Products" = List.Count(List.Distinct(#"Filtered Rows"[Product]))
in
#"Distinct Products",
#"Added M Type" = Table.AddColumn(Source, "M Type", each if fnDistinct(Source, [Serviceid]) > 1 then "Multiple Products" else "Single Product", type text)
in
#"Added M Type"
或使用 DAX:
DAX Type =
VAR CountProducts =
CALCULATE(
DISTINCTCOUNT ( Table1[Product] ),
ALLEXCEPT ( Table1, Table1[Serviceid] )
)
RETURN
SWITCH (
CountProducts,
1, "Single Product",
"Multiple Products"
)
工作示例 PBIX 文件:https://pwrbi.com/so_55918190/
性能测试 DAX 解决方案,250,000 行:
使用简单的子查询:
Select
Serviceid,
Product,
IIf((Select Count(*)
From YourTable As T
Group By Serviceid, Product
Having T.Serviceid = YourTable.Serviceid And T.Product = YourTable.Product) = 1, "Single Product", "Multiple Products") As [Type]
From
YourTable
Group By
Serviceid,
Product