Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Rolling 12 months average

Hello,

 

I know there are a number of questions on Rolling 12 months already, However, i was not able to find one that fits my needs.

I need to create a bar chart with months on x-axis and values on y-axis as mentioned below:

Show last 12 months until previous month (Since we are in Feb now, the chart should show Jan-21 to Feb-20 on x-axis)

Calculation:

Each bar in the chart should display value as Jan-21 = (Jan-21 + Dec-20 +…. + Mar-20 + Feb-20)/12

Similarly for Dec-20 = (Dec-20 + Nov-20 +…. + Feb-20 + Jan-20)/12

Nov-20 = (Nov-20 + Oct-20 +…. + Jan-20 + Dec-19)/12

.

.

.

Feb-20 = (Feb-20 + Jan-20 +…. + Apr-19 + Mar-19)/12

 

Source Data:

Month-YearValue
Jan-1950
Feb-1930
Mar-1910
Apr-1960
May-1930
Jun-1920
Jul-1910
Aug-1910
Sep-1940
Oct-1970
Nov-1940
Dec-1950
Jan-2060
Feb-2030
Mar-2040
Apr-2060
May-2020
Jun-2030
Jul-2070
Aug-2010
Sep-2030
Oct-2050
Nov-2020
Dec-2030
Jan-2150
Feb-2120

 

Required (in a bar chart):

 SumSum/12
Feb-2043035.83333
Mar-2046038.33333
Apr-2046038.33333
May-2045037.5
Jun-2046038.33333
Jul-2052043.33333
Aug-2052043.33333
Sep-2051042.5
Oct-2049040.83333
Nov-2047039.16667
Dec-2045037.5
Jan-2144036.66667

 

Thank you in advance.

1 ACCEPTED SOLUTION
v-yangliu-msft
Community Support
Community Support

Hi  @Anonymous ,

v-yangliu-msft_0-1612830867112.png

 

Here are the steps you can follow:

1. Create measure.

Sum =
var _1=
IF(MAX([month])<>12,
CALCULATE(MAX('Table'[Month-Year]),FILTER(ALL('Table'),'Table'[Year]=MAX('Table'[Year])-1&&'Table'[month]=MAX('Table'[month])+1)),
DATE(YEAR(MAX([Month-Year])),1,1))

var _2=
CALCULATE(
    SUM('Table'[Value]),
    FILTER(ALL('Table'),[Month-Year]>=_1&&[Month-Year]<=MAX([Month-Year])))
    return _2
Sum/12 =
var _1=
IF(MAX([month])<>12,
CALCULATE(MAX('Table'[Month-Year]),FILTER(ALL('Table'),'Table'[Year]=MAX('Table'[Year])-1&&'Table'[month]=MAX('Table'[month])+1)),
DATE(YEAR(MAX([Month-Year])),1,1))

var _2=
CALCULATE(
    SUM('Table'[Value]),
    FILTER(ALL('Table'),[Month-Year]>=_1&&[Month-Year]<=MAX([Month-Year]))) 
    return _2/12

2. Result

v-yangliu-msft_1-1612830867119.jpeg

 

You can downloaded PBIX file from here.

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

7 REPLIES 7
Ashish_Mathur
Super User
Super User

Hi,

When you say "Show last 12 months until previous month", then for the December 2020 column of the chart shouldn't the range for calculation be 1 December 2019 to 30 November 2020?  Why have you have mentioned the range as 1 January 2020 to 31 December 2020?

See if my Blog here helps - Flex a Pivot Table to show data for x months ended a certain user defined month.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

Hi Ashish,

 

For the calculation of sum, it has to consider 12 months backwards including the respective month i.e.,

for Jan 2021 = Jan 21 + Dec 20 + Nov 20 + ........ + Feb 20

However, in the chart, i need only the last 12 months starting from previous month on the x-axis i.e., since we are in Feb 21 now, the chart should show data for Jan 21, Dec 20, Nov 20..... Feb 20

v-yangliu-msft
Community Support
Community Support

Hi  @Anonymous ,

v-yangliu-msft_0-1612830867112.png

 

Here are the steps you can follow:

1. Create measure.

Sum =
var _1=
IF(MAX([month])<>12,
CALCULATE(MAX('Table'[Month-Year]),FILTER(ALL('Table'),'Table'[Year]=MAX('Table'[Year])-1&&'Table'[month]=MAX('Table'[month])+1)),
DATE(YEAR(MAX([Month-Year])),1,1))

var _2=
CALCULATE(
    SUM('Table'[Value]),
    FILTER(ALL('Table'),[Month-Year]>=_1&&[Month-Year]<=MAX([Month-Year])))
    return _2
Sum/12 =
var _1=
IF(MAX([month])<>12,
CALCULATE(MAX('Table'[Month-Year]),FILTER(ALL('Table'),'Table'[Year]=MAX('Table'[Year])-1&&'Table'[month]=MAX('Table'[month])+1)),
DATE(YEAR(MAX([Month-Year])),1,1))

var _2=
CALCULATE(
    SUM('Table'[Value]),
    FILTER(ALL('Table'),[Month-Year]>=_1&&[Month-Year]<=MAX([Month-Year]))) 
    return _2/12

2. Result

v-yangliu-msft_1-1612830867119.jpeg

 

You can downloaded PBIX file from here.

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Thank you for the solution, this works just right.

amitchandak
Super User
Super User

@Anonymous , Try measure like this with date table

 

divide( CALCULATE([sum],DATESINPERIOD('DateTable'[Date ],MAX('DateTable'[Date]),-12,MONTH)) ,
CALCULATE(distinctCOUNT('DateTable'[Month Year]),DATESINPERIOD('DateTable'[Date],MAX('DateTable'[Date]),-12,MONTH), not(isblank([sum]))))

 

To get the best of the time intelligence function. Make sure you have a date calendar and it has been marked as the date in model view. Also, join it with the date column of your fact/s. Refer :radacad sqlbi My Video Series Appreciate your Kudos.

Anonymous
Not applicable

Hi Amit,

 

Thank you for the response.

I tried your measure, but i get this error in DAX

DivyaAnand_0-1612522902849.png

 

Could you please explain what is the denominator part of your formula trying to calculate?

i.e., CALCULATE(distinctCOUNT('DateTable'[Month Year]),DATESINPERIOD('DateTable'[Date],MAX('DateTable'[Date]),-12,MONTH), not(isblank([sum]))) 

@Anonymous , Try like

 


divide( CALCULATE([sum],DATESINPERIOD('DateTable'[Date ],MAX('DateTable'[Date]),-12,MONTH)) ,
CALCULATE(distinctCOUNT('DateTable'[Month Year]),DATESINPERIOD('DateTable'[Date],MAX('DateTable'[Date]),-12,MONTH), filter('Table',not(isblank([sum])))))

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.