Forum Discussion

corodyne's avatar
corodyne
Frequent Visitor
9 years ago
Solved

% change (month-over-month) crashes when aggregating

I've been working on this for a couple days, only coming to half a solution.  I'm hoping one of the many gurus here can look at my file and tell me what I'm doing wrong.  I'm not afraid of doing the work, just at a loss as to how to make the desired result.

 

Problem statement:  given a table of sales data (see format below), present a graph like the one on powerbi.tips (month-on-month % change) -- well, it could be any date range for that matter...

 

To date, I've tried a variety of solutions, including attempting the "second table with dates" option, and have had no luck creating a % change graph.  I have been able to create a table that shows the % change, but only when the rows are set day-by-day (see image at the bottom).  As soon as I aggregate, either with bins or with date heirarchy, the "previous month" stops working, % change goes to infinity, and any graph derived from the same becomes a flat line.

 

I'm hoping one of your smart folks can tell me where I've gone awry.  Alternately, if one of you brave souls wants to interact more directly, please send me a PM and I'll email you.  

Thanks,  Chris S.

 

Example File

 

Problem Month-on-Month Data file

 

Code Sample (all measures)

 

Current Monthly Sales = CALCULATE([Total Unit Sales],PARALLELPERIOD('product-sales-by-day'[date],0,MONTH))

 

Prior Month Sales = CALCULATE([Total Unit Sales], PREVIOUSMONTH('product-sales-by-day'[date]))

 

% Change = ([Current Monthly Sales]-[Prior Month Sales])/[Prior Month Sales]

 

Example output

 

 

 

Data Sample

 

datesale_idunits_solditem_id
1/1/2015 0:00101365134
1/3/2015 0:00171680310
1/4/2015 0:005610290134
1/4/2015 0:00250186270142
1/4/2015 0:00337196140149
1/4/2015 0:00273191100150
1/4/2015 0:0011224255249
1/4/2015 0:0010942000257
1/4/2015 0:0020446988289
1/4/2015 0:00597505995290
1/4/2015 0:00409660310
1/4/2015 0:003140314
1/4/2015 0:0068471820332
1/6/2015 0:001076877695149
1/6/2015 0:00730533085150
1/6/2015 0:00362152670249
1/6/2015 0:0022572450257
1/6/2015 0:00549119263289
1/6/2015 0:0017561545915290
1/6/2015 0:0019243995310
1/6/2015 0:006210314
1/6/2015 0:0010411025332
1/7/2015 0:0011819845134
1/7/2015 0:00509381780142
    
  • corodyne

     

    Hi, My propose to solution is:

     

    1. Add a Calendar Table

     

    From Modeling - New Table

     

    Calendar=CalendarAuto()

     

    2.  The measures

     

    Current Monthly Sales = CALCULATE([Total Unit Sales])

     

    Prior Month Sales = CALCULATE([Total Unit Sales]; PREVIOUSMONTH('Calendar'[date]))

     

    3. The Visual

     

     

2 Replies

  • Vvelarde's avatar
    Vvelarde
    Icon for Community Champion rankCommunity Champion

    corodyne

     

    Hi, My propose to solution is:

     

    1. Add a Calendar Table

     

    From Modeling - New Table

     

    Calendar=CalendarAuto()

     

    2.  The measures

     

    Current Monthly Sales = CALCULATE([Total Unit Sales])

     

    Prior Month Sales = CALCULATE([Total Unit Sales]; PREVIOUSMONTH('Calendar'[date]))

     

    3. The Visual

     

     

    • corodyne's avatar
      corodyne
      Frequent Visitor

      Vvelarde

       

      Thanks very much for the help.  Your fix was just what I needed.  I went back in with some calendar table help from this site (Calendar Table in Two Steps), but I had to replace the semicolons with commas in my version of PowerBI.

       

      Also, I realized my graphs were going to infinity because a single row was in error.  This code fixed it:

       

      % Change =

      // the % change variable can't be infinity or it throws off the graphs
       if(  ISERROR(([Current Monthly Sales]-[Prior Month Sales])/[Prior Month Sales]),
        BLANK(),
        ([Current Monthly Sales]-[Prior Month Sales])/[Prior Month Sales]
       )


      The final result is just what I needed.

       

      Solution file is here for others to use:  Solution File