+ Reply to Thread
Results 1 to 12 of 12

Power Query Wrong Merging (fuzzymerge with 1 result)

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-30-2010
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    215

    Power Query Wrong Merging (fuzzymerge with 1 result)

    Hi all

    I have a lot of tables that I merge back and forth in Power Query and in general, I need to merge LeftOuter, but as the table I merge with often has duplicates, I use fuzzy matching, with only 1 result (as I always only need the first occurrence).

    But I have just noticed a quite troubling issue with Power Query merging, when I do as above, as it seems to merge completely incorrect sometimes.

    I have set up a small test sheet, with 2 tables with numbers (but treated as text) - table2 has a single duplicate (the last 2 records). As seen below, when I merge table2 into table 1, and then check if the merged result corresponds with the source result, a bunch of rows are incorrectly merged:
    WrongMerging.png

    This is my merging code:
    let
        Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Numbers1", type text}}),
        #"Merged Queries" = Table.FuzzyNestedJoin(#"Changed Type", {"Numbers1"}, Numbers2Table, {"Numbers2"}, "Numbers2Merging", JoinKind.LeftOuter, [IgnoreCase=false, IgnoreSpace=false, NumberOfMatches=1]),
        #"Expanded Numbers2" = Table.ExpandTableColumn(#"Merged Queries", "Numbers2Merging", {"Numbers2", "Occurrence"}, {"Numbers2Merged", "Occurrence"}),
        #"Added Conditional Column" = Table.AddColumn(#"Expanded Numbers2", "WrongMerge", each if [Numbers1] <> [Numbers2Merged] and [Numbers2Merged] <> null then "WrongMerge" else null)
    in
        #"Added Conditional Column"
    Is this a bug or am I doing something wrong - and what is the correct way of doing this kind of merge in Power Query?

    Best regards
    Imbizile
    Attached Files Attached Files

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2509 (Windows 11 Home 24H2 64-bit)
    Posts
    92,434

    Re: Power Query Wrong Merging (fuzzymerge with 1 result)

    Why don't you simply merge and then remove duplicates? Fuzzy merging is not the right approach as far as I can see, based on your description.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  3. #3
    Valued Forum Contributor
    Join Date
    04-27-2015
    Location
    Abu Dhabi, U.A.E
    MS-Off Ver
    Office 365 | 2016
    Posts
    697

    Re: Power Query Wrong Merging (fuzzymerge with 1 result)

    Have you tried 'Full outer -All Rows from both' option in Merge > Join Kind
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    04-30-2010
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    215

    Re: Power Query Wrong Merging (fuzzymerge with 1 result)

    I need the table I am merging into to stay intact, meaning keeping the same number of rows and not altering any of the existing data in that table. And that table might contain duplicates as well (I did not address that in the first post, but that is often the case). So I only need to add extra columns to that first table with additional info from the second table, without otherwise changing the first table.

    As I see it, both suggestions will change data in the first table.

  5. #5
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2509 (Windows 11 Home 24H2 64-bit)
    Posts
    92,434

    Re: Power Query Wrong Merging (fuzzymerge with 1 result)

    OK - please mock up manually the results you want to see in the sample workbook. At the moment, we are stabbing in the dark.

    NOTHING is ever changed in an underlying source table.

  6. #6
    Forum Contributor
    Join Date
    04-30-2010
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    215

    Re: Power Query Wrong Merging (fuzzymerge with 1 result)

    Ok, I now added a duplicate row in numbers1 table to illustrate that the scenario is 2 tables both with duplicates, and what I need is to get additional info (columns) from second table into first table , without otherwise changing data in the first table.

    So now the 2 tables look like this:
    WrongMerging1.png

    I then made a new "Expected result" table, like this:
    WrongMerging2.png

    Basically, I just made them from these formulas and dragged to bottom:
    Numbers2Merged
    =IFERROR(VLOOKUP(K3;Table3[#All];1;FALSE);"")
    
    Occurrence
    =IFERROR(VLOOKUP(K3;Table3[#All];2;FALSE);"")
    
    WrongMerge
    =IF(OR(L3="";K3=L3);"";"WrongMerge")
    And that is what I would expect the Power Query to return as well, but it returns what is listed under the "result" table.
    Attached Files Attached Files

  7. #7
    Forum Guru Bo_Ry's Avatar
    Join Date
    09-10-2018
    Location
    Thailand
    MS-Off Ver
    MS 365
    Posts
    7,223

    Re: Power Query Wrong Merging (fuzzymerge with 1 result)

    Please try

    let
        Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
        Merged = Table.NestedJoin(Source, {"Numbers1"}, Numbers2Table, {"Numbers2"}, "Numbers2Table", JoinKind.LeftOuter),
        Expanded = Table.ExpandTableColumn(Merged, "Numbers2Table", {"Numbers2", "Occurrence"}, {"Numbers2", "Occurrence"}),
        RemovedDuplicates = Table.Distinct(Expanded, {"Numbers1"})
    in
        RemovedDuplicates
    or

    let
        Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
        Merged = Table.NestedJoin(Source, {"Numbers1"}, Table.Distinct(Numbers2Table, {"Numbers2"}), {"Numbers2"}, "Numbers2Table", JoinKind.LeftOuter),
        Expanded = Table.ExpandTableColumn(Merged, "Numbers2Table", {"Numbers2", "Occurrence"}, {"Numbers2", "Occurrence"})
    in
        Expanded
    Attached Files Attached Files
    Last edited by Bo_Ry; 12-15-2020 at 10:22 AM.

  8. #8
    Forum Contributor
    Join Date
    04-30-2010
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    215

    Re: Power Query Wrong Merging (fuzzymerge with 1 result)

    Thanks so much Bo_Ry - your first suggestion deletes the last duplicate row in the first table, so that is a no go, but your second suggestion gives the expected result. So I see the trick was adding the Table.Distinct() to the second table when merging.

    I will now change all my fuzzymerges to the Table.Distinct() approach instead.

    Thanks again and also thanks to Shareez Saleem and AliGW for their contributions

  9. #9
    Forum Contributor
    Join Date
    04-30-2010
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    215

    Re: Power Query Wrong Merging (fuzzymerge with 1 result)

    A bonus question: in this demo sheet, both tables were sorted by the numbers column, but if the tables have different sorting, it will change the sorting order whenever I merge a table into another table. Is there a way to keep the same sorting order in the table I am merging into?

  10. #10
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2509 (Windows 11 Home 24H2 64-bit)
    Posts
    92,434

    Re: Power Query Wrong Merging (fuzzymerge with 1 result)

    Add an index column before merging, and sort on it after merging, then remove it.

  11. #11
    Forum Contributor
    Join Date
    04-30-2010
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    215

    Re: Power Query Wrong Merging (fuzzymerge with 1 result)

    Ahh right, I will do that!

    Thanks

  12. #12
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2509 (Windows 11 Home 24H2 64-bit)
    Posts
    92,434

    Re: Power Query Wrong Merging (fuzzymerge with 1 result)

    You're welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Power Query merging
    By Imbizile in forum Excel General
    Replies: 7
    Last Post: 05-09-2020, 08:59 AM
  2. [SOLVED] Power Query - excel formula translation into Power Query
    By afgi in forum Excel Charting & Pivots
    Replies: 7
    Last Post: 02-19-2020, 03:38 AM
  3. Replies: 4
    Last Post: 02-17-2020, 06:03 AM
  4. Power Query wrong data loading
    By muad in forum Excel General
    Replies: 3
    Last Post: 02-13-2019, 03:54 AM
  5. Sub-Forum for Excel Power Tools (Power Query, Power Pivot & Power BI)
    By chullan88 in forum Suggestions for Improvement
    Replies: 10
    Last Post: 06-28-2018, 02:25 PM
  6. Power Query - calculated true or false show result of table????
    By janljan in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-02-2018, 11:23 PM
  7. Power Query Solution for merging data
    By kersplash in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-23-2018, 12:40 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1