How to use filter when creating calculated column in Power BI?

你。 提交于 2021-01-29 13:16:48

问题


Filter cannot be used in calculated columns, then how can I simply create a column in DAX that would pick up a value based on maximum claimID and maximum DateCreated ?

The result should be like this:


回答1:


Filters can be used in calculated columns. Just don't expect them to be responsive to slicers. In this case, it is probably more efficient computationally not to use a FILTER function in favor of simple CALCULATE arguments:

TotalIncurredMaxDate =
VAR MaxDate =
    CALCULATE (
        MAX ( Table1[DateCreated] ),
        ALLEXCEPT ( Table1, Table1[ControlNo], Table1[ClaimID] ),
        Table1[TotalIncurred] > 0
    )
RETURN
    IF ( Table1[DateCreated] = MaxDate, Table1[TotalIncurred], 0 )



回答2:


Calculated columns have a "row context" of the current row, and no filter context, as they are calculated during model load, before any filters are applied. But you can create filters in your calculation. EG

TotalIncurredMaxDate = 
    var claimId = Table1[ClaimID]
    var controlNo = Table1[ControlNo]

    var maxDate = calculate(
        max(Table1[DateCreated]), 
        filter (
          all (Table1), 
          Table1[ClaimID]=claimId
          && Table1[ControlNo] = controlNo
          && Table1[TotalIncurred] > 0
        )
    )

    var retval = if(Table1[DateCreated] = maxDate, Table1[TotalIncurred], 0)

    return retval 


来源:https://stackoverflow.com/questions/57699280/how-to-use-filter-when-creating-calculated-column-in-power-bi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!