【问题标题】:DAX Code change suggestionDAX 代码更改建议
【发布时间】:2016-03-05 00:33:48
【问题描述】:

所以我有以下 DAX 代码作为度量值。我想要做的是用另一列BillDetail [SourceServiceMapID] 替换Billdetail [SOurceWasteServiceID]。但问题是,对于单个 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
                                                )
                                    )

                            )
                    )

【问题讨论】:

    标签: sql ssas dax


    【解决方案1】:

    您知道...您提供的示例看起来不仅是与将最新记录连接到某些“基本”记录有关的问题,但是...如果尽管如此,我们可以“玩”这个问题一点点。只是为了好玩。

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

    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
    

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

    并针对它编写 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
    

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

    【讨论】:

    • 在这种情况下,您假设只有一个子 id 对应于父 id,但事实并非如此。单个父 ID 有多个子 ID。而问题是在当前的代码逻辑下,父id被分组(SUMX子句)。我尝试用子 ID 替换此父 ID,然后在 ISCurrent= 1 中添加了一个过滤器,但我仍然得到不同的结果集。
    • 你误解了我的假设。单个父 ID 有多个子 ID。所以,请再次检查我的答案。而且...如果您有标识当前子记录的“IsCurrent”标志,为什么需要使用分组功能?解决方案我根据“IsCurrent”标志为每个父记录提供过滤器单个子记录,因此不需要分组。
    猜你喜欢
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 2017-09-16
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多