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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
asimpson22
New Member

Find closest date before transaction date based on unique ID

Hello!

 

I am a fairly new user to Power Query and am running into an issue. I have been asked to create an excel report where for all cancelled orders we include the previous sale date. From the most basic standpoint, I am transforming the below 2 tables into the third table.

 

asimpson22_1-1672594508215.png

 

There are a few nuances to this though:

 

1) A lot can have multiple cancellations. For each cancellation, I will need to identify the most previous order date related to that specific lot.

 

2) A lot can cancel and resell on the same day. This one is more tricky as we don't have the time stamps for these specific transactions, just the date. A work around that I can think of would be to track if the difference between orders and cancellations is 0 or 1 (gross orders - cancels = 0 then the first transaction of the day must be an order for this lot else a cancel).

 

I have tried using the following solution from another forum post:

asimpson22_2-1672594884048.png

While I believe this solution is very close to what I'm looking for, it does not take into account the UniqueID. It would also neglect to capture the challenge of issue number 2.

 

I currently have a query for cancellations and a separate query for gross orders with a unique ID and transaction date within each.

 

Any help would be greatly appreciated! Please let me know if more information is required.

1 ACCEPTED SOLUTION

Hi @asimpson22 

 

You may create a new query with below code. 

let
    Source = Table.NestedJoin(CancelTable, {"SalesOffice", "Lot", "UniqueID"}, SalesTable, {"SalesOffice", "Lot", "UniqueID"}, "SalesTable", JoinKind.LeftOuter),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each let __CancelDate = [CancelDate] in Table.FirstN(Table.Sort(Table.SelectRows([SalesTable], each [Sale Date] <= __CancelDate), {{"Sale Date", Order.Descending}}),1)),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"SalesOffice", "Lot", "UniqueID", "Transaction Type", "CancelDate", "Custom"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"Sale Date"}, {"Orig. Sale Date
"})
in
    #"Expanded Custom"

Result:

vjingzhang_0-1672724883767.png

 

The problem is that from the current sample data, we cannot know which one is a record of a lot cancelling and then reselling on the same day, and which one is a record of a lot resold and then cancelled on the same day. There is no difference in the sample data. So you will find that for the example of Sales Office D, the output is not meeting your expected result. 

 

I have attached a sample file at bottom. 

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it. Highly appreciate your Kudos!

View solution in original post

3 REPLIES 3
ronrsnfld
Super User
Super User

An example that can be copy/pasted, also showing the issue of multiple order/cancellations on the same date, would be useful.

Thanks for your reply! Here are some sample tables and descriptions below. The first 2 tables are what I'm working with and the third table is the result I'm looking for. 

 

SalesOfficeLotUniqueIDTransaction TypeCancelDate
A1A1Cancel12/2/2022
B2B2Cancel12/2/2022
C3C3Cancel12/2/2022
A1A1Cancel11/1/2022
D4D4Cancel10/1/2022
E5E5Cancel9/1/2022
E5E5Cancel8/15/2022

 

SalesOfficeLotUniqueIDTransaction TypeSale Date
A1A1Gross Order11/15/2022
B2B2Gross Order2/1/2022
C3C3Gross Order3/1/2022
A1A1Gross Order10/1/2022
D4D4Gross Order10/1/2022
D4D4Gross Order9/1/2022
E5E5Gross Order9/1/2022
E5E5Gross Order8/1/2022

 

SalesOfficeLotUniqueIDTransaction TypeCancelDateOrig. Sale Date
A1A1Cancel12/2/20225/25/2022
B2B2Cancel12/2/20222/17/2022
C3C3Cancel12/2/20221/17/2022
A1A1Cancel11/1/202210/1/2022
D4D4Cancel10/1/20229/1/2022
E5E5Cancel9/1/20229/1/2022
E5E5Cancel8/15/20228/1/2022

 

Sales Offices A - C are the typical transaction examples with 1 sale and 1 cancel.

Sales Office D is an example of a lot cancelling and then reselling on the same day.

Sales Office E is an example where there were multiple cancellations. This is also an example where a lot resold and then cancelled on the same day.

 

Please let me know if this helps or if any additional detail would be helpful!

Hi @asimpson22 

 

You may create a new query with below code. 

let
    Source = Table.NestedJoin(CancelTable, {"SalesOffice", "Lot", "UniqueID"}, SalesTable, {"SalesOffice", "Lot", "UniqueID"}, "SalesTable", JoinKind.LeftOuter),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each let __CancelDate = [CancelDate] in Table.FirstN(Table.Sort(Table.SelectRows([SalesTable], each [Sale Date] <= __CancelDate), {{"Sale Date", Order.Descending}}),1)),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"SalesOffice", "Lot", "UniqueID", "Transaction Type", "CancelDate", "Custom"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"Sale Date"}, {"Orig. Sale Date
"})
in
    #"Expanded Custom"

Result:

vjingzhang_0-1672724883767.png

 

The problem is that from the current sample data, we cannot know which one is a record of a lot cancelling and then reselling on the same day, and which one is a record of a lot resold and then cancelled on the same day. There is no difference in the sample data. So you will find that for the example of Sales Office D, the output is not meeting your expected result. 

 

I have attached a sample file at bottom. 

 

Best Regards,
Community Support Team _ Jing
If this post helps, please Accept it as Solution to help other members find it. Highly appreciate your Kudos!

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

Top Solution Authors
Top Kudoed Authors