Forum Discussion

bgierwi2's avatar
bgierwi2
Advocate I
7 months ago
Solved

Calculating Daily Compounding Penalities

Trying to get this to calculate in Power Bi Power Query or Dax. I'm not sure which would be better, or is it possible to do it in both?   I have a data set that has a daily car count, with a targe...
  • AshokKunwar's avatar
    AshokKunwar
    7 months ago

    bgierwi2 

     

    Your current running total is "Global," meaning it counts every car regardless of the group. To make groups accumulate penalties independently, the DAX needs to "partition" the calculation by the Group name.

    The Solution: Group-Aware DAX logic

    Step 1: Update the Running Total (The "Partition" Logic)

    ​Replace your previous RunningTotalOver column with this version. It adds a second filter condition to only sum values where the Group matches the current row.

    RunningTotalOver = 
    CALCULATE(
        SUM('Car Counts'[CarsOverTarget]),
        FILTER(
            'Car Counts',
            'Car Counts'[Group] = EARLIER('Car Counts'[Group]) && 
            'Car Counts'[Date] <= EARLIER('Car Counts'[Date])
        )
    )

     

    Step 2: The Penalty Measure (No changes needed, but for reference)

    ​Your penalty measure will now automatically work correctly because it relies on the RunningTotalOver. Since that total now resets for each group, the _StartOccurrence and _EndOccurrence variables will start back at 1 for "Red," "Green," or any future groups

    Penalty Cost / Day = 
    VAR _CurrentTotal = 'Car Counts'[RunningTotalOver]
    VAR _DailyOver = 'Car Counts'[CarsOverTarget]
    VAR _StartOccurrence = _CurrentTotal - _DailyOver + 1
    VAR _EndOccurrence = _CurrentTotal
    
    RETURN
    IF(
        _DailyOver > 0,
        SUMX(
            GENERATESERIES(_StartOccurrence, _EndOccurrence),
            VAR _ID = [Value]
            RETURN
            IF(_ID <= 2, 500, 500 + (INT(DIVIDE(_ID - 3, 3)) + 1) * 500)
        ),
        0
    )

     

    Why this fixes the final detail:

    • Independence: The Group = EARLIER(Group) part of the formula acts like a "Partition By" clause in SQL. It creates a "silo" for each color.
    • Scalability: If you add "Group Yellow" or "Group Green" tomorrow, the formula will automatically start a new 1-to-1000 sequence for them without any code changes.
    • Accuracy: It handles the transition perfectly. Even if Group Blue and Group Red have data on the same date (though you mentioned they don't currently), this logic would still keep their penalty buckets separate.

     

    Since this completes your requirements for independent group tracking, please mark this as the "Accepted Solution"! This should be your final badge for this thread!