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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
SteveDesmedt
Frequent Visitor

Sum values in child table based on two filters

Hi all, 

 

I'm pretty new in Power Query and would like to achive the following :

 

I have two tables :

Table 1

Entry No.

1

2
3
4

 

Table 2

Entry No.Cost CodeCost Amount
1AA150
2BB200
2AA100
1AA300
4CC200
1CC400
4AA200
3BB150
4CC150

 

I would like to add three additional columns in table 1 per cost code (manually)

 

Entry No.Cost Code AACost Code BBCost Code CC
1450 (150 + 300)0400
21002000
301500
42000350 (200 + 150)

 

I don't know how to achive this in Power query.  I'm not able two have second table filtered on entry No. and fixed filter value (AA, BB, CC) for each different column. 

 

Thanks in advance.

 

Steve

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @SteveDesmedt 

You can create two blank queries and put the following code to advanced editor in power query

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJ0BBKGpgZKsTrRSkZAtpMTkDAyQAhAVEAF4FqMoQImQLazM5IWQ5iACZIKsBaYCmOYLTBr4WaABWIB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Entry No." = _t, #"Cost Code" = _t, #"Cost Amount" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Entry No.", Int64.Type}, {"Cost Code", type text}, {"Cost Amount", Int64.Type}})
in
    #"Changed Type"
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWMgKTxmDSRCk2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Entry No." = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Entry No.", Int64.Type}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Entry No."}, Query1, {"Entry No."}, "Query1", JoinKind.LeftOuter),
    #"Expanded Query1" = Table.ExpandTableColumn(#"Merged Queries", "Query1", {"Cost Code", "Cost Amount"}, {"Cost Code", "Cost Amount"}),
    #"Pivoted Column" = Table.Pivot(#"Expanded Query1", List.Distinct(#"Expanded Query1"[#"Cost Code"]), "Cost Code", "Cost Amount",  List.Sum),
    #"Replaced Value" = Table.ReplaceValue(#"Pivoted Column",null,0,Replacer.ReplaceValue,{"AA", "BB", "CC"})
in
    #"Replaced Value"

 

Output

vxinruzhumsft_0-1702273775557.png

Best Regards!

Yolo Zhu

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
Anonymous
Not applicable

Hi @SteveDesmedt 

You can create two blank queries and put the following code to advanced editor in power query

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJ0BBKGpgZKsTrRSkZAtpMTkDAyQAhAVEAF4FqMoQImQLazM5IWQ5iACZIKsBaYCmOYLTBr4WaABWIB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Entry No." = _t, #"Cost Code" = _t, #"Cost Amount" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Entry No.", Int64.Type}, {"Cost Code", type text}, {"Cost Amount", Int64.Type}})
in
    #"Changed Type"
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlSK1YlWMgKTxmDSRCk2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Entry No." = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Entry No.", Int64.Type}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Entry No."}, Query1, {"Entry No."}, "Query1", JoinKind.LeftOuter),
    #"Expanded Query1" = Table.ExpandTableColumn(#"Merged Queries", "Query1", {"Cost Code", "Cost Amount"}, {"Cost Code", "Cost Amount"}),
    #"Pivoted Column" = Table.Pivot(#"Expanded Query1", List.Distinct(#"Expanded Query1"[#"Cost Code"]), "Cost Code", "Cost Amount",  List.Sum),
    #"Replaced Value" = Table.ReplaceValue(#"Pivoted Column",null,0,Replacer.ReplaceValue,{"AA", "BB", "CC"})
in
    #"Replaced Value"

 

Output

vxinruzhumsft_0-1702273775557.png

Best Regards!

Yolo Zhu

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

AlienSx
Super User
Super User

hello, @SteveDesmedt in PQ Editor select Cost Code column, go to Transform tab, find Pivot Column button,  use Cost Amount as values column and choose Sum as aggregation function. 

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors