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
thmonte
Helper IV
Helper IV

Matching Values Between Two Tables and Displaying Them

I am trying to pull two tables together into some visuals and I am having a hard time.

 

Here is an example dataset:

 

Table 1   
DateIDType 
1/1/20180011065 
1/1/20180021065 
1/1/20180031065 
1/1/20180041065 
1/2/20180011065 
1/2/20180061065 
    
Table 2   
DateIDWorkWork_Attrib
1/1/2018001Work Type A 
1/1/2018001Work Type BOT
1/1/2018002Work Type B 
1/1/2018003Work Type B 
1/1/2018004Work Type C 
1/1/2018004Work Type A 
1/2/2018001Work Type C 
1/2/2018006Work Type B 

 

I'd like to get one regular table visual to display the following

 

Visual 1   
DateIDTypeWork Type
1/1/20180011065Work Type B
1/1/20180021065Work Type B
1/1/20180031065Work Type B
1/1/20180041065Work Type A
1/2/20180011065Work Type C
1/2/20180061065Work Type B

 

You'll notice each ID has multiple work types in Table 2 but I need a unique 1 for the visual so here are some rules around it

  • If there are multiple values in table two for an ID for a day AND there is one of them with OT as a attrib then use that one in the visual
  • If there are multiple values in table two for an ID for a day AND none of them have OT then take the latest one
  • If there are multiple values in table two for an ID for a day AND more than one has a attrib with OT then take the latest out of OT for the visual

The second type of a visual will be a sum of these work types that are being displayed in Visual 1

 

Visual 2   
DateWork Type AWork Type BWork Type C
1/1/2018130
1/2/2018011

 

Hope you all can help me!  Thanks

4 REPLIES 4
LivioLanzo
Solution Sage
Solution Sage

@thmonte

 

You can do this easily work Power Query. After having loaded Table1 and Table2 as two different queries, create this Query:

 

let
    Source = Table.NestedJoin( Table1, {"Date", "ID"}, Table2, {"Date", "ID"}, "JoinedTable", JoinKind.Inner ), 
    
    fnGetRow = ( tbl as table ) as table =>
       let
           FilterOT = Table.SelectRows( tbl, each [Work_Attrib] = "OT" ),
           SelectedTable  = if Table.RowCount( FilterOT ) = 0 then tbl else FilterOT,
           GetLastRow = Table.LastN( SelectedTable, 1 )
       in
           GetLastRow,
           
   TransformJoinTable = Table.TransformColumns( Source, {"JoinedTable", fnGetRow, type table}),
   
   ExpandTable = Table.ExpandTableColumn( TransformJoinTable, "JoinedTable", {"Work"} )
           
in
    ExpandTable

Capture.PNG 

 


 


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


Proud to be a Datanaut!  

THis is pretty good!  I do most of my work in DAX but seems like PowerQuery might be something I want to dive into as well.

 

I have a question tho.

 

It looks like if there is no match in the second database (no Work Type is found for that ID) that row records is not included in the power query.  Is it possible to tweak this?  And have all records from Table 1 show and if nothing is found in Table 2 to populate work column then make it a static value like "No Work Found"

@thmonte

 

Yes, you can do it like this:

 

let
    Source = Table.NestedJoin( Table1, {"Date", "ID"}, Table2, {"Date", "ID"}, "JoinedTable", JoinKind.LeftOuter ), 
    
    fnGetRow = ( tbl as table ) as table =>
       let
           FilterOT = Table.SelectRows( tbl, each [Work_Attrib] = "OT" ),
           SelectedTable  = if Table.RowCount( FilterOT ) = 0 then tbl else FilterOT,
           GetLastRow = if
                           Table.IsEmpty( SelectedTable )
                        then
                           Table.FromRecords( { [Work = "No Work Found"] } )
                        else
                            Table.LastN( SelectedTable, 1 )
       in
           GetLastRow,
           
   TransformJoinTable = Table.TransformColumns( Source, {"JoinedTable", fnGetRow, type table}),
   
   ExpandTable = Table.ExpandTableColumn( TransformJoinTable, "JoinedTable", {"Work"} )
           
in
    ExpandTable

 


 


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


Proud to be a Datanaut!  

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.