+ Reply to Thread
Results 1 to 10 of 10

Combine tables in Power Query

Hybrid View

Ebl Combine tables in Power Query 07-26-2021, 10:41 AM
XLent Re: Combine tables in Power... 07-26-2021, 12:16 PM
Ebl Re: Combine tables in Power... 07-30-2021, 08:30 AM
Ebl Re: Combine tables in Power... 07-30-2021, 06:45 AM
XLent Re: Combine tables in Power... 08-02-2021, 04:06 AM
Ebl Re: Combine tables in Power... 08-02-2021, 04:48 AM
XLent Re: Combine tables in Power... 08-02-2021, 06:39 AM
Ebl Re: Combine tables in Power... 08-02-2021, 07:14 AM
Bo_Ry Re: Combine tables in Power... 08-02-2021, 07:59 AM
Ebl Re: Combine tables in Power... 08-02-2021, 08:11 AM
  1. #1
    Registered User
    Join Date
    06-08-2018
    Location
    Dk
    MS-Off Ver
    365
    Posts
    6

    Combine tables in Power Query

    The title is not very accurate, but I'm looking for help to merge (or append) two (or more) tables from Power Query.
    In the attached example, I have a range of from and to inputs of various information (in this case, for a railway example project rail- and superstructure types).
    I have imported both tables into Power Query, and tried to both combine and append them, but the results are not exactly what I'm looking form

    I've added a tab called "Goal" to show what I'm trying to accomplish.
    I will need to add additional information in the same type of formatting at a later stage, so anything that automates this process is greatly appreciated!

    Hopefully someone will be able to help.


    Best Regards,
    Attached Files Attached Files
    Last edited by Ebl; 08-02-2021 at 08:12 AM. Reason: Solved

  2. #2
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,706

    Re: Combine tables in Power Query

    Hi, I think the below would do what you want, i.e. mimics your "Goal" but, I've no doubt the more savvy PQ folk could do this much more efficiently ...

    let
        Source = Table.Combine({Types, Rails}),
        #"Sorted Rows" = Table.Sort(Source,{{"To [St.]", Order.Ascending}}),
        #"Filled Up0" = Table.FillUp(#"Sorted Rows",{"Rail type"}),
        #"Filled Up1" = Table.FillUp(#"Filled Up0",{"Track type"}),
        #"Added Index" = Table.AddIndexColumn(#"Filled Up1", "Index", 0, 1),
        #"Prior" = Table.AddColumn(#"Added Index","Prior", each if [Index] = 0 then 0 else #"Added Index"[#"To [St.]"]{[Index] - 1}),
        #"Update_Fm" = Table.ReplaceValue(#"Prior", each [#"From [St.]"], each if [#"From [St.]"] = 0 then 0 else if [Prior] > [#"From [St.]"] then [Prior] else [#"From [St.]"], Replacer.ReplaceValue, {"From [St.]"}),
        #"Update_Ln" = Table.ReplaceValue(#"Update_Fm",each [#"Length [m]"], each [#"To [St.]"] - [#"From [St.]"], Replacer.ReplaceValue, {"Length [m]"}),
        #"Filled Dn0" = Table.FillDown(#"Update_Ln",{"Rail type","Track type"}),    
        #"Removed Columns" = Table.RemoveColumns(#"Filled Dn0",{"Index","Prior"})
    in
        #"Removed Columns"

  3. #3
    Registered User
    Join Date
    06-08-2018
    Location
    Dk
    MS-Off Ver
    365
    Posts
    6

    Re: Combine tables in Power Query

    I see that I made a mistake in the "Goal" tab, by having cell A8 = 0, and not a reference back.
    How do I get rid of this in the script?
    Last edited by AliGW; 08-02-2021 at 04:09 AM. Reason: PLEASE don't quote unnecessarily!

  4. #4
    Registered User
    Join Date
    06-08-2018
    Location
    Dk
    MS-Off Ver
    365
    Posts
    6

    Re: Combine tables in Power Query

    Yes, this works - Thank you!
    What if I wanted to add another or more data with the same formatting?

  5. #5
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,706

    Re: Combine tables in Power Query

    change

    #"Update_Fm" = Table.ReplaceValue(#"Prior", each [#"From [St.]"], each if [#"From [St.]"] = 0 then 0 else if [Prior] > [#"From [St.]"] then [Prior] else [#"From [St.]"], Replacer.ReplaceValue, {"From [St.]"}),
    to

    #"Update_Fm" = Table.ReplaceValue(#"Prior", each [#"From [St.]"], each if [Prior] > [#"From [St.]"] then [Prior] else [#"From [St.]"], Replacer.ReplaceValue, {"From [St.]"}),
    Last edited by AliGW; 08-02-2021 at 04:09 AM. Reason: Code tags added.

  6. #6
    Registered User
    Join Date
    06-08-2018
    Location
    Dk
    MS-Off Ver
    365
    Posts
    6

    Re: Combine tables in Power Query

    Thanks a lot. This was extremely useful!
    Is there an "easy" way to add additional information, like another table with the same formatting?

  7. #7
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,706

    Re: Combine tables in Power Query

    Power Query is capable of lots of things however, to advise further you would need to provide a further sample (including both source, and desired output - as before).

  8. #8
    Registered User
    Join Date
    06-08-2018
    Location
    Dk
    MS-Off Ver
    365
    Posts
    6

    Re: Combine tables in Power Query

    Yes of course.
    Please find attached an updated spreadsheet with two additional sheets with their own respective tables.
    The "Goals" tab was updated as well, with these two new tables.
    Attached Files Attached Files

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

    Re: Combine tables in Power Query

    Rename Alignment

    let
        Source = Excel.CurrentWorkbook(){[Name="Alignment"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"From [St.]", type number}, {"To [St.]", type number}, {"Length [m]", type number}, {"Track type", type text}}),
        #"Renamed Columns" = Table.RenameColumns(#"Changed Type",{{"Track type", "Alignment"}})
    in
        #"Renamed Columns"
    and Result

    let
        Source = Table.Combine({ Types, Rails, Alignment, Sections}),
        SortedRows = Table.Sort(Source,{{"To [St.]", Order.Ascending}, {"Section", Order.Ascending}}),
        RemovedLength = Table.RemoveColumns(SortedRows,{"Length [m]"}),
        ToColumn = Table.ToColumns(RemovedLength),
        NewFrom = Table.FromColumns({List.Range({0}&ToColumn{1},0,Table.RowCount(RemovedLength))}& List.Skip(ToColumn,1),Table.ColumnNames(RemovedLength)),
        AddedLength = Table.AddColumn(NewFrom, "Length [m]", each [#"To [St.]"]-[#"From [St.]"]),
        FilledUp = Table.FillUp(AddedLength,{"Track type", "Rail type", "Alignment", "Section"}),
        ReorderedColumns = Table.ReorderColumns(FilledUp,{"Section", "From [St.]", "To [St.]", "Length [m]", "Track type", "Rail type", "Alignment"})
    in
        ReorderedColumns
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    06-08-2018
    Location
    Dk
    MS-Off Ver
    365
    Posts
    6

    Re: Combine tables in Power Query

    This is incredible!
    Thank you all very much for your help.
    Power Queries are such a powerful tool if mastered correctly.

+ 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. Power Query - Combine Merges
    By ptmuldoon in forum Excel General
    Replies: 1
    Last Post: 12-19-2020, 06:45 PM
  2. [SOLVED] Power Query Combine
    By kabseeker in forum Excel General
    Replies: 2
    Last Post: 05-05-2020, 10:12 AM
  3. Power Query: Updating a query based on changing tables
    By cheesehead101 in forum Excel General
    Replies: 1
    Last Post: 12-16-2019, 06:54 PM
  4. [SOLVED] Power Query: Combine records with deliminter
    By misty15 in forum Office 365
    Replies: 4
    Last Post: 09-17-2019, 04:34 PM
  5. Power Query: Combine and Transform from folder
    By LUKE92 in forum Excel General
    Replies: 13
    Last Post: 06-26-2019, 12:34 AM
  6. [SOLVED] I do not have a combine option - Power Query
    By callumlmcgrath in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 05-08-2019, 06:24 AM
  7. Power Query: combine sheets from different workbook
    By erica.ho56458 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-09-2018, 02:28 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