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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Anonymous
Not applicable

DAX find previous non blank value based on several criteria

Hi I have the following table  that contains orders and transactions within several days. Please note that not all orders become transactions and that transaction time does not always follow order time (latest order may be served before newest).

 

Order DateOrder TimeOrder PriceTransaction TimeTransaction Value 
1/1/202113:30:120.514:20:550.38
1/1/202115:20:100.416:15:080.39
2/1/202108:12:020.38  
2/1/202109:15:430.3611:20:430.36
2/1/202110:13:450.3513:55:350.39
2/1/202111:17:060.3312:00:45 
5/1/202120:23:200.3820:23:25 

 

So, I'm trying to create an extra column (or measure) that holds, for every order, the previous price. Previous price is the price of the latest transaction, at the time of the order. If there haven't been any transactions during current day, Previous Price is the value of the latest transaction of previous day. Please note that there are days with no transactions at all, so we may need to look several days before.

 

The expected result would look like

Order DateOrder TimeOrder PriceTransaction TimeTransaction Value Previous PriceExplanation
1/1/202113:30:120.514:20:550.38 Start -> no transactions yet
1/1/202115:20:100.416:15:080.390.38latest transaction on 15:20:10
2/1/202108:12:020.38  0.39latest transaction of the previous day
2/1/202109:15:430.3611:20:430.360.39On 09:15:43 no transactions yet, get latest of previous day
2/1/202110:13:450.3513:55:350.390.39On 10:13:45 no transactions yet, get latest of previous day
2/1/202111:47:060.3312:00:45 0.36On 11:47:06 get price of previous transaction (effected on 11:20:43)
5/1/202120:23:200.3820:23:25 0.39

No transactions for days get the latest transaction of 2/1/2021 (13:55:35)

 


I' ve tried to use lastnonblank, filter with earlier but with no success so far.

I would greatly appreciate any help.

Thanks in advance!

1 ACCEPTED SOLUTION
ryan_mayu
Super User
Super User

@Anonymous 

you can create three colums

Orderdatetime = 'Table'[Order Date]+'Table'[Order Time]

transactiondatetime = 
VAR _date=maxx(FILTER('Table','Table'[Order Date]<EARLIER('Table'[Order Date])),'Table'[Order Date])
return if(ISBLANK('Table'[Transaction Time]),_date+maxx(FILTER('Table','Table'[Order Date]=_date),'Table'[Transaction Time]),'Table'[Order Date]+'Table'[Transaction Time])

previousprice = 
VAR _time='Table'[Orderdatetime]
VAR _previous= MAXX(FILTER(all('Table'),'Table'[transactiondatetime]<=_time&&not(ISBLANK('Table'[Transaction Value ]))),'Table'[transactiondatetime])
return maxx(FILTER('Table','Table'[transactiondatetime]=_previous),'Table'[Transaction Value ])

1.PNG

please see the attachment below





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

1 REPLY 1
ryan_mayu
Super User
Super User

@Anonymous 

you can create three colums

Orderdatetime = 'Table'[Order Date]+'Table'[Order Time]

transactiondatetime = 
VAR _date=maxx(FILTER('Table','Table'[Order Date]<EARLIER('Table'[Order Date])),'Table'[Order Date])
return if(ISBLANK('Table'[Transaction Time]),_date+maxx(FILTER('Table','Table'[Order Date]=_date),'Table'[Transaction Time]),'Table'[Order Date]+'Table'[Transaction Time])

previousprice = 
VAR _time='Table'[Orderdatetime]
VAR _previous= MAXX(FILTER(all('Table'),'Table'[transactiondatetime]<=_time&&not(ISBLANK('Table'[Transaction Value ]))),'Table'[transactiondatetime])
return maxx(FILTER('Table','Table'[transactiondatetime]=_previous),'Table'[Transaction Value ])

1.PNG

please see the attachment below





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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