Forum Discussion

hgzelaya's avatar
hgzelaya
Icon for Helper I rankHelper I
5 years ago
Solved

Is this even possbile?

hi everyone! i have the following table

IDDATEITEMS PURCHASEDAVERAGE OF ITEMS PURCHASED IN THE LAST 2 MONTH BY ID
1mar-211020
1abr-212020
1may-212020
2jun-212030
2jul-213030
2ago-213030

 

What i need is a calculated column that shows me the average of items purchased in the last 2 dates by id. 

For example id#1 would be 20 items purchased and for id#2 would be 30. I'll leave the following link with the containing data below. Thank you! 🙂

https://docs.google.com/spreadsheets/d/1x3dLSifBYHCcuZTJ6UFaiFKRTdWjriAzu7f08TeioLI/edit?usp=sharing

 

 

  • Once you convert your mmm-yy column to a Date type, you can use this column expression to get your result.

     

    Last Two Avg =
    VAR thisID = Items[ID]
    VAR Last2Rows =
        TOPN ( 2FILTER ( Items, Items[ID] = thisID ), Items[DATE], DESC )
    RETURN
        AVERAGEX ( Last2Rows, Items[ITEMS PURCHASED] )

     

    Pat

3 Replies

  • mahoneypat's avatar
    mahoneypat
    Icon for Microsoft Employee rankMicrosoft Employee

    Once you convert your mmm-yy column to a Date type, you can use this column expression to get your result.

     

    Last Two Avg =
    VAR thisID = Items[ID]
    VAR Last2Rows =
        TOPN ( 2FILTER ( Items, Items[ID] = thisID ), Items[DATE], DESC )
    RETURN
        AVERAGEX ( Last2Rows, Items[ITEMS PURCHASED] )

     

    Pat

  • Hi,

    I do not understand your end result.  For ID1 in abr-21, how can the result be 20 (should it not be 15?).  Also, such a calculation should typically be a measure (not a calculated column).  Why do you want this to be a calculated column formula?