2016-03-05 75 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」標誌爲每個父記錄過濾單個子記錄,因此不需要分組。 –