【问题标题】:DAX Power BI - Show latest record according to date filterDAX Power BI - 根据日期过滤器显示最新记录
【发布时间】:2021-06-09 08:42:13
【问题描述】:

我在 Power BI 模型中有 2 个表,[日期] 和 [配额]

  • “日期”[YYYYMM] 用作报告中的期间过滤器
  • 我想根据所选时间段的最大日期显示最新的配额

如何将其写入 DAX / 计算列?

Example:

If I select YYYYMM = "202005" in filter.
Below table should show:

|product|quota|
|A      |10   |
|B      |20   |

Quota Table:

|product|quota|effectiveDate|
|A      |10   |2020-01-01   |
|B      |20   |2020-01-01   |
|A      |25   |2021-01-01   |
Date Table:

|Date      |YYYYMM|
|2020-01-01|202001|
|2020-01-02|202001|
...
|2021-06-09|202106|

要获得最新的配额,在 SQL 中应该是:

SELECT TOP (1) q.Quota 
FROM [Quota] q 
LEFT JOIN [Date] d on d.[Date] >= q.effectiveDate 
ORDER BY q.effectiveDate desc

【问题讨论】:

    标签: powerbi dax


    【解决方案1】:

    在 DAX 中测量:

    FlagToFilter = 
    var __ChoicedDate = CALCULATE(MAX(DateTable[Date      ]), FILTER(ALL(DateTable), SELECTEDVALUE(DateTable[YYYYMM]) = DateTable[YYYYMM] ))
    var __MaxFor = CALCULATE(max(QuotaTable[effectiveDate]), FILTER(ALL(QuotaTable), QuotaTable[effectiveDate] <= __ChoicedDate && SELECTEDVALUE(QuotaTable[product])= QuotaTable[product] ))
    
    return
    CALCULATE( countrows(VALUES(QuotaTable[product])), FILTER(ALL(QuotaTable[effectiveDate]), SELECTEDVALUE(QuotaTable[effectiveDate]) = __MaxFor))
    

    【讨论】:

    • 非常感谢您的回答 msta42a,但如果从表中取出有效日期,则此措施似乎不起作用。实际上我需要这个度量作为其他 DAX 计算的基础,所以在最终情况下有效日期不会出现在表格中。
    • 我发现我们可以将返回语句更改为: CALCULATE(sum('quota'[quota]), filter(all(quota), 'quota'[effectiveDate] = _MaxFor), values( 'quota'[product])),我们可以简单地将产品和最新配额放入表中。感谢您的基地!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多