2016-03-05 61 views
0

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

在此先感谢您的帮助!

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 
               ) 
            ) 

          ) 
        ) 

回答

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 

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

+0

在这种情况下,你假设只有一个子id对应于父ID,但这不是案子。有一个父ID的多个子ID。问题是,在当前的代码逻辑下,父ID被分组(SUMX子句)。我试图用child id替换这个父id,然后在ISCurrent = 1中添加一个过滤器,但仍得到不同的结果集。 – Dennyc

+0

你误解了我的假设。有一个父母身份证有多个孩子ID。所以,请再次检查我的答案。并且...如果您有标识当前子记录的“IsCurrent”标志,为什么需要使用分组功能?解决方案I提供了基于“IsCurrent”标志为每个父记录过滤单个子记录,因此不需要分组。 –