我同时使用Access和Power BI来解决此问题,但无济于事。 原则上,这实际上很简单,但是我需要以特殊的方式进行后续的价格计算。
从本质上讲,我有一个带有服务ID的事务的表。一个或多个产品会多次出现此服务ID。如果只是一种产品,那么我需要在列中填充“单一产品”,否则要填充“多种产品”。参见下面的插图:
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
答案 0 :(得分:1)
在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/
25万行的性能测试DAX解决方案:
答案 1 :(得分:0)
您可以将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
答案 2 :(得分:0)
使用一个简单的子查询:
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