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
byr10112
Employee
Employee

Checking if value in table 1 exists in table 2, and if not inserting that value into table 2

I have two separate tables, where one table has sales targets of accounts and the other has the sales amounts. Some accounts may have sales targets but no sales, thus they do not appear in the realized sales table. Is there a way for me to check, via power query, whether an account with a target in table 1 exists in table 2? If it doesn't exist, I would like to manually insert this account into table 2 with sales amounts staying as blank. Below are examples of my two tables:

Table 1 (Target)

Client IDClient NameTarget
1001ABC Inc200000
1002BCG Inc150000
1003MNH Inc180000

Table 2 (Sales)

Client IDClient NameTargetSales
1001ABC Inc200000180000
1002BCG Inc150000175000
1004XYZ Inc160788200000
1005POT Inc1008256150344

 

As you can see, Client 1003 doesn't exist in Table 2. I would like the end table to look like this:

Client IDClient NameTargetSales
1001ABC Inc200000180000
1002BCG Inc150000175000
1003MNH Inc180000 
1004XYZ Inc160788200000
1005POT Inc1008256150344

 

Is this possible via power query?

1 ACCEPTED SOLUTION
artemus
Employee
Employee

This should be as simple as doing a merge with table1 and table 2 using a left join. After you do the join, you will want to only retain columns in table 2 that are not in table 1.

View solution in original post

3 REPLIES 3
tackytechtom
Super User
Super User

Hi @byr10112 ,

 

Here another wayu of doing it via a FULL OUTER JOIN:

tomfox_0-1651004986797.png

 

Here the code in Power Query M that you can paste into the advanced editor (if you do not know, how to exactly do this, please check out this quick walkthrough). Note: My table is called Table33a and Table33b in the example:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQwMFTSUXJ0clbwzEsGsowMQADIMLQAM2J1wIqMgCJOzu5QRYamMEXmpkiKTIAiEZFRMEVmBuYWFggjoYpMgSIB/iEwRQYGFkamZhAzjU1MlGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Client ID" = _t, #"Client Name" = _t, Target = _t, Sales = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Client ID", Int64.Type}, {"Client Name", type text}, {"Target", Int64.Type}, {"Sales", Int64.Type}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Client ID"}, Table33a, {"Client ID"}, "Table33a", JoinKind.FullOuter),
    #"Expanded Table33a" = Table.ExpandTableColumn(#"Merged Queries", "Table33a", {"Client ID", "Client Name", "Target"}, {"Table33a.Client ID", "Table33a.Client Name", "Table33a.Target"}),
    #"ISNULL on Client ID Col" = Table.ReplaceValue(#"Expanded Table33a",null, each _[Table33a.Client ID], Replacer.ReplaceValue,{"Client ID"}),
    #"ISNULL on Client Name Col" = Table.ReplaceValue(#"ISNULL on Client ID Col",null, each _[Table33a.Client Name], Replacer.ReplaceValue,{"Client Name"}),
    #"ISNULL on Target Col" = Table.ReplaceValue(#"ISNULL on Client Name Col",null, each _[Table33a.Target], Replacer.ReplaceValue,{"Target"}),
    #"Removed Columns" = Table.RemoveColumns(#"ISNULL on Target Col",{"Table33a.Client ID", "Table33a.Client Name", "Table33a.Target"})
in
    #"Removed Columns"

Let me know if this solves your issue 🙂

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

artemus
Employee
Employee

This should be as simple as doing a merge with table1 and table 2 using a left join. After you do the join, you will want to only retain columns in table 2 that are not in table 1.

tackytechtom
Super User
Super User

Hi @byr10112 ,

 

How about this:

tomfox_3-1650920768273.png

 

Take the Sales table and click on append:

tomfox_0-1650920624588.png

 

Append the target table:

tomfox_1-1650920661089.png

 

Remove duplicates:

tomfox_2-1650920750772.png

 

Does this help? 🙂

 

/Tom
https://www.tackytech.blog/
https://www.instagram.com/tackytechtom/



Did I answer your question➡️ Please, mark my post as a solution ✔️

Also happily accepting Kudos 🙂

Feel free to connect with me on LinkedIn! linkedIn

#proudtobeasuperuser 

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.

Top Solution Authors
Top Kudoed Authors