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

Shape the future of the Fabric Community! Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions. Take survey.

Reply
apatwal
Helper III
Helper III

Calculate Last Transaction Records

Hi,

 

Within Power BI, I have a below table and I want to create another column which calculates Last Margin % for every records. 

apatwal_0-1643788701408.png

 

If the customer A buys product A12 then for its Last Margin % is null and if same customer (i.e. A) buys same product (i.e. A12) then its Margin % will 30%. So my result table will be as below.

apatwal_1-1643788869202.png

 

I want to write DAX for this. Can some one help me on this?

 

 

 

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@apatwal , A new column and new measure

 

new column =
var _max = maxx(filter(table, [Customer] = earlier([Customer]) && [Product] =earlier([Product]) && [Date] < earlier([Date])), [Date])
return
maxx(filter(table, [Customer] = earlier([Customer]) && [Product] =earlier([Product]) && [Date] _max), [Margin %])

 


new measure =
var _max = maxx(filter(allselected(table), [Customer] = max([Customer]) && [Product] =max([Product]) && [Date] < max([Date])), [Date])
return
maxx(filter(allselected(table), [Customer] = max([Customer]) && [Product] =max([Product]) && [Date] _max), [Margin %])

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

View solution in original post

7 REPLIES 7
v-eqin-msft
Community Support
Community Support

Hi @apatwal ,

 

1.Yes, I have modified my previous reply.

2. I think @amitchandak 's method is the most effective ,if there is a large data, you may try to apply some filters (like set a date period by using slicer) to reduce the query.

 

Best Regards,
Eyelyn Qin

wdx223_Daniel
Super User
Super User

Calculated Column=MAXX(TOPN(1,FILTER(Table,Table[Product]=Earlier(Table[Product])&&Table[Date]<EARLIER(Table[Date])),Table[Date]),Table[Margin%])

v-eqin-msft
Community Support
Community Support

Hi @apatwal ,

 

Firstly, you could follow @amitchandak 's suggestion.

 

Or try:

 

Column = 
var _previousDate= MAXX(FILTER('Table',[Product]=EARLIER('Table'[Product]) && [Customer]=EARLIER('Table'[Customer]) && [Date]<EARLIER('Table'[Date])),[Date]) 
return LOOKUPVALUE('Table'[Margin %],[Customer],[Customer],[Date],_previousDate) 

 

 

Eyelyn9_0-1644307805688.png

 

Best Regards,
Eyelyn Qin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

there is a bug in this code.

it always gets the max value of Margin%, instead of the Margin% of the max date.

 

Hi @wdx223_Daniel ,

 

Sorry for my mistake, you are right !

I have modified my previous reply. Thanks for reminding me😀

 

Best Regards,
Eyelyn Qin

Hi @v-eqin-msft 

 

Thanks for your reply!
Let me know if below DAX provided by you is updated and I can try to use your DAX:

Column = 
var _previousDate= MAXX(FILTER('Table',[Product]=EARLIER('Table'[Product]) && [Customer]=EARLIER('Table'[Customer]) && [Date]<EARLIER('Table'[Date])),[Date]) 
return LOOKUPVALUE('Table'[Margin %],[Customer],[Customer],[Date],_previousDate) 

 

As of now, I have used below DAX provided by @amitchandak 

new measure =
var _max = maxx(filter(allselected(table), [Customer] = max([Customer]) && [Product] =max([Product]) && [Date] < max([Date])), [Date])
return
maxx(filter(allselected(table), [Customer] = max([Customer]) && [Product] =max([Product]) && [Date] _max), [Margin %])

 

This works perfectly fine but with this DAX I am sometime facing issue "This visual has exceeded the available resources. Try filtering to decrease the amount of data displayed.", this may be due to large dataset present in our tables and this will also take long time to load as our data grows.

 

Any way to optimize this DAX query?

amitchandak
Super User
Super User

@apatwal , A new column and new measure

 

new column =
var _max = maxx(filter(table, [Customer] = earlier([Customer]) && [Product] =earlier([Product]) && [Date] < earlier([Date])), [Date])
return
maxx(filter(table, [Customer] = earlier([Customer]) && [Product] =earlier([Product]) && [Date] _max), [Margin %])

 


new measure =
var _max = maxx(filter(allselected(table), [Customer] = max([Customer]) && [Product] =max([Product]) && [Date] < max([Date])), [Date])
return
maxx(filter(allselected(table), [Customer] = max([Customer]) && [Product] =max([Product]) && [Date] _max), [Margin %])

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

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.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.