Power BI:分组导致多对多关系

时间:2019-11-15 14:12:55

标签: powerbi

我是Power BI的新手,我从SQL Server导入了3个表到Power BI Desktop:2个主表和1个多对多关系(桥接),我需要基于分组从第二个主表获取结果来自第一和第二个主表。

表是:客户,订单,OrderCustomers(桥接表)。订单表有一个SalesChannelId字段,我需要按销售渠道对每个客户的订单进行分组,并获得所有客户订单的百分比

我已经通过SQL查询实现了这一点(这是我擅长的事情):

select 
Customers.FirstName,
all_orders.orders_count,
SalesChannels.Name as SalesChannel,
COUNT(OrderCustomers.OrderId) as SaleChannelOrdersCount,
cast (((COUNT(OrderCustomers.OrderId) * 100) / all_orders.orders_count) as nvarchar(3)) +  '%' as SaleChannelOrdersPercent
from
Customers
inner join OrderCustomers
on OrderCustomers.CustomerId = Customers.Id
inner join Orders
on Orders.Id = OrderCustomers.OrderId
inner join SalesChannels
on SalesChannels.Id = Orders.SalesChannelId
inner join
(select 
Customers.id as customer_id,
COUNT(OrderCustomers.OrderId) as orders_count
from
Customers
inner join OrderCustomers
on OrderCustomers.CustomerId = Customers.Id
where 1=1
group by Customers.id) as all_orders
on all_orders.customer_id = Customers.Id
where 1=1
group by Customers.id, Customers.FirstName, SalesChannels.Name, all_orders.orders_count
order by Customers.id, SalesChannels.Name

通过此查询,我得到如下结果:

FirstName   orders_count    SalesChannel    SaleChannelOrdersCount  SaleChannelOrdersPercent
Adam           9              Online               1                           11%
Adam           9              Counter              8                           88%
Henrik         3              Counter              3                           100%
Mary           15             Online               3                           20%
Mary           15             Counter              12                          80%

如何使用Power BI获得相同的结果?

0 个答案:

没有答案