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
Anonymous
Not applicable

Merge multiple queries with same header without creating duplicate columns

I am having trouble merging queries that have partial data. I am trying to merge Queries B and C with Query A. The end result will create 2 columns for "Sales". I would like "Sales" to be in the same column. Also, I know appending B and C would solve for the sample data, but this will not work for my actual data so let's assume appending is not an option. Here are my sample Queries:

 

Query A

NameWeek Number
Jon1
Jack1
Hank1
Jill1
Jon2
Jack2
Hank2
Jill2

 

Query B

NameWeek NumberSales
Jon150
Jack130
Hank160
Jill1

70

 

Query C

NameWeek NumberSales
Jon275
Jack235
Hank265
Jill245

 

Actual Result

NameWeek NumberSalesSales 1
Jon150 
Jack130 
Hank160 
Jill170 
Jon2 75
Jack2 35
Hank2 65
Jill2 45

 

Desired Result

NameWeek NumberSales
Jon150
Jack130
Hank160
Jill170
Jon275
Jack235
Hank265
Jill245

 

Thank you in advance for your help!

1 ACCEPTED SOLUTION

Hi @Anonymous 

 

I'm sure you could write M function to do it, but the easiest solution would be merging all your tables, expanding and later merging the columns together as below.
Why append is not an option?

let
    Source = Table.NestedJoin(#"Query A", {"Name", "Week Number"}, #"Query B", {"Name", "Week Number"}, "Query B", JoinKind.LeftOuter),
    #"Expanded Query B" = Table.ExpandTableColumn(Source, "Query B", {"Sales"}, {"Query B.Sales"}),
    #"Merged Queries" = Table.NestedJoin(#"Expanded Query B", {"Name", "Week Number"}, #"Query C", {"Name", "Week Number"}, "Query C", JoinKind.LeftOuter),
    #"Expanded Query C" = Table.ExpandTableColumn(#"Merged Queries", "Query C", {"Sales"}, {"Query C.Sales"}),
    #"Merged Queries1" = Table.NestedJoin(#"Expanded Query C", {"Name", "Week Number"}, #"Query D", {"Name", "Week Number"}, "Query D", JoinKind.LeftOuter),
    #"Expanded Query D" = Table.ExpandTableColumn(#"Merged Queries1", "Query D", {"Sales"}, {"Query D.Sales"}),
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Expanded Query D", {{"Query B.Sales", type text}, {"Query C.Sales", type text}, {"Query D.Sales", type text}}, "en-GB"),{"Query B.Sales", "Query C.Sales", "Query D.Sales"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged")
in
    #"Merged Columns"

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
Mariusz Repczynski



 

 

View solution in original post

3 REPLIES 3
Mariusz
Community Champion
Community Champion

Hi @Anonymous 

 

You can append Query B and C and later Merge appended result into Qury A.

 

image.png

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
Mariusz Repczynski

 

Anonymous
Not applicable

Thanks for the response. Appending isn't an option for my actual dataset. Essentially I am asking if there is a way to merge queries so the values in the expanded columns can be populated in an existing column rather than creating a new column.

Hi @Anonymous 

 

I'm sure you could write M function to do it, but the easiest solution would be merging all your tables, expanding and later merging the columns together as below.
Why append is not an option?

let
    Source = Table.NestedJoin(#"Query A", {"Name", "Week Number"}, #"Query B", {"Name", "Week Number"}, "Query B", JoinKind.LeftOuter),
    #"Expanded Query B" = Table.ExpandTableColumn(Source, "Query B", {"Sales"}, {"Query B.Sales"}),
    #"Merged Queries" = Table.NestedJoin(#"Expanded Query B", {"Name", "Week Number"}, #"Query C", {"Name", "Week Number"}, "Query C", JoinKind.LeftOuter),
    #"Expanded Query C" = Table.ExpandTableColumn(#"Merged Queries", "Query C", {"Sales"}, {"Query C.Sales"}),
    #"Merged Queries1" = Table.NestedJoin(#"Expanded Query C", {"Name", "Week Number"}, #"Query D", {"Name", "Week Number"}, "Query D", JoinKind.LeftOuter),
    #"Expanded Query D" = Table.ExpandTableColumn(#"Merged Queries1", "Query D", {"Sales"}, {"Query D.Sales"}),
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Expanded Query D", {{"Query B.Sales", type text}, {"Query C.Sales", type text}, {"Query D.Sales", type text}}, "en-GB"),{"Query B.Sales", "Query C.Sales", "Query D.Sales"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged")
in
    #"Merged Columns"

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
Mariusz Repczynski



 

 

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.