首页 文章

DAX中的递归逻辑

提问于
浏览
2

我正在尝试创建一个取决于前一行结果的计算列,并且我遇到了循环依赖问题 .

我在源表中有一个索引和一个值,我正在创建如下计算:

TestValue = [Value] +
    CALCULATE(sum([Value]), 
        filter(all(Table), [Index] < earlier([Index]))) + 
                      // running total of previous Value's
    CALCULATE(sum([TestResult]), 
        filter(all(Table), [Index] < earlier([Index]))) 
                      // running total of previous TestResult's

TestResult = if([TestValue] > 10000, [Value])

我认为问题在于,即使我在 index < earlier (index) 上进行了过滤,它仍然在评估 TestResult 以查找大于当前索引的索引 .

我该怎么做才能让它发挥作用?

Here是成功实现递归的链接,但仅限于将先前结果乘以百分比的情况 .

编辑:

对于结果应该是什么似乎有些混淆,所以我将尝试说明期望的结果:

原始表

| Index | Value |
-----------------
| 1     | 500   |
| 2     | 12000 |
| 3     | 24000 |

预期结果

| Index | Value | TestResult | TestValue
----------------------------------------
| 1     | 500   | NULL       | NULL
| 2     | 12000 | 12000      | 500
| 3     | 15000 | 15000      | 500 + 12000 + 12000 = 24500

第三行是通过索引为1和2的“值”的运行总计加上“TestResult”的运行总计来计算的 .

1 回答

  • 0

    唯一的选择是分解您的查询,以便没有循环依赖 .

    这应该返回您之后的结果:

    TempValue = [value] + 
      CALCULATE(sum([value]), 
        filter(all('Table'), 
        [index] < earlier([index])))
    
    TestResult = if([TempValue] > 10000, [Value],0)
    
    TestValue = [TempValue] + 
      CALCULATE(sum([TestResult]), 
        filter(all('Table'), 
        [Index] < earlier([Index])))
    

相关问题