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
erajka
Employee
Employee

Merging Rows from many to 2 per ID for a specific value

I have two tables, one (ID-Table) with IDs, Team Name, Area Name and another (ComplianceData) with duplicates of IDs with their different compliance states/count of compliant items. I want to merge these two together to the outcome below.

ID-Table:

IDTeam NameArea Name
1AAA
2BBB
3CCC

ComplianceData:

IDCompliance StateNumber of Items
1Noncompliant22
1Compliant300
1Noncompliant23
2Compliant499
2Noncompliant 9
2Noncompliant1
3Compliant70
3Noncompliant30

Final Table Wanted:

IDTeamNameAreaNameCompliance State 
1AAACompliant300
1AAANoncompliant45
2BBBCompliant499
2BBBNoncompliant10
3CCCCompliant70
3CCCNoncompliant30

 

I have tried merging queries together, but then I am down to just one row for each ID...Is it possible to have 2 rows per ID, one for Compliant and one for Noncompliant? 

1 ACCEPTED SOLUTION
dax
Community Support
Community Support

Hi @erajka , 

You could use M code or DAX to achieve this goal.

DAX: create relationship between two table, then show them in table.

M code: use merge based on ID , then use groupby to get sum value.

 

let
    Source = Table.NestedJoin(#"ID-Table", {"ID"}, ComplianceData, {"ID"}, "ComplianceData", JoinKind.LeftOuter),
    #"Expanded ComplianceData" = Table.ExpandTableColumn(Source, "ComplianceData", {"Compliance State", "Number of Items"}, {"Compliance State", "Number of Items"}),
    #"Grouped Rows" = Table.Group(#"Expanded ComplianceData", {"ID", "Team Name", "Area Name", "Compliance State"}, {{"sum", each List.Sum([Number of Items]), type number}})
in
    #"Grouped Rows"

 

You could refer to my sample for details.

Best Regards,
Zoe Zhi

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
dax
Community Support
Community Support

Hi @erajka , 

You could use M code or DAX to achieve this goal.

DAX: create relationship between two table, then show them in table.

M code: use merge based on ID , then use groupby to get sum value.

 

let
    Source = Table.NestedJoin(#"ID-Table", {"ID"}, ComplianceData, {"ID"}, "ComplianceData", JoinKind.LeftOuter),
    #"Expanded ComplianceData" = Table.ExpandTableColumn(Source, "ComplianceData", {"Compliance State", "Number of Items"}, {"Compliance State", "Number of Items"}),
    #"Grouped Rows" = Table.Group(#"Expanded ComplianceData", {"ID", "Team Name", "Area Name", "Compliance State"}, {{"sum", each List.Sum([Number of Items]), type number}})
in
    #"Grouped Rows"

 

You could refer to my sample for details.

Best Regards,
Zoe Zhi

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Select ComplianceData

 

Go to Home 

Merge Queries 

 

Select ID on both ComplianceData: and ID-Table then hit enter. 

 

Karlos_0-1595448127532.png

 

Then expand out ID-table tab 

 

Karlos_1-1595448158330.png

 

Result 

 

Karlos_2-1595448174020.png

 

Reorder if needed

 

Karlos_3-1595448207710.png

 

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.