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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
SaCvP125
Frequent Visitor

Create a rolling average metric by Week

Hi guys,

 

I've this dataset:

Session TypeFilm_IDStart_DateEnd_Date
ActionFI1006924532013-09-04 12:40:29.0002013-09-04 15:56:18.000
ActionFI1006924792013-09-04 14:21:27.0002013-09-06 16:56:03.000
ActionFI1006925032013-09-04 15:07:53.0002013-09-04 15:31:39.000
ActionFI1006925162013-09-04 15:41:28.0002013-09-06 10:12:25.000
ActionFI1006925292013-09-04 16:08:34.0002013-09-04 17:07:08.000
ActionFI1006925382013-09-04 16:11:10.0002013-09-04 17:28:22.000
ActionFI1006925412013-09-04 16:13:25.0002013-09-06 10:12:25.000
ActionFI1006925992013-09-04 19:41:49.0002013-09-04 20:23:15.000
ActionFI1006926472013-09-05 01:05:18.0002013-09-05 03:48:43.000
ActionFI1006927372013-09-05 10:27:06.0002013-09-05 10:44:05.000
ActionFI1006928352013-09-05 15:20:12.0002013-09-06 12:10:45.000
ActionFI1006928842013-09-05 19:42:58.0002013-09-05 23:58:35.000
ActionFI1006929642013-09-06 02:22:51.0002013-09-06 02:28:08.000
ActionFI1006931002013-09-06 12:22:54.0002013-09-06 13:31:22.000
ActionFI1006931032013-09-06 12:23:00.0002013-09-06 13:10:43.000

 

Basically this represents a dataset from a cinema that shows the Films_ID session opened and closed date.

I'm tryint to create tow measures to insert into a Line and Clustered Column Chart:
Columns:
- Number of Sessions Open in Week
- Number of Sessions Close in Week
Lines (Rolling Average):
- Average number of Films_ID open per week (based on last 6 months)
- Average number of Films_ID closed per week (based on last 6 months)

 

To calculate the Columns Measures I create the Column Year, Month and Week for Open and Close Dates. To calculate the Number of Sessions Close in Week I use this formula:

Close_Sessions = SUMX(SUMMARIZE('Movies_Data';Movies_Data[Week_End_Date];Movies_Data[Month_End_Date];Movies_Data[Year_End_Date];"CloseMovies";DISTINCTCOUNT(Movies_Data[Film_ID])); [CloseMovies])

 

Don't know if this formula is the best to calculate the measure that I want. But my question is:

 

How can I calculate the Rolling Average based on my data and the measures that I create above?

 

Thanks for your support 🙂

 

1 ACCEPTED SOLUTION
v-ljerr-msft
Microsoft Employee
Microsoft Employee

Hi @SaCvP125,

 

If I understand you correctly, you can firstly create a YearMonth column for for Open and Close Dates.

YearMonth_End_Date = Movies_Data[Year_End_Date]*12+Movies_Data[Month_End_Date]

Then you should be able to use the formula below to create a new measure to calculate the Rolling Average in your scenario.

Rolling Average = 
VAR Close_Sessions_Last_6_Month =
    CALCULATE (
        DISTINCTCOUNT ( Movies_Data[Session_ID] ),
        FILTER (
            ALL ( Movies_Data ),
            Movies_Data[Week_End_Date] <= MAX ( Movies_Data[Week_End_Date] )
                && Movies_Data[YearMonth_End_Date]
                    >= MAX ( Movies_Data[YearMonth_End_Date] ) - 6
        )
    )
VAR Weeks_Last_6_Month =
    CALCULATE (
        DISTINCTCOUNT ( Movies_Data[Week_End_Date] ),
        FILTER (
            ALL ( Movies_Data ),
            Movies_Data[Week_End_Date] <= MAX ( Movies_Data[Week_End_Date] )
                && Movies_Data[YearMonth_End_Date]
                    >= MAX ( Movies_Data[YearMonth_End_Date] ) - 6
        )
    )
RETURN
    DIVIDE ( Close_Sessions_Last_6_Month, Weeks_Last_6_Month, 0 )

r1.PNG

 

Here is the sample pbix file for your reference. Smiley Happy

 

Regards

View solution in original post

14 REPLIES 14
v-ljerr-msft
Microsoft Employee
Microsoft Employee

Hi @SaCvP125,

 

If I understand you correctly, you can firstly create a YearMonth column for for Open and Close Dates.

YearMonth_End_Date = Movies_Data[Year_End_Date]*12+Movies_Data[Month_End_Date]

Then you should be able to use the formula below to create a new measure to calculate the Rolling Average in your scenario.

Rolling Average = 
VAR Close_Sessions_Last_6_Month =
    CALCULATE (
        DISTINCTCOUNT ( Movies_Data[Session_ID] ),
        FILTER (
            ALL ( Movies_Data ),
            Movies_Data[Week_End_Date] <= MAX ( Movies_Data[Week_End_Date] )
                && Movies_Data[YearMonth_End_Date]
                    >= MAX ( Movies_Data[YearMonth_End_Date] ) - 6
        )
    )
VAR Weeks_Last_6_Month =
    CALCULATE (
        DISTINCTCOUNT ( Movies_Data[Week_End_Date] ),
        FILTER (
            ALL ( Movies_Data ),
            Movies_Data[Week_End_Date] <= MAX ( Movies_Data[Week_End_Date] )
                && Movies_Data[YearMonth_End_Date]
                    >= MAX ( Movies_Data[YearMonth_End_Date] ) - 6
        )
    )
RETURN
    DIVIDE ( Close_Sessions_Last_6_Month, Weeks_Last_6_Month, 0 )

r1.PNG

 

Here is the sample pbix file for your reference. Smiley Happy

 

Regards

@v-ljerr-msftmany thanks for your response 🙂 I'm getting this error when I try your solution into my data:

 

The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value.

Hi @SaCvP125,

 

Could you post the formula of the measure you're using? Smiley Happy

 

Regards

Ashish_Mathur
Super User
Super User

Hi,

 

On the X-axis of your visual, do you want to see the wek number?  Please confirm.


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

Yes 😉

Hi,

 

Based on the table that you have pasted above, please show (in a simple table) what the result (numbers) should be for each of the 4 measures that you want to compute.


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

Using Excel, I've this simple summarize table:

Year_EndMonth_EndWeek_EndCount of Film_IDMoving_Average
20178324 
20179364 
201793744
201793834
201793983,5
2017104045,5
2017104166
2017104275

 

That allows me to create this chart:

Moving_Average.PNG

This creates two measures from the four  that I want:
- Number of Filmes ended by Week
- Rolling Average from the last two 6 weeks

Hi,

 

In the original dataset that you pasted, all End dates fall in week number 36 so even after i write my measure for the rolling 6 months average, i will not be able to test my result.  Paste the dataset from where you have built your expected result table.  


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

The dataset from the original post was only a sample. It only have 2 weeks to... That's why I use another one to show you my goal.

How are you calculating the Moving Average?

So, i will need a dataset whch spans multiple weeks/months so that i can compute the rolling 6 months average week over week.  Please share a dataset which spans multiple months.  I'll try to use the Date/time intelligence functions to get the desired result.


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

Thanks! For your support. I extract a sample from the last 6 months

 

Session_IDStart_DateEnd_Date
FI64651722/05/201722/05/2017
FI62254520/03/201706/04/2017
FI63960708/05/201708/05/2017
FI66264029/06/201729/06/2017
FI70649811/10/201711/10/2017
FI62759203/04/201703/04/2017
FI62266220/03/201720/03/2017
FI66922412/07/201712/07/2017
FI69012229/08/201729/08/2017
FI67735731/07/201731/07/2017
FI68575420/08/201720/08/2017
FI66746809/07/201709/07/2017
FI69009129/08/201729/08/2017
FI63324519/04/201719/04/2017
FI65278004/06/201704/06/2017
FI67162818/07/201718/07/2017
FI69344206/09/201707/09/2017
FI65791421/06/201721/06/2017
FI68380414/08/201721/08/2017
FI70725712/10/201712/10/2017
FI69700717/09/201717/09/2017
FI64860827/05/201727/05/2017
FI67258819/07/201719/07/2017
FI70146827/09/201727/09/2017
FI70184128/09/201728/09/2017
FI65988526/06/201726/06/2017
FI66499603/07/201703/07/2017
FI69699617/09/201717/09/2017
FI66387429/06/201730/06/2017
FI64213213/05/201713/05/2017
FI69098731/08/201731/08/2017
FI67362121/07/201725/07/2017
FI66184428/06/201729/06/2017
FI62191218/03/201720/03/2017
FI66181828/06/201728/06/2017
FI68419516/08/201716/08/2017
FI64213913/05/201713/05/2017
FI70532508/10/201708/10/2017
FI62859106/04/201706/04/2017
FI67865303/08/201703/08/2017
FI68590421/08/201721/08/2017
FI69201703/09/201706/09/2017
FI67977805/08/201705/08/2017
FI69343006/09/201704/10/2017
FI66326129/06/201729/06/2017
FI64160012/05/201712/05/2017
FI62682831/03/201731/03/2017
FI63624927/04/201728/04/2017
FI63357420/04/201720/04/2017
FI69314206/09/201706/09/2017
FI62516126/03/201727/03/2017
FI65660617/06/201718/06/2017
FI67311420/07/201720/07/2017
FI66453301/07/201701/07/2017
FI62510626/03/201727/03/2017
FI68618221/08/201721/08/2017
FI67053216/07/201716/07/2017
FI63987408/05/201708/05/2017
FI68203610/08/201711/08/2017

Hi @SaCvP125,

 

See of my solution here helps.

 


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

@Ashish_MathurAshish_Mathur where I can see you solution? 🙂

Hi @SaCvP125,

 

See my solution here.


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

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors