【问题标题】:Calculated column in DAX to show current BusinessAreaDAX 中的计算列以显示当前的 BusinessArea
【发布时间】:2015-11-12 13:58:35
【问题描述】:

我的 SSAS 模型中有一个具有 SCD 类型 2 功能的表。

CustNr      StartDate      EndDate       BusinessArea
123         2014-12-01     2015-01-01    Norway
123         2015-01-01     -             Sweden

我需要一个显示当前 BusinessArea(基于客户编号)的计算列(DAX)。我该如何处理?我听说过“早期”功能,但我是 DAX 新手,无法理解它。

想要的输出应该是这样的:

CustNr      StartDate      EndDate       BusinessArea      CurrentBA
123         2014-12-01     2015-01-01    Norway            Sweden
123         2015-01-01     -             Sweden            Sweden

欢迎所有答案!干杯!

【问题讨论】:

    标签: sql-server excel ssas powerpivot dax


    【解决方案1】:

    LOOKUPVALUE()

    (编辑:请注意原始左侧的连续性 - 编辑部分下方的正确测量)

    CurrentBusinessArea =
    LOOKUPVALUE(
        DimCustomer[BusinessArea] // Lookup column - will return value
                                  // matching search criteria below.
        ,DimCustomer[CustNr]      // Search column 1.
        ,DimCustomer[CustNr]      // Value to match to search column 1 -
                                  // this is evaluated in row context.
        ,DimCustomer[EndDate]     // Search column 2.
        ,"-"                      // Literal value to match for search
                                  // column 2.
    )
    

    我怀疑您的 [EndDate] 实际上是一个文本字段,因此我也怀疑表示当前业务区域的行的 [EndDate] 的文字值实际上是“-”。如果为空,请使用 BLANK() 函数而不是文字“-”。

    根据评论编辑,通过一些讨论更正了度量定义:

    CurrentBusinessArea =
    LOOKUPVALUE(
        DimCustomer[BusinessArea]
        ,DimCustomer[CustNr]
        ,DimCustomer[CustNr]
        ,DimCustomer[EndDate]
        ,DATE(BLANK(),BLANK(),BLANK())
    )
    

    通常在 DAX 中,您可以直接测试是否与 BLANK() 相等。它的行为往往与 SQL 中的 NULL 不同。实际上,您可以创建一个列来对此进行测试。如果您执行其中任何一项操作,它们会为带有空白 [EndDate] 的行返回 true:

    =DimCustomer[EndDate] = BLANK()
    =ISBLANK(DimCustomer[EndDate])
    =DimCustomer[EndDate] = 0  //implicit conversion 0 = blank
    

    由于某种原因,从日期/时间到 BLANK() 的转换存在问题。上面的结构,使用带有所有 BLANK()s 的 DATE() 函数对我有用。我曾假设 LOOKUPVALUE() 可以与文字空白一起使用(有趣的是,如果数据类型是整数,则 LOOKUPVALUE() 可以与 BLANK() 一起使用)。对此深表歉意。

    【讨论】:

    • 我试过了,但它只显示空白值。而你右边的 EndDate 是一个日期数据类型,所以我使用了空白()。 = LOOKUPVALUE( DimCustomer[BusinessArea] ;DimCustomer[CustomerNr] ;DimCustomer[CustomerNr] ;DimCustomer[EndDate] ;blank() )
    猜你喜欢
    • 2018-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-07
    • 2023-04-04
    • 1970-01-01
    • 2023-02-09
    相关资源
    最近更新 更多