Forum Discussion

erajka's avatar
erajka
Microsoft Employee
6 years ago
Solved

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? 

  • 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.

2 Replies

  • dax's avatar
    dax
    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's avatar
    Anonymous
    Not applicable

    Select ComplianceData

     

    Go to Home 

    Merge Queries 

     

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

     

     

    Then expand out ID-table tab 

     

     

    Result 

     

     

    Reorder if needed