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 two tables, where duplication of rows are needed

Hi,

I have two tables, one is the vacancy rates of a portfolio (Table 1) and a second one with the benchmark data (Table 2).

 

Table 1: Portfolio

yearstreetmunicipalityunitvacancy
2018Marktgasse 2KlotenKloten (Pfo)2.50%
2019Marktgasse 2KlotenKloten (Pfo)2.25%
2018Flurstr. 7KlotenKloten (Pfo)3.00%
2019Flurstr. 7KlotenKloten (Pfo)2.70%
2018Freulerstr. 5AllschwilAllschwil (Pfo)4.50%
2019Freulerstr. 5AllschwilAllschwil (Pfo)3.90%

 

Table 2: Benchmark Data 

yearmunicipalityunitvacancy
2017AllschwilUnteres Baselbiet (MS)0.67%
2018AllschwilUnteres Baselbiet (MS)0.86%
2019AllschwilUnteres Baselbiet (MS)1.08%
2017AllschwilAllschwil (Mun)0.89%
2018AllschwilAllschwil (Mun)1.08%
2019AllschwilAllschwil (Mun)1.10%
2017KlotenGlattal-Furttal (MS)0.85%
2018KlotenGlattal-Furttal (MS)1.05%
2019KlotenGlattal-Furttal (MS)0.77%
2017KlotenKloten (Mun)0.57%
2018KlotenKloten (Mun)0.87%
2019KlotenKloten (Mun)0.88%

 

Now, I want to merge the tables to get the follwing result:

yearstreetmunicipalityunitvacancy
2017Marktgasse 2KlotenGlattal-Furttal (MS)0.85%
2017Marktgasse 2KlotenKloten (Mun)0.57%
2018Marktgasse 2KlotenKloten (Pfo)2.50%
2018Marktgasse 2KlotenGlattal-Furttal (MS)1.05%
2018Marktgasse 2KlotenKloten (Mun)0.87%
2019Marktgasse 2KlotenKloten (Pfo)2.25%
2019Marktgasse 2KlotenGlattal-Furttal (MS)0.77%
2019Marktgasse 2KlotenKloten (Mun)0.88%
2017Flurstr. 7KlotenGlattal-Furttal (MS)0.85%
2017Flurstr. 7KlotenKloten (Mun)0.57%
2018Flurstr. 7KlotenKloten (Pfo)3.00%
2018Flurstr. 7KlotenGlattal-Furttal (MS)1.05%
2018Flurstr. 7KlotenKloten (Mun)0.87%
2019Flurstr. 7KlotenKloten (Pfo)2.70%
2019Flurstr. 7KlotenGlattal-Furttal (MS)0.77%
2019Flurstr. 7KlotenKloten (Mun)0.88%

 

I didn't show the case for Allschwil, but I think you get what I mean. This structure is needed, so I can show a Line Chart with the vacancy rate. When I select Kloten, Marktgasse 2, then the vacancy rates of the portfolio and the corresponding Benchmarks should be displayed. I tried it different ways but so far with no success. Many thanks for your help!

1 ACCEPTED SOLUTION

Hi

oh yes, I see that now (proper sample data would have helped, tbh..)

More operations have to be applied on the aggregated level:

 

let
    Start = Portfolio,
    #"Grouped Rows" = Table.Group(Start, {"street", "municipality", "unit"}, {{"Object", each _, type table [year=number, street=text, municipality=text, unit=text, vacancy=number]}}),
    SelectBenchmark = Table.AddColumn(#"Grouped Rows", "BenchmarkData", each Table.SelectRows(#"Benchmark Data", (x) => x[municipality] = [municipality])),
    MergeOnYear = Table.AddColumn(SelectBenchmark, "MergedBenchmark", each Table.NestedJoin([Object], {"year"}, [BenchmarkData] , {"year"}, "Benchmark Data", JoinKind.FullOuter)),
    Combine = Table.AddColumn(MergeOnYear, "Custom.1", each Table.AddColumn([MergedBenchmark], "Custom", (x) => Table.FromRecords( { x } ) & x[Benchmark Data])),
    ExpandCustom = Table.AddColumn(Combine, "Custom.2", each Table.Combine( [Custom.1]  [Custom] )),
    FillDown = Table.AddColumn(ExpandCustom, "Custom.3", each Table.FillDown([Custom.2],{"street"})),
    Custom2 = Table.Combine ( FillDown[Custom.3] ),
    #"Filtered Rows" = Table.SelectRows(Custom2, each ([unit] <> null)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Benchmark Data"})
in
    #"Removed Columns"

 

Please check attached file.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

11 REPLIES 11
ImkeF
Super User
Super User

Hi @Anonymous 

 

this would be pretty easy if both tables would cover the same years.

 

let
    Source = Table.NestedJoin(Portfolio, {"year", "municipality"}, #"Benchmark Data" , {"year", "municipality"}, "Benchmark Data", JoinKind.LeftOuter),
    #"Added Custom" = Table.AddColumn(Source, "Custom", each Table.FromRecords( { _ } ) & [Benchmark Data]),
    Custom1 = Table.Combine( #"Added Custom"[Custom] ),
    #"Filled Down" = Table.FillDown(Custom1,{"street"}),
    #"Removed Columns" = Table.RemoveColumns(#"Filled Down",{"Benchmark Data"})
in
    #"Removed Columns"

 

Please come back if 2017 shall be included as well (although there is no portfolio-data for it? )

 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Anonymous
Not applicable

Hi @ImkeF 

 

many thanks for your reply. As you anticipated correctly, we need 2017 as well and there is no portfolio data for 2017. We would like to show how the market evolved and actually have data back to 2012, but for simplicity I only added 2017. I already tried to do a merge and fill up the missing address but this doesn't work for properties wich are in the same municipality. Already tried to create a key with years and municipality but that didn't work neither.

 

many thanks for your help!

Bea

Hi @Anonymous 

how about this then?: 

let
    Start = Portfolio,
    #"Grouped Rows" = Table.Group(Start, {"street", "municipality", "unit"}, {{"Object", each _, type table [year=number, street=text, municipality=text, unit=text, vacancy=number]}}),
    #"Added Custom1" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.NestedJoin([Object], {"year","municipality"}, #"Benchmark Data" , {"year","municipality"}, "Benchmark Data", JoinKind.FullOuter)),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Custom.1", each Table.AddColumn([Custom], "Custom", (x) => Table.FromRecords( { x } ) & x[Benchmark Data])),
    Custom1 = Table.Combine( Table.Combine( #"Added Custom2"[Custom.1] ) [Custom] ),
    #"Filtered Rows" = Table.SelectRows(Custom1, each ([unit] <> null)),
    #"Filled Down" = Table.FillDown(#"Filtered Rows",{"street"}),
    #"Removed Columns" = Table.RemoveColumns(#"Filled Down",{"Benchmark Data"})
in
    #"Removed Columns"

 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Anonymous
Not applicable

Thank you very much @ImkeF 

 

Unfortunately, it didn't work properly. I think the problem is that the fill down is only working properly, when the first row with the "street" data is in the right place.  With your code I got the following result:

 

yearstreetmunicipalityunitvacancy
2017nullKlotenGlattal-Furttal (MS)0.85%
2017nullAllschwilAllschwil (Mun)0.89%
2018nullAllschwilUnteres Baselbiet (MS)0.86%
2018nullAllschwilAllschwil (Mun)1.08%
2017nullAllschwilUnteres Baselbiet (MS)0.67%
2017nullKlotenKloten (Mun)0.57%
2019nullAllschwilAllschwil (Mun)1.10%
2019nullAllschwilUnteres Baselbiet (MS)1.08%
2018Flurstrasse 7KlotenKloten (Pfo)3.00%

 

That was exactly the problem I had when I tried to do it with the fill down. Maybe you have an idea how to fix this?

Hi @Anonymous  

this is hard to understand, as I'm starting with the table incl. streets (on the left side) and merge tables (to the right side).

So when expanding, the fill down should work properly. Any chance you might have a applied my code in a different way?

Attaching the file to follow along.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Anonymous
Not applicable

Hi @ImkeF 

 

thank you for sharing your PBI. Now I see, why it worked for you. In the expected result I plotted, I omitted Allschwil for simplicity reasons, but I would expect that the code works for all Portfolio entries and not only for Kloten. 

The point of this excercise is to automate the process, for e.g. when we have a portfolio of another client with different locations it should automatically merge it to the Benchmarks.

Hi @Anonymous 

if you check query "Portfolio" in my file, you'll see that I explicitely filtered on "Kloten" to compare like for like:

 

image.png

 

The problem with your sample data is that the input data and your sample of the desired results are not compatible.

 

I would expect my solution to work on your full data as well.

 

If it doesn't, please adjust your sample data so that I can adjust my file accordingly.

Thanks.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Anonymous
Not applicable

@ImkeF 

 

I did see that you filtered for Kloten, but whithout the filter you get the same result as I posted earlier. 

I once tried a solution where you first map the municipality of the portfolio with the Benchmark data, so an additional column in the Benchmark file, where the municipalities which are in portfolio gets a one. But then I got stack.

Hi

oh yes, I see that now (proper sample data would have helped, tbh..)

More operations have to be applied on the aggregated level:

 

let
    Start = Portfolio,
    #"Grouped Rows" = Table.Group(Start, {"street", "municipality", "unit"}, {{"Object", each _, type table [year=number, street=text, municipality=text, unit=text, vacancy=number]}}),
    SelectBenchmark = Table.AddColumn(#"Grouped Rows", "BenchmarkData", each Table.SelectRows(#"Benchmark Data", (x) => x[municipality] = [municipality])),
    MergeOnYear = Table.AddColumn(SelectBenchmark, "MergedBenchmark", each Table.NestedJoin([Object], {"year"}, [BenchmarkData] , {"year"}, "Benchmark Data", JoinKind.FullOuter)),
    Combine = Table.AddColumn(MergeOnYear, "Custom.1", each Table.AddColumn([MergedBenchmark], "Custom", (x) => Table.FromRecords( { x } ) & x[Benchmark Data])),
    ExpandCustom = Table.AddColumn(Combine, "Custom.2", each Table.Combine( [Custom.1]  [Custom] )),
    FillDown = Table.AddColumn(ExpandCustom, "Custom.3", each Table.FillDown([Custom.2],{"street"})),
    Custom2 = Table.Combine ( FillDown[Custom.3] ),
    #"Filtered Rows" = Table.SelectRows(Custom2, each ([unit] <> null)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Benchmark Data"})
in
    #"Removed Columns"

 

Please check attached file.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Anonymous
Not applicable

Hi @ImkeF 

 

you're my star! Thank you so much for your help, very much appreciated!

Greg_Deckler
Super User
Super User

@ImkeF 


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.

Top Solution Authors