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
wlknsn
Helper I
Helper I

Dax Measure: Average per last x items per group

Hi,

 

I'm looking for a measure to calculate the average sales of the last 3 items per product/group. Like below.

 

ProductOrderNumberSalesAveragePerLast3Items
A13 
A26 
A375,333333333
A445,666666667
B74 
B87 
B1286,333333333
B1456,666666667

 

Many thanks!

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

Hi @wlknsn

Create a calculated column

rank = RANKX(FILTER(ALL(Sheet10),[Product]=EARLIER(Sheet10[Product])),[OrderNumber],,ASC)

Then create a measure

Measure =
VAR selected =
    SELECTEDVALUE ( Sheet10[OrderNumber] )
VAR filtered =
    TOPN (
        3,
        FILTER (
            ALL ( Sheet10 ),
            [OrderNumber] <= selected
                && [Product] = SELECTEDVALUE ( Sheet10[Product] )
        ),
        [OrderNumber], DESC
    )
RETURN
    AVERAGEX ( filtered, [Sales] )

8.png

Finaly with this calculated column

Average per last x items per group = IF([rank]>=3,[Measure],BLANK())

 

10.jpg

 

Best Regards

Maggie

View solution in original post

2 REPLIES 2
v-juanli-msft
Community Support
Community Support

Hi @wlknsn

Create a calculated column

rank = RANKX(FILTER(ALL(Sheet10),[Product]=EARLIER(Sheet10[Product])),[OrderNumber],,ASC)

Then create a measure

Measure =
VAR selected =
    SELECTEDVALUE ( Sheet10[OrderNumber] )
VAR filtered =
    TOPN (
        3,
        FILTER (
            ALL ( Sheet10 ),
            [OrderNumber] <= selected
                && [Product] = SELECTEDVALUE ( Sheet10[Product] )
        ),
        [OrderNumber], DESC
    )
RETURN
    AVERAGEX ( filtered, [Sales] )

8.png

Finaly with this calculated column

Average per last x items per group = IF([rank]>=3,[Measure],BLANK())

 

10.jpg

 

Best Regards

Maggie

wlknsn
Helper I
Helper I

Anyone???

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.