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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
marksman1941
New Member

Merging multiple tables in power query

Hi all. Brand new user here, but I've been lurking for a few weeks. I'm new to Power BI, and while I think I'm getting close to a solution I still don't have things quite right regarding my use of Power Query. 

 

I will be getting monthly spreadsheets with fairly messy data on them that will need cleaned up on a regular basis. The spreadsheet I am working with currently has 5 separate tables with several common column titles, and several unique to each spreadsheet. The data contained is personnel information, and some names are duplicated across multiple tables. My goal is to have a single iteration of each name whether it appears 1 or 5 times, and all information from each table for that person drawn into a single table. The data set is much larger, but the table below is representative of the raw input I have. 

 

Record ID Last NameFirst NameGender   
111SmithJamesM  
222JohnsonJackM  
333RandomSomeF  
      
Record ID Last NameFirst NameGender Status? 
111SmithJamesMActive 
222JohnsonJackMInactive 
333RandomSomeF  
      
Record ID Last NameFirst NameGender CodeLocation
111SmithJamesM6Downtown
222JohnsonJackM5Home
333RandomSomeF6Local
444SubjectTestF2 
      
      
Record ID Last NameFirst NameGender CodeLocation
111SmithJamesM6Downtown
222JohnsonJackM5Home
333RandomSomeF6Local
444SubjectTestF2 

 

My goal for the data outcome is to look like this

Record ID Last NameFirst NameGender Status?CodeLocation
111SmithJamesMActive6Downtown
222JohnsonJackMInactive5Home
333RandomSomeF 6Local
444SubjectTestFInactive2 

 

My process so far has been -

* Ensure all headers are identical

* Get Data from Table and save as a connection, for all 5 tables. 

* Merge Queries, using the Record ID, Last Name, First Name, and Gender as join kind, left outer. 

* Expand Table 2, uncheck Record ID, Last Name, First Name, and Gender, click OK

 

When I do this method I get all of my information in one spreadsheet and things seem functional. I wreck on merge2. 

When I import in a table that has new names or report ID's, they aren't added to the spreadsheet at all. I'm limited to the initial group of people who were in the initial Table 1.

 

To rectify this, I copied every single person into a single list with Report ID, Last Name, First Name, and used that as the start of my table that I merged everything with. Merge1 looks fine and works great, using the above 3 headers as join kind. When I try to bring in a third table however, no combination (that I have found) of join kind columns or checked/unchecked columns in the power query editor will allow all of the personnel data from table 3 to move over. 

 

I am very clearly using this improperly, but I feel like I'm just missing a step or two somewhere. Is anyone able to help identify where I'm going wrong? Thank you in advance!

4 REPLIES 4
AlienSx
Super User
Super User

Hi, @marksman1941 combine your tables with Table.Combine, then group by ID and apply some custom function that fills up and down all the columns (I don't know what columns except Record ID always has data so it might be an overkill). Please note that in order Table.FillUp/Down to work you must have nulls in empty "cells" of your tables. 

    a = Table.Combine({t1, t2, t3, t4, t5}),
    // this little custom function replaces blanks with nulls,
    // fills up/down all columns of the table and returns single record
    f = (t as  table) as record => 
        let 
            cols = List.Buffer(Table.ColumnNames(t)),
            replace_blanks = Table.ReplaceValue(t, "", null, Replacer.ReplaceValue, cols),
            up = Table.FillUp(replace_blanks, cols),
            down = Table.First(Table.FillDown(up, cols))
        in down,
    // group by Record ID, apply function 
    groups = Table.Group(a, "Record ID", {{"all", each f(_)}}),
    out = Table.FromRecords(groups[all])

 

As silly as this may sound, it seems like the Append function did exactly what I was looking for. Are there any unintended side effects with using Append?

Duplicated Record IDs as opposed to what you show as you "goal for the data outcome". BTW there is no "Append function" in M language. It's a UI's name which effectively resembles to Table.Combine function behind the scene that I use in the very first row of code. 

I appreciate the help! I'm still using fairly surface level operations here, as I haven't used power.bi much before now. I'm glad that the built in append function works similar to what you were referring to. 

 

When I ran the append function, it did give me about 20 duplicates of the Record ID lines. I ran a remove duplicates tool which pulled those and left me with what looked to be a clean data set, although i haven't verified that all 600 lines transferred properly.

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

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

Top Solution Authors
Top Kudoed Authors