# DAX Formulas for Rolling 12 Month Ending Sums



## AlphaJava (Jul 17, 2015)

I am trying to calculate Rolling 12 months ending sums using DAX Formulas to calculate 12 months ending PMPMs (per member per month costs)  and PMPM Trends by month. The exhibit below is my data sample.  The PowerPivot Table is displayed in *BOLD*.  PMPM and PMPM Trends are fields I want to include in the pivot table using DAX formulas.  The excel formula for PMPM 12 months ending Dec-91 is the Sum of Payment from Jan-91 to Dec-91 divided by Sum of Enrollment for the same period for a result of $139.  This formula is copied down to Dec-93 (12 months ending PMPM of $163).  The PMPM Trends is simply a Year over Year change in PMPMs.  For example, the PMPM Trends 12 months ending Dec-92 of 7.2% is calculated as the 12 months ending Dec-92 PMPM of $149 divided by the 12 months ending PMPM a year ago (Dec-91) of $139 minus 1.  So the question is how calculate PMPM and PMPM Trends using DAX formulas.

Any help would be greatly appreciated.

Thanks.



*PowerPivot Table**Date**Enrollment**Payment*PMPMPMPM TrendsJan-91507,000$66,000,000Feb-91499,000$65,400,000Mar-91499,000$77,800,000Apr-91501,000$66,900,000May-91500,000$70,100,000Jun-91502,000$71,700,000Jul-91505,000$64,600,000Aug-91508,000$72,800,000Sep-91501,000$69,300,000Oct-91499,000$71,600,000Nov-91500,000$71,800,000Dec-91499,000$68,500,000$139Jan-92475,000$70,000,000$140Feb-92472,000$69,900,000$142Mar-92473,000$75,200,000$142Apr-92471,000$67,500,000$143May-92471,000$74,000,000$144Jun-92473,000$68,200,000$144Jul-92470,000$67,900,000$146Aug-92476,000$74,100,000$147Sep-92466,000$64,500,000$147Oct-92467,000$74,800,000$148Nov-92468,000$71,600,000$149Dec-92469,000$64,300,000$1497.2%Jan-93465,000$81,900,000$1517.8%Feb-93456,000$71,900,000$1527.3%Mar-93461,000$75,300,000$1537.4%Apr-93468,000$79,800,000$1558.4%May-93471,000$80,200,000$1568.2%Jun-93473,000$72,500,000$1578.6%Jul-93473,000$77,200,000$1588.6%Aug-93479,000$77,700,000$1598.2%Sep-93473,000$73,900,000$1609.2%Oct-93473,000$86,000,000$1629.4%Nov-93470,000$73,000,000$1628.9%Dec-93469,000$69,700,000$1639.5%

<colgroup><col><col><col><col><col></colgroup><tbody>

</tbody>


----------



## Matt Allington (Jul 17, 2015)

Well first you need a calendar table with an ID column.  Read about ID columns here  Power Pivot Calendar Tables -

create a 12 month rolling enrolment total like this

=calculate(sum(tablename[enrolment]),filter(all(calendar),calendar[id]>=min(calendar[id])-12 && calendar[id] <= max(calendar[id])))

use the the same principle to create a total payments number. Then divide them out. Finally you can work out the %


----------



## AlphaJava (Jul 18, 2015)

Matt:

Thank you very much for your response.  Your suggestion works very well. Great Stuff!! Really appreciate it.  Many thanks to you again.


----------



## AlphaJava (Jul 18, 2015)

Matt:

False alarm; after a closer look, the formula did not work after all.  If returns the same enrollment and payment amount by month not the sum of 12 months of enrollment or payments. Thanks anyways for your help; the formula is definitely a good start to getting the correct results that I am expecting.


----------



## Matt Allington (Jul 18, 2015)

I'm. It really sure what the problem is that you describe. This formula will take the current row in your pivot table and then go back in time 12 months and add up all the enrolment numbers to get a total. Actually the formula should be -11 and not -12.  So in Dec 91, that would be approximately 6 million


----------



## AlphaJava (Jul 18, 2015)

Thanks Matt. Yes I was expecting 6.02M for Dec-91, 5.99M for Jan-92, 5.96M for Feb-92, ...., 5.63M for Dec-93. I believe the issue is that the powerpivot table above is already a summarize table for enrollment and payments. So when I try to insert a calculated field for Rolling 12 Mos of enrollment using the formula, it requires using the detail table for enrollment not the summarized one displayed above. As a result, the formula gives the same amounts by month as noted in the summarized powerpivot table. I was hoping that the formula would have used only the information provided in the summarized pivot table and not the underlying data of the pivot table.  In regular pivot tables one can insert calculated fields using only the information in the pivot table and not the underlying data. It seems that is not as straightforward for powerpivot tables.  Thank you very much for you time; I really appreciate it.


----------



## Matt Allington (Jul 18, 2015)

Well I am sure I can help you, but I need to see the workbook, or a sample of it


----------



## scottsen (Jul 19, 2015)

Best guess:  If you made a Calendar table as Matt correctly recommends, make sure you drop fields from the Calendar table onto the pivot table (not the Dates from your fact/data table).  His formula looks good to me...


----------



## AlphaJava (Jul 19, 2015)

Matt:

I send you a sample of the workbook via email.  The data is a little different because I created the sample from scratch. However the formulas for the results remain the same.  Thanks in advance.


----------



## Matt Allington (Jul 22, 2015)

Thanks for sharing the file.  Here is the link for others.  https://dl.dropboxusercontent.com/u/30711565/PowerPivot_Question.xlsx 
There are a few issues.  
1. Firstly your tables are not connected the correct way.  I have added a sheet with before and after.    
2. Secondly the dates in your calendar table don't line up with the dates in your data tables.  The calendar table has to cover the range of your data.
3.  The third issue is that your months in your pivot table is coming from the wrong table.  You have to select months from the calendar table for your pivot.

When you fix all three - it works.  You may want to put some IF statements around your rolling total so that it returns blank for the first 11 months of data (where there is not a valid 12 month total).

Regards

Matt


----------



## AlphaJava (Jul 17, 2015)

I am trying to calculate Rolling 12 months ending sums using DAX Formulas to calculate 12 months ending PMPMs (per member per month costs)  and PMPM Trends by month. The exhibit below is my data sample.  The PowerPivot Table is displayed in *BOLD*.  PMPM and PMPM Trends are fields I want to include in the pivot table using DAX formulas.  The excel formula for PMPM 12 months ending Dec-91 is the Sum of Payment from Jan-91 to Dec-91 divided by Sum of Enrollment for the same period for a result of $139.  This formula is copied down to Dec-93 (12 months ending PMPM of $163).  The PMPM Trends is simply a Year over Year change in PMPMs.  For example, the PMPM Trends 12 months ending Dec-92 of 7.2% is calculated as the 12 months ending Dec-92 PMPM of $149 divided by the 12 months ending PMPM a year ago (Dec-91) of $139 minus 1.  So the question is how calculate PMPM and PMPM Trends using DAX formulas.

Any help would be greatly appreciated.

Thanks.



*PowerPivot Table**Date**Enrollment**Payment*PMPMPMPM TrendsJan-91507,000$66,000,000Feb-91499,000$65,400,000Mar-91499,000$77,800,000Apr-91501,000$66,900,000May-91500,000$70,100,000Jun-91502,000$71,700,000Jul-91505,000$64,600,000Aug-91508,000$72,800,000Sep-91501,000$69,300,000Oct-91499,000$71,600,000Nov-91500,000$71,800,000Dec-91499,000$68,500,000$139Jan-92475,000$70,000,000$140Feb-92472,000$69,900,000$142Mar-92473,000$75,200,000$142Apr-92471,000$67,500,000$143May-92471,000$74,000,000$144Jun-92473,000$68,200,000$144Jul-92470,000$67,900,000$146Aug-92476,000$74,100,000$147Sep-92466,000$64,500,000$147Oct-92467,000$74,800,000$148Nov-92468,000$71,600,000$149Dec-92469,000$64,300,000$1497.2%Jan-93465,000$81,900,000$1517.8%Feb-93456,000$71,900,000$1527.3%Mar-93461,000$75,300,000$1537.4%Apr-93468,000$79,800,000$1558.4%May-93471,000$80,200,000$1568.2%Jun-93473,000$72,500,000$1578.6%Jul-93473,000$77,200,000$1588.6%Aug-93479,000$77,700,000$1598.2%Sep-93473,000$73,900,000$1609.2%Oct-93473,000$86,000,000$1629.4%Nov-93470,000$73,000,000$1628.9%Dec-93469,000$69,700,000$1639.5%

<colgroup><col><col><col><col><col></colgroup><tbody>

</tbody>


----------



## AlphaJava (Jul 22, 2015)

Thanks very much Matt.


----------



## AlphaJava (Jul 23, 2015)

For the 12 Mo Rolling Enrollment, I want to return blank between dates 1/1/1991 and 11/1/1991 using the DATESBETWEEN() and blank() functions as shown in the formula below,  but the formula is returning blanks for all dates. What am I doing wrong?

=IF(CALCULATE(SUM ( Population[Enrollment] ), DATESBETWEEN(Calendar[Month], DATE(1991,1,1), DATE(1991,11,1) )), BLANK(), (CALCULATE (
    SUM ( Population[Enrollment] ),
    FILTER (
        ALL ( Calendar ),
        Calendar[DateID]
            >= MIN ( Calendar[DateID] ) - 11
            && Calendar[DateID] <= MAX ( Calendar[DateID] )
     )
 ) ))


----------

