DAX代码更改建议

时间:2016-03-05 00:33:49

标签: sql ssas dax

所以我有一个下面的DAX代码用于测量。我想要做的是将Billdetail [SOurceWasteServiceID]替换为另一列BillDetail [SourceServiceMapID]。但问题是,对于单个SourceWasteServiceID,我可以有多个SourceServiceMapID记录。由于数据必须组合在一起,我不能直接用另一个替换。该表在表中有一个IsCurrent标志,对于最新记录是“1”。我试图在Filter语句中使用这个IsCurrent,但我仍然得到不匹配的数据。  有人建议我怎么改变这个?

提前感谢您的帮助!!

 Sum of Volume:=CALCULATE(
                        SUMX(
                                     Summarize(BillDetail
                                                                ,BillDetail[SourceWasteServiceID]
                                                                ,BillDetail[ActualBillMonth]
                                                                ,WasteServiceMap[ContainerCount]
                                                                ,WasteServiceMap[WasteContainerSizeQuantity]
                                                                ,WasteServiceMap[WasteContainerSizeUnit]
                                                                ,WasteServiceMap[WastePickupSchedule]
                                                                ,WasteServiceMap[WastePickupFrequencyMultiplier]
                                                                ,WasteServiceMap[PercentFull]
                                                                ,WasteServiceMap[CompactionRatio]
                                                                ,"ItemQuantity", CALCULATE(Sum(BillDetail[ActualItemQuantity]),BillDetail[AlternateBillDetailKey] = True)
                                                                )
                                 ,IF ( UPPER((WasteServiceMap[WastePickupSchedule])) = "FIXED" 
                                            ,(WasteServiceMap[ContainerCount])
                                            * (WasteServiceMap[WasteContainerSizeQuantity])  
                                            *(IF(WasteServiceMap[WastePickupFrequencyMultiplier] = -1,0,WasteServiceMap[WastePickupFrequencyMultiplier]))  
                                            * (WasteServiceMap[PercentFull]) 
                                            * (WasteServiceMap[CompactionRatio]) 
                                            *IF(UPPER((WasteServiceMap[WasteContainerSizeUnit])) = "GALLONS"
                                                    , 0.00495113169 
                                                    , IF(UPPER((WasteServiceMap[WasteContainerSizeUnit])) = "LITERS"
                                                            , 0.00130795062
                                                            ,IF(UPPER((WasteServiceMap[WasteContainerSizeUnit])) = "YARDS"  
                                                                    ,1
                                                                    ,BLANK())
                                                          )
                                                 )

                                            , IF ( OR(OR(OR(UPPER((WasteServiceMap[WastePickupSchedule])) = "ON CALL" ,UPPER((WasteServiceMap[WastePickupSchedule])) = "MAILBACK"),UPPER((WasteServiceMap[WastePickupSchedule])) = "HAND PICKUP"),UPPER((WasteServiceMap[WastePickupSchedule])) = "SCHEDULED ONCALL")
                                                    , (WasteServiceMap[WasteContainerSizeQuantity])  
                                                        * (WasteServiceMap[CompactionRatio]) 
                                                        * (WasteServiceMap[PercentFull]) 
                                                        * ([ItemQuantity])
                                                        *IF(UPPER((WasteServiceMap[WasteContainerSizeUnit])) = "GALLONS"
                                                            , 0.00495113169 
                                                            , IF(UPPER((WasteServiceMap[WasteContainerSizeUnit])) = "LITERS"
                                                                    , 0.00130795062
                                                                    ,IF(UPPER((WasteServiceMap[WasteContainerSizeUnit])) = "YARDS"  
                                                                            ,1
                                                                            ,BLANK())
                                                                  )
                                                         )
                                                    , 0
                                                )
                                    )

                            )
                    )

1 个答案:

答案 0 :(得分:0)

你知道......你提供的例子看起来不像是一个与将某些“基础”记录加入最新记录有关的问题,但是......如果尽管如此,我们可以“解决”这个问题一点点。只是为了好玩。

假设我们的数据库中有两个非常简单的表

create table parent_table
(
    parent_id int identity(1, 1) primary key,
    some_value nvarchar(100)
);

create table child_table
(
    child_id int identity(1, 1) primary key,
    parent_id int,
    is_current bit,
    some_value nvarchar(100)
);

带有一些毫无意义但相关的数据

insert into parent_table (some_value) 
values ('value 1'),('value 2'),('value 3'),('value 4');

insert into child_table (parent_id, is_current, some_value) values 
(1, 1, 'value 1.1'),
(2, 0, 'value 2.1'),
(2, 0, 'value 2.2'),
(2, 1, 'value 2.3'),
(3, 0, 'value 3.1'),
(3, 1, 'value 3.2'),
(4, 0, 'value 4.1'),
(4, 1, 'value 4.2');

并且......我们想要找到每个父行的当前子数据。 如果我们在T-SQL上写了一个查询,它可能看起来像这样

select p.parent_id
, p.some_value [parent_value]
, c.some_value [current_child_value]
from parent_table p
left join child_table c on p.parent_id = c.parent_id
    and c.is_current = 1;



(4 row(s) affected)

parent_id   parent_value    current_child_value
-----------------------------------------------
1           value 1         value 1.1
2           value 2         value 2.3
3           value 3         value 3.2
4           value 4         value 4.2

现在我们可以尝试在这些表的顶部构建一些简单的表格模型 enter image description here

并针对它编写DAX查询

evaluate
filter (
    addcolumns(
        child_table,
        "parent_value", related(parent_table[some_value])
    ),
    child_table[is_current] = True
)

收到与使用T-SQL

几乎相同的结果
child_table[child_id]   child_table[parent_id]  child_table[is_current] child_table[some_value] [parent_value]
------------------------------------------------------------------------------------------------------------------
8                       4                       True                    value 4.2               value 4
6                       3                       True                    value 3.2               value 3
4                       2                       True                    value 2.3               value 2
1                       1                       True                    value 1.1               value 1

我希望它能够帮助您解决问题,或者至少可以指出您正确的方向