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

Joining Tables with Different number of rows and columns

Hi! I am looking to implement a solution that will create a joined table at created from two tables that have different numbers of rows / columns. To make the example and ideal outcome simpler, I have included example tables below. 

 

Table 1 consists of Categories (that get repeated in each row) and subcategories that roll up / align to the categories.  

 

Table 2 consists of the Subcategories from Table 1, but with "Yes / No / N/A" fields to account for certain criteria within the dataset. The criteria ID is just a binary field in Power BI that uses the value to define it numerically.

 

The ideal table will have everything rolled up to the "Category" level, so the number of rows will be equal to the number of Categories. Then the columns will expand to the number of Subcategories within the dataset, then creating a SUM of the binary criteria ID's that will be used in the report. 

 

I am seeking a solution that will create this functionality via Power Query or the Data view. Please see examples below as that may clarify the ask!

 

Thank you!

 

 

Table 1 
CategorySubcategory
AA.BB-1Z1
AA.BB-1X2
AA.BB-1V3
CC.DD-2Q1
CC.DD-2W2
EE.FF-1P1
EE.FF-1R3
EE.FF-1K4
EE.FF-1L5
EE.FF-1M8
EE.FF-1

G2

 

 

 

Table 2      
SubcategoryCriteria 1Criteria 2Criteria 3Criteria 1 IDCriteria 2 IDCriteria 3 ID
Z1YesN/AN/A100
X2NoNoYes001
V3NoNoN/A000
Q1YesNoNo100
W2YesN/AN/A100
P1YesYesYes111
R3NoYesYes011
K4NoNoYes001
L5YesYesNo110
M8NoN/AN/A000
G2YesNoNo100

 

 

 

Ideal Outcome       
CategorySubcategory 1Subcategory 2Subcategory 3Subcategory 4Subcategory 5Subcategory 6Sum(Criteria ID)
AA.BB-1Z1X2V3N/AN/AN/A2
CC.DD-2Q1W2N/AN/AN/AN/A2
EE.FF-1P1R3K4L5M8G29

 

1 ACCEPTED SOLUTION
v-jingzhang
Community Support
Community Support

Hi @Anonymous 

 

You can achieve this with Power Query.

082008.jpg

 

M codes:

let
    Source = Table.NestedJoin(#"Table 1", {"Subcategory"}, #"Table 2", {"Subcategory"}, "Table 2", JoinKind.LeftOuter),
    #"Expanded Table 2" = Table.ExpandTableColumn(Source, "Table 2", {"Criteria 1 ID", "Criteria 2 ID", "Criteria 3 ID"}, {"Criteria 1 ID", "Criteria 2 ID", "Criteria 3 ID"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Table 2", "Sum(Criteria ID)", each [Criteria 1 ID]+[Criteria 2 ID]+[Criteria 3 ID]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Criteria 1 ID", "Criteria 2 ID", "Criteria 3 ID"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns", {"Category"}, {{"GroupTable", each _, type table [Category=nullable text, Subcategory=nullable text, #"Sum(Criteria ID)"=number]}}),
    #"Added Custom1" = Table.AddColumn(#"Grouped Rows", "SubcategoryList", each [GroupTable][Subcategory]),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom1", {"SubcategoryList", each Text.Combine(List.Transform(_, Text.From), "#(tab)"), type text}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values", "SubcategoryList", Splitter.SplitTextByDelimiter("#(tab)", QuoteStyle.Csv), {"SubcategoryList.1", "SubcategoryList.2", "SubcategoryList.3", "SubcategoryList.4", "SubcategoryList.5", "SubcategoryList.6"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"SubcategoryList.1", type text}, {"SubcategoryList.2", type text}, {"SubcategoryList.3", type text}, {"SubcategoryList.4", type text}, {"SubcategoryList.5", type text}, {"SubcategoryList.6", type text}}),
    #"Aggregated GroupTable" = Table.AggregateTableColumn(#"Changed Type", "GroupTable", {{"Sum(Criteria ID)", List.Sum, "Sum of Sum(Criteria ID)"}}),
    #"Replaced Value" = Table.ReplaceValue(#"Aggregated GroupTable",null,"N/A",Replacer.ReplaceValue,{"SubcategoryList.1", "SubcategoryList.2", "SubcategoryList.3", "SubcategoryList.4", "SubcategoryList.5", "SubcategoryList.6"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Replaced Value",{"Category", "SubcategoryList.1", "SubcategoryList.2", "SubcategoryList.3", "SubcategoryList.4", "SubcategoryList.5", "SubcategoryList.6", "Sum of Sum(Criteria ID)"})
in
    #"Reordered Columns"

 

Download the attached pbix to see all steps.

 

Regards,
Community Support Team _ Jing
If this post helps, please Accept it as the solution to help other members find it.

View solution in original post

2 REPLIES 2
v-jingzhang
Community Support
Community Support

Hi @Anonymous 

 

You can achieve this with Power Query.

082008.jpg

 

M codes:

let
    Source = Table.NestedJoin(#"Table 1", {"Subcategory"}, #"Table 2", {"Subcategory"}, "Table 2", JoinKind.LeftOuter),
    #"Expanded Table 2" = Table.ExpandTableColumn(Source, "Table 2", {"Criteria 1 ID", "Criteria 2 ID", "Criteria 3 ID"}, {"Criteria 1 ID", "Criteria 2 ID", "Criteria 3 ID"}),
    #"Added Custom" = Table.AddColumn(#"Expanded Table 2", "Sum(Criteria ID)", each [Criteria 1 ID]+[Criteria 2 ID]+[Criteria 3 ID]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Criteria 1 ID", "Criteria 2 ID", "Criteria 3 ID"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns", {"Category"}, {{"GroupTable", each _, type table [Category=nullable text, Subcategory=nullable text, #"Sum(Criteria ID)"=number]}}),
    #"Added Custom1" = Table.AddColumn(#"Grouped Rows", "SubcategoryList", each [GroupTable][Subcategory]),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom1", {"SubcategoryList", each Text.Combine(List.Transform(_, Text.From), "#(tab)"), type text}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values", "SubcategoryList", Splitter.SplitTextByDelimiter("#(tab)", QuoteStyle.Csv), {"SubcategoryList.1", "SubcategoryList.2", "SubcategoryList.3", "SubcategoryList.4", "SubcategoryList.5", "SubcategoryList.6"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"SubcategoryList.1", type text}, {"SubcategoryList.2", type text}, {"SubcategoryList.3", type text}, {"SubcategoryList.4", type text}, {"SubcategoryList.5", type text}, {"SubcategoryList.6", type text}}),
    #"Aggregated GroupTable" = Table.AggregateTableColumn(#"Changed Type", "GroupTable", {{"Sum(Criteria ID)", List.Sum, "Sum of Sum(Criteria ID)"}}),
    #"Replaced Value" = Table.ReplaceValue(#"Aggregated GroupTable",null,"N/A",Replacer.ReplaceValue,{"SubcategoryList.1", "SubcategoryList.2", "SubcategoryList.3", "SubcategoryList.4", "SubcategoryList.5", "SubcategoryList.6"}),
    #"Reordered Columns" = Table.ReorderColumns(#"Replaced Value",{"Category", "SubcategoryList.1", "SubcategoryList.2", "SubcategoryList.3", "SubcategoryList.4", "SubcategoryList.5", "SubcategoryList.6", "Sum of Sum(Criteria ID)"})
in
    #"Reordered Columns"

 

Download the attached pbix to see all steps.

 

Regards,
Community Support Team _ Jing
If this post helps, please Accept it as the solution to help other members find it.

Fowmy
Super User
Super User

@Anonymous 

In Power Query UnPivot table 2 using the technique shown here and merge table 1 to table 2 using subcategory then expand the Category. You Pivot the Sub Category and get the result or use a matrix visual in Power BI.

You can UnPivot and create a relationship then use a matrix to visualize.


Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

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.