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
BrianDWalker
Regular Visitor

Power BI: Power Query reference another table (dates/ID)

Hi,

 

I'm experiencing a problem while trying to reference another table. Here is the problem I'm running into:

I have a table of sales data where it has various columns, but the ones that I'm focusing on is the Employee UID and the transaction date. I have another table that shows employee info: Name, email, UID, Position Start Date, & Position End Date. The problem I'm running into is that I don't want to query sales data from when they were in their previous role or next role. Only while they were active in this organization. I'm not exactly sure how to create the formula in Power BI: Power Query. I tried the following, but doesn't work:

 

= Table.SelectRows(#"Filtered Rows1", each [transaction_date] >= #date(Date.Year(EmployeeHistory[position_start_date]),Date.Month(EmployeeHistory[position_start_date]),Date.Day(EmployeeHistory[position_start_date])) and [transaction_date] <= #date(Date.Year(EmployeeHistory[position_end_date]),Date.Month(EmployeeHistory[position_end_date]),Date.Day(EmployeeHistory[position_end_date])) and List.Contains(EmployeeHistory[uid], [uid] ))
 
I'm going to put below an example of the data. In the show column, I indicate whether or not I want it to show or not.
 
EmployeeHistory Table
UIDPosition State DatePosition End Date
AB123C10/15/202012/31/9999
EF456G2/1/20195/15/2021
KL789M3/15/202112/31/9999
 
Sales Table
Transaction DateUIDQuantityAmountSHOW?
1/1/2021EF456G5$100.00YES
2/10/2021AB123C7$250.00YES
3/12/2021KL789M6$300.00NO
4/21/2021AB123C2$400.00YES
5/30/2021EF456G3$95.00NO
6/5/2021KL789M4$75.00YES
 
If someone could assist me with how I can accomplish this in Power BI Power Query, that would be extremely helpful!
1 ACCEPTED SOLUTION
Vera_33
Resident Rockstar
Resident Rockstar

Hi @BrianDWalker 

 

You are identifying whether the [Transaction Date] is between [Position State Date] and [Position End Date] for each UID.

 

Your way is to select the rows then do List.Contains

Table.AddColumn(yourPreviousStep, "Show",(x)=>List.Contains(
    Table.SelectRows(EmployeeHistory, each [Position State Date]<=x[Transaction Date] and [Position End Date]>=x[Transaction Date])[UID],x[UID]))

 

If you have many rows, it can be slow. So merge the table first

    #"Merged Queries" = Table.NestedJoin(yourPreviousStep, {"UID"}, EmployeeHistory, {"UID"}, "EmployeeHistory", JoinKind.LeftOuter),
    #"Added Custom1" = Table.AddColumn(#"Merged Queries", "Custom", each [Transaction Date]>=[EmployeeHistory][Position State Date]{0} and [Transaction Date]<=[EmployeeHistory][Position End Date]{0})

View solution in original post

1 REPLY 1
Vera_33
Resident Rockstar
Resident Rockstar

Hi @BrianDWalker 

 

You are identifying whether the [Transaction Date] is between [Position State Date] and [Position End Date] for each UID.

 

Your way is to select the rows then do List.Contains

Table.AddColumn(yourPreviousStep, "Show",(x)=>List.Contains(
    Table.SelectRows(EmployeeHistory, each [Position State Date]<=x[Transaction Date] and [Position End Date]>=x[Transaction Date])[UID],x[UID]))

 

If you have many rows, it can be slow. So merge the table first

    #"Merged Queries" = Table.NestedJoin(yourPreviousStep, {"UID"}, EmployeeHistory, {"UID"}, "EmployeeHistory", JoinKind.LeftOuter),
    #"Added Custom1" = Table.AddColumn(#"Merged Queries", "Custom", each [Transaction Date]>=[EmployeeHistory][Position State Date]{0} and [Transaction Date]<=[EmployeeHistory][Position End Date]{0})

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.