Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Anonymous
Not applicable

Sumif based on 2 columns

Hi All, 

 

I have a table something ilike this:

Id1Id2Value1Value2
aa17
ab28
aa39
bb410
ba511
bb612

 

...and need to calculate this

IdValue
a=sum of Value1 where Id1 = a + sum of Value2 where Id2 = a
b=sum of Value1 where Id1 = b + sum of Value2 where Id2 = b

 

That is, for any Id = 'x', I need the sum of Value1 + sum of Value2 where Id1 or Id2 is 'x'. I need it as a measure, but I couldn't figure how to do this efficiently, without practivally duplicating the table.

I'd be grateful for any help!

1 ACCEPTED SOLUTION
LivioLanzo
Solution Sage
Solution Sage

Hello @Anonymous 

 

the best way is to apply this transofmration within Power query and then drop the id in the matrix rows and the sum of value in the matrix values:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSAWNDIDZXitWBiCQBsREQW8BFQNgYiC3BIklQNSYgnQZwIZAiU5CQIYoqM5CQkVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Id1 = _t, Id2 = _t, Value1 = _t, Value2 = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Id1", type text}, {"Id2", type text}, {"Value1", Int64.Type}, {"Value2", Int64.Type}}),

    ReshapeTable = Table.Combine(
                            {
                                Table.SelectColumns(ChangedType, {"Id1", "Value1"}),
                                Table.RenameColumns(Table.SelectColumns(ChangedType, {"Id2", "Value2"}), {{"Id2", "Id1"}, {"Value2", "Value1"}})
                            })
in
    ReshapeTable

 

 


 


Did I answer your question correctly? Mark my answer as a solution!


Proud to be a Datanaut!  

View solution in original post

2 REPLIES 2
LivioLanzo
Solution Sage
Solution Sage

Hello @Anonymous 

 

the best way is to apply this transofmration within Power query and then drop the id in the matrix rows and the sum of value in the matrix values:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSAWNDIDZXitWBiCQBsREQW8BFQNgYiC3BIklQNSYgnQZwIZAiU5CQIYoqM5CQkVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Id1 = _t, Id2 = _t, Value1 = _t, Value2 = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Id1", type text}, {"Id2", type text}, {"Value1", Int64.Type}, {"Value2", Int64.Type}}),

    ReshapeTable = Table.Combine(
                            {
                                Table.SelectColumns(ChangedType, {"Id1", "Value1"}),
                                Table.RenameColumns(Table.SelectColumns(ChangedType, {"Id2", "Value2"}), {{"Id2", "Id1"}, {"Value2", "Value1"}})
                            })
in
    ReshapeTable

 

 


 


Did I answer your question correctly? Mark my answer as a solution!


Proud to be a Datanaut!  

Anonymous
Not applicable

Yup, that's plain and easy, should've thought about it, but I did not consider doing this in PQ! Thanks a lot!

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.

Top Solution Authors