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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
thollitez
Regular Visitor

Need help SUM or AVERAGE last 7 Previous Records (Not Using Date)

Hi Guys,

 

I've been checking the forum and google regarding SUM or AVERAGE last specific number of records.

All solutions i've found is using DATE, but DATE is irrelevant on what i am trying to attain.

 

What i am really trying to attain is get the AVERAGE of last 7 records regardless of the dates.

 

Please see example below.

 Capture.PNG

 

Thanks in advance.

 

1 ACCEPTED SOLUTION
v-piga-msft
Resident Rockstar
Resident Rockstar

Hi @thollitez,

 

For your scenario, you should create an index column in Query Editor and create the measure below.

 

1. Create Index column

 

add index.PNG

 

2. Create the measure under Modeling tab.

Moving Average last 7 Records =
VAR SevenrecordsTotal =
    CALCULATE (
        SUM ( 'table'[UNITS SOLD] ),
        FILTER (
            ALL ( 'table'[Index] ),
            'table'[Index] <= MAX ( 'table'[Index] )
                && 'table'[Index]
                    >= MAX ( 'table'[Index] ) - 6
        ),
        ALL ( 'table' )
    )
VAR Records =
    CALCULATE (
        DISTINCTCOUNT ( 'table'[Index] ),
        FILTER (
            ALL ( 'table'[Index] ),
            'table'[Index] <= MAX ( 'table'[Index] )
                && 'table'[Index]
                    >= MAX ( 'table'[Index] ) - 6
                && 'table'[Index] <> BLANK ()
        ),
        ALL ( 'table' )
    )
RETURN
    IF ( SUM ( 'table'[Index] ) >= 7, DIVIDE ( SevenrecordsTotal, Records ), BLANK () )

 

 

Then you could get the output below.

average last 7 records.PNG

 

You also could refer to the attached pbix

 

Best Regards,

Cherry

Community Support Team _ Cherry Gao
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

3 REPLIES 3
v-piga-msft
Resident Rockstar
Resident Rockstar

Hi @thollitez,

 

For your scenario, you should create an index column in Query Editor and create the measure below.

 

1. Create Index column

 

add index.PNG

 

2. Create the measure under Modeling tab.

Moving Average last 7 Records =
VAR SevenrecordsTotal =
    CALCULATE (
        SUM ( 'table'[UNITS SOLD] ),
        FILTER (
            ALL ( 'table'[Index] ),
            'table'[Index] <= MAX ( 'table'[Index] )
                && 'table'[Index]
                    >= MAX ( 'table'[Index] ) - 6
        ),
        ALL ( 'table' )
    )
VAR Records =
    CALCULATE (
        DISTINCTCOUNT ( 'table'[Index] ),
        FILTER (
            ALL ( 'table'[Index] ),
            'table'[Index] <= MAX ( 'table'[Index] )
                && 'table'[Index]
                    >= MAX ( 'table'[Index] ) - 6
                && 'table'[Index] <> BLANK ()
        ),
        ALL ( 'table' )
    )
RETURN
    IF ( SUM ( 'table'[Index] ) >= 7, DIVIDE ( SevenrecordsTotal, Records ), BLANK () )

 

 

Then you could get the output below.

average last 7 records.PNG

 

You also could refer to the attached pbix

 

Best Regards,

Cherry

Community Support Team _ Cherry Gao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thank you very much sir v-piga-msft, it really solved my problem.

 

You're the best.

Hi @thollitez,

 

It's glad that we can help. You're welcome.Smiley Very Happy

 

Best Regards,

Cherry

Community Support Team _ Cherry Gao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.