当前上下文之外的DAX SUM

时间:2016-11-02 12:17:28

标签: sql dax

这是我目前的数据模型 - > part of data model

我需要帮助来创建一个衡量标准。

我想要创建的措施应该显示在一个时间间隔内出行的船舶的总容量,并且能够根据房间类型查看总容量。

我有兴趣获得相当于DAX的DAX:

SELECT SUM(sc.capacity)
FROM Reporting.Trips t
    JOIN Reporting.Dates d ON d.DateId = t.DateId
    JOIN Reporting.ShipCapacities sc on sc.ShipId = t.ShipId
WHERE d.FiscalYear= 2017 AND d.FiscalWeek=15

每种类型的OR

SELECT SUM(sc.capacity)
FROM Reporting.Trips t
    JOIN Reporting.Dates d ON d.DateId = t.DateId
    JOIN Reporting.ShipCapacities sc on sc.ShipId = t.ShipId
WHERE d.FiscalYear= 2017 AND d.FiscalWeek=15 and sc.CabinTypeId = 11

我试图以这种方式定义一个度量:

Total Rooms:=CALCULATE ( SUM ( 'Ship Capacities'[Ship Capacity] ) )

我已经看到它输出的T-SQL相当于:

SELECT SUM(Capacity)
FROM 
(
SELECT DISTINCT sc.*
FROM Reporting.Trips t
    JOIN Reporting.Dates d ON d.DateId = t.DateId
    JOIN Reporting.ShipCapacities sc on sc.ShipId = t.ShipId
WHERE d.FiscalYear= 2017 AND d.FiscalWeek=15
) x

每种类型的OR

SELECT SUM(Capacity)
FROM 
(
SELECT DISTINCT sc.*
FROM Reporting.Trips t
    JOIN Reporting.Dates d ON d.DateId = t.DateId
    JOIN Reporting.ShipCapacities sc on sc.ShipId = t.ShipId
WHERE d.FiscalYear= 2017 AND d.FiscalWeek=15 and sc.CabinTypeId = 11
) x

这些是查询结果 - > Query results

感谢。

1 个答案:

答案 0 :(得分:0)

这是对我有用的最终措施:

Total Rooms:=CALCULATE (
    SUMX (
        'Ship Capacities',
        'Ship Capacities'[Ship Capacity]
            * COUNTROWS ( FILTER ( Trips, AND( Trips[Sailed] = TRUE (),Trips[ShipId] = 'Ship Capacities'[ShipId] ) ))
    )
)

注意COUNTROWS。 在多对一的情况下,然后是一对多的关系,它没有迭代(但之前我使用的是SUM而不是SUMX)。 我发现不得不提到多对多表之间的关系。 否则它不起作用。