Forum Discussion

TwiggyHaz's avatar
TwiggyHaz
Frequent Visitor
6 years ago
Solved

Measure for running total between two dates

Hello DAX experts!   This issue seems fairly above my abilities in PowerBI so hoping for some help. I am looking to find the running total of accounts in my data where any accounts are live and not...
  • TwiggyHaz's avatar
    6 years ago

    I initially solved this with a brute force method in SQL, creating a new table with a row for each account for each month live and running a countrows over this. While this worked and allowed interactivity the table itself was massive and would grow by millions of rows each year.

     

    Have just cracked this using DAX:

     

    1. Add a date table to the model, for this example just the date and month name was needed.

     

    2. Create a active relationship from Live Date to the Date column in the date table

     

    3. Create an inactive relationship from Close Date to the Date column in the date table

     

    4. Add the following measure:

     

    Running Live = 
    VAR RunningPlacedTotal =
        CALCULATE (
            COUNTA ( 'Base Data'[Live Month] ),
            FILTER (
                ALL ( 'Dates Base'[MonthName] ),
                ISONORAFTER ( 'Dates Base'[MonthName], MAX ( 'Dates Base'[MonthName] ), DESC )
            )
        )
    VAR RunningCloseTotal =
        CALCULATE (
            COUNTA ( 'Base Data'[Close Month] ),
            USERELATIONSHIP ( 'Base Data'[Close Date], 'Dates Base'[DateID] ),
            FILTER (
                ALL ( 'Dates Base'[MonthName] ),
                ISONORAFTER ( 'Dates Base'[MonthName], MAX ( 'Dates Base'[MonthName] ), DESC )
            )
        )
    
    RETURN RunningPlacedTotal - RunningCloseTotal

    5. Add graph with measure to values, and the date table month to the axis.

     

    6. Add a visual level relative date filter to match your needs.

     

    Hope this helps!