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 closed by day/month. Currently using Direct Query on 500k rows and additional columns to that shown below. Example of data (Close year of 9999 means account is still live):

 

IDLive DateClose DateLive MonthClose MonthStageVintage
128/05/201919/06/2019May 2019June 2019Pre Issue2010
229/11/201829/05/2019November 2018May 2019Post Issue2011
308/07/201931/07/2019July 2019July 2019Post Judgement2012
417/01/201921/02/2019January 2019February 2019Mid Proceedings2013
519/08/201812/03/2019August 2018March 2019Post Enforcement2014
618/05/201927/06/2019May 2019June 2019Pre Issue2015
714/07/201802/06/2019July 2018June 2019Post Issue2016
816/03/201901/01/9999March 2019January 9999Post Judgement2017
917/03/201928/06/2019March 2019June 2019Mid Proceedings2018
1023/10/201819/07/2019October 2018July 2019Post Enforcement2019
1105/05/201827/08/2018May 2018August 2018Pre Issue2010
1202/10/201829/08/2019October 2018August 2019Post Issue2011
1307/05/201908/08/2019May 2019August 2019Post Judgement2012
1425/07/201802/02/2019July 2018February 2019Mid Proceedings2013
1521/12/201825/04/2019December 2018April 2019Post Enforcement2014
1629/04/201830/06/2018April 2018June 2018Pre Issue2015

 

Ideally I need the solution to show by month how many live accounts there are, and to have this linked back to the dataset so additional columns can be used in slicers so the output changes. I have produced exactly how I would do this in Excel, so I am hopeful this can be done in DAX as well.

 

Thanks in advance!Example in ExcelBase Data

  • 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!

6 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Sounds like you can avoid having to create a new measure!

     

    Simply create a new chart in Power BI with the Close Month as your x axis. For your values, put in a count of "Live Month". Then to only show live accounts, filter the visual by "Close Month" to only show the January 2099 values. You should be able to recreate the two charts you have in Excel!

     

    Hope this helps!

    • TwiggyHaz's avatar
      TwiggyHaz
      Frequent Visitor

      Cheers for the reply! Have attached my results, both by just showing the 9999s and also by excluding them. Can't get the same results unless I have done somthing incorrect?

      Just 9999sExcluding 9999s

      • Anonymous's avatar
        Anonymous
        Not applicable

        My mistake! Use "Live Month" for your x-axis, and then use count of "Live Month" for your values. This way you are counting the amount of Live Accounts were created in that month. Then place "Close Month" as a visual filter and filter to only show the 2099's, limiting the data to only show live accounts that have not been closed

         

        Hope this works!

  • TwiggyHaz's avatar
    TwiggyHaz
    Frequent Visitor

    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!