如何在PowerBI中动态更改DATEADD函数的间隔类型?

时间:2019-01-17 13:28:49

标签: powerbi dax

我想让我的用户能够选择是否将当前数据与上个月或上一年的数据进行比较。此DAX度量是用PowerBI编写的。以下代码段使用用户选择的“ MarketShare”和“匹配”来确定计算结果,并使用用户选择的“比较”来确定是否与上个月或上一年进行比较,但是效率不高,因为我重复了“ ScriptTypeVar '用于月份和年份版本的变量。

Previous Amount Scripted = 
VAR SelectedMatch = FIRSTNONBLANK(Match[Match],1)
VAR SelectedMarketShare = FIRSTNONBLANK('Market Share'[Market Share],1)
VAR SelectedCompare = FIRSTNONBLANK('Compare'[Compare],1)


VAR PreviousMonthScriptTypeVar = SWITCH(TRUE(),
                SelectedMarketShare = "Quantity", CALCULATE(SUM(Scripts_Fact[Pack Quantity]),DATEADD('Date_Dimension'[Date], -1, ,MONTH)),
                SelectedMarketShare = "Quantity (Repeats)", CALCULATE(SUM(Scripts_Fact[Pack Quantity (Repeats)]),DATEADD('Date_Dimension'[Date], -1, ,MONTH)),
                SelectedMarketShare = "Value", CALCULATE(SUM(Scripts_Fact[Value]),DATEADD('Date_Dimension'[Date], -1, ,MONTH)),
                SelectedMarketShare = "Value (Repeats)", CALCULATE(SUM(Scripts_Fact[Value (Repeats)]),DATEADD('Date_Dimension'[Date], -1, ,MONTH))
            )

VAR PreviousYearScriptTypeVar = SWITCH(TRUE(),
                SelectedMarketShare = "Quantity", CALCULATE(SUM(Scripts_Fact[Pack Quantity]),DATEADD('Date_Dimension'[Date], -1, ,YEAR)),
                SelectedMarketShare = "Quantity (Repeats)", CALCULATE(SUM(Scripts_Fact[Pack Quantity (Repeats)]),DATEADD('Date_Dimension'[Date], -1, ,YEAR)),
                SelectedMarketShare = "Value", CALCULATE(SUM(Scripts_Fact[Value]),DATEADD('Date_Dimension'[Date], -1, ,YEAR)),
                SelectedMarketShare = "Value (Repeats)", CALCULATE(SUM(Scripts_Fact[Value (Repeats)]),DATEADD('Date_Dimension'[Date], -1, ,YEAR))
            )

VAR CompareTypeVar = SWITCH(TRUE(),
                SelectedCompare = "Previous Month", PreviousMonthScriptTypeVar,
                SelectedCompare = "Same Month Previous Year", PreviousYearScriptTypeVar
            )


VAR AmountScriptedVar = 
IF(AND(HASONEVALUE('Calendar'[Year]),HASONEVALUE('Calendar'[Month])),
                                SWITCH(TRUE(),
                                    SelectedMatch = "A", CALCULATE(CompareTypeVar,USERELATIONSHIP('Reporting_Products'[A], Product_Dimension[A])),
                                    SelectedMatch = "B", CALCULATE(CompareTypeVar,USERELATIONSHIP(Reporting_Products[B], Product_Dimension[B])),
                                    SelectedMatch = "C", CALCULATE(CompareTypeVar,USERELATIONSHIP('Reporting_Products'[C], Product_Dimension[C])),
                                    SelectedMatch = "D", CALCULATE(CompareTypeVar,USERELATIONSHIP('Reporting_Products'[D], Product_Dimension[D]))
                                    ),
                                [AmountScripted]
                            )
RETURN
AmountScriptedVar

我尝试使用'CompareTypeVar'开关返回MONTH或YEAR关键字,并按如下所示简化测量:

Previous Amount Scripted = 
VAR SelectedMatch = FIRSTNONBLANK(Match[Match],1)
VAR SelectedMarketShare = FIRSTNONBLANK('Market Share'[Market Share],1)
VAR SelectedCompare = FIRSTNONBLANK('Compare'[Compare],1)

VAR CompareTypeVar = SWITCH(TRUE(),
                SelectedCompare = "Previous Month", MONTH,
                SelectedCompare = "Same Month Previous Year", YEAR
            )

VAR ScriptTypeVar = SWITCH(TRUE(),
                SelectedMarketShare = "Quantity", CALCULATE(SUM(Scripts_Fact[Pack Quantity]),DATEADD('Date_Dimension'[Date], -1, ,CompareTypeVar)),
                SelectedMarketShare = "Quantity (Repeats)", CALCULATE(SUM(Scripts_Fact[Pack Quantity (Repeats)]),DATEADD('Date_Dimension'[Date], -1, ,CompareTypeVar)),
                SelectedMarketShare = "Value", CALCULATE(SUM(Scripts_Fact[Value]),DATEADD('Date_Dimension'[Date], -1, ,CompareTypeVar)),
                SelectedMarketShare = "Value (Repeats)", CALCULATE(SUM(Scripts_Fact[Value (Repeats)]),DATEADD('Date_Dimension'[Date], -1, ,CompareTypeVar))
            )               

VAR AmountScriptedVar = IF(AND(HASONEVALUE('Calendar'[Year]),HASONEVALUE('Calendar'[Month])),
                                SWITCH(TRUE(),
                                    SelectedMatch = "Generic Code", CALCULATE(ScriptTypeVar,USERELATIONSHIP('Reporting_Products'[Medprax Generic Code], Product_Dimension[Medprax Generic Code])),
                                    SelectedMatch = "ATC5 Code", CALCULATE(ScriptTypeVar,USERELATIONSHIP(Reporting_Products[ATC5 Code], Product_Dimension[ATC5 Code])),
                                    SelectedMatch = "ATC4 Code", CALCULATE(ScriptTypeVar,USERELATIONSHIP('Reporting_Products'[ATC4 Code], Product_Dimension[ATC4 Code])),
                                    SelectedMatch = "ATM Link", CALCULATE(ScriptTypeVar,USERELATIONSHIP('Reporting_Products'[ATM Link], Product_Dimension[ATM Link]))
                                    ),
                                [AmountScripted]
                            )
RETURN
AmountScriptedVar

...但是出现以下错误:

The last argument must be one of these keywords: DAY, MONTH, QUARTER, or YEAR.

我已经搜索了,但是找不到动态更改interval关键字的解决方案。我还尝试在DATEADD函数中使用if语句直接返回关键字,而不使用变量,但这返回了相同的错误。任何帮助将不胜感激。

尽管初始版本有效,但将来的项目可能会为用户提供更多选择,并且需要对更多的虚拟变量进行编码。如果每个用户的选择都可以由switch语句处理并结合起来以创建最终的计算(如第二版所示),那将是理想的选择。

1 个答案:

答案 0 :(得分:0)

我不确定是否可以将最后一个参数设置为变量。我建议采用以下方法。

Previous Amount Scripted =
<...>
VAR CompareDatesTable =
    SWITCH (
        TRUE (),
        SelectedCompare = "Previous Month",
        DATEADD ( 'Date_Dimension'[Date], -1, MONTH ),
        SelectedCompare = "Same Month Previous Year",
        DATEADD ( 'Date_Dimension'[Date], -1, YEAR)
    )
VAR ScriptTypeVar =
    SWITCH (
        TRUE (),
        SelectedMarketShare = "Quantity",
        CALCULATE (
            SUM ( Scripts_Fact[Pack Quantity] ),
            CompareDatesTable
        ),
        <...>
    )
<...>

在上面,我将整个表CompareDatesTable传递到CALCULATE函数中。