You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在DAX中基于计算日期显示最近的有效计算值?

问题描述

我有一个名为Apples75th%的度量值卡片,搭配Calculated Date下拉切片器。由于部分数据每7天更新一次,当切片器选择最近7天的日期时,卡片会显示BLANK:

  • 选择2023/02/08(最近7天内的日期),Apples75th%显示BLANK;
  • 选择2023/02/01(距上次更新回溯7天的日期),该度量值显示8.88%。

当前使用的DAX公式:

Apples75th% = 
IF( 
    SELECTEDVALUE(produce[Product Type]) = "Apples", 
    produce[75apple_percentile],
    produce[75thallproducts_percentile]
)

需求:当切片器选中的日期导致卡片值为BLANK时,自动显示最近的有效历史计算值。

解决方案

可以通过引入变量存储当前值、查找最近有效日期并计算对应值的方式实现需求,修改后的DAX公式如下:

Apples75th% = 
VAR CurrentValue = 
    IF( 
        SELECTEDVALUE(produce[Product Type]) = "Apples", 
        produce[75apple_percentile],
        produce[75thallproducts_percentile]
    )
VAR SelectedDate = SELECTEDVALUE('Date'[Calculated Date]) -- 替换为你的日期表实际字段名
VAR LastValidDate = 
    CALCULATE(
        LASTNONBLANK('Date'[Calculated Date], 
            IF( 
                SELECTEDVALUE(produce[Product Type]) = "Apples", 
                produce[75apple_percentile],
                produce[75thallproducts_percentile]
            )
        ),
        'Date'[Calculated Date] <= SelectedDate
    )
VAR LastValidValue = 
    CALCULATE(
        IF( 
            SELECTEDVALUE(produce[Product Type]) = "Apples", 
            produce[75apple_percentile],
            produce[75thallproducts_percentile]
        ),
        'Date'[Calculated Date] = LastValidDate
    )
RETURN
    IF(NOT ISBLANK(CurrentValue), CurrentValue, LastValidValue)

公式说明

  1. CurrentValue:保留原公式逻辑,计算当前选中日期对应的度量值;
  2. SelectedDate:获取切片器选中的日期,注意替换成你实际使用的日期表(或produce表)中的日期字段;
  3. LastValidDate:通过LASTNONBLANK函数,在选中日期之前的范围内,找到第一个能计算出非空度量值的日期;
  4. LastValidValue:基于找到的最近有效日期,重新计算对应的度量值;
  5. 返回逻辑:如果当前选中日期有有效值则直接返回,否则返回最近的有效历史值。

注意事项

如果你的日期字段直接存储在produce表中而非单独的日期表,需要将公式中的'Date'[Calculated Date]替换为produce[Calculated Date],确保筛选上下文正确。

内容的提问来源于stack exchange,提问作者Will

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 01:40:37