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

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

 

 

1 ACCEPTED SOLUTION
mahoneypat
Employee
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





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

3 REPLIES 3
Ashish_Mathur
Super User
Super User

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?


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
mahoneypat
Employee
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





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Worked as needed. Thank you very much!

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.