Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Sum Values by condition for the previous months

Hi all   I am trying to replicate this logic in Power BI. I have a column 'Fees' where I would like to sum the values in it when the condition 'Banked' is Yes (Y). But when the condition is met I w...
  • Anonymous's avatar
    Anonymous
    6 years ago

    Here's a much simpler measure that works in all circumstances:

    Measure = 
    var __tableToIterateOver =
        filter(
            'Table',
            'Table'[BANKED] = "Y"
        )
    var __amount = 
        SUMX(
            __tableToIterateOver,
            var __currentDate = 'Table'[Date]
            var __result =
                calculate(
                
                    var __prevDateOfBankedStatus =
                        CALCULATE(
                            MAX( 'Table'[Date] ),
                            'Table'[BANKED] = "Y",
                            'Table'[Date] < __currentDate
                        )				
                    var __output =
                        CALCULATE(
                            SUM( 'Table'[FEES] ),
                            __prevDateOfBankedStatus < 'Table'[Date],
                            'Table'[Date] <= __currentDate
                        )
                    return
                        __output,
    
                    // Leave only the filter on Fund.
                    ALLEXCEPT( 'Table', 'Table'[Fund] )
                )
            return
                __result
        )
    return
        __amount

     

    Best

    D