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

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

Reply
SaberSuM
Frequent Visitor

Need help with Power Query, Suming with conditions

Hi,

 

Is there a anyway in power query to transform the sample data into the second table below. Basically, some employees need to split costs based on a ratio to other cost centres (B, C, D or E). Using staff B as an example, his costs should be split across cost centre C & D while no costs should remain at cost centre B.

 

Sample Data:

Account CodeAccount NameAmountCost CentreStaffBCDE
446Salary1000AEmployee A    
447Annual Leave100AEmployee A    
449Superannuation100AEmployee A    
446Salary2000BEmployee B 10001000 
447Annual Leave200BEmployee B 100100 
449Superannuation100BEmployee B 5050 
446Salary2500CEmployee C  2000500
447Annual Leave100CEmployee C  8020
449Superannuation100CEmployee C  8020

 

Output:

Cost CentreAccount CodeAccount NameAmount
A446Salary1000
A447Annual Leave100
A449Superannuation100
B446Salary0
B447Annual Leave0
B449Superannuation0
C446Salary1000
C447Annual Leave100
C449Superannuation50
D446Salary3000
D447Annual Leave180
D449Superannuation130
E446Salary500
E447Annual Leave20
E449Superannuation20

 

Thank you in advance

1 ACCEPTED SOLUTION

use Record.RemoveFields instead

View solution in original post

3 REPLIES 3
AlienSx
Super User
Super User

let
    Source = sample_data,
    upd_amount = Table.AddColumn(
        Source, "updated_amount", 
        each  - List.Sum(
            {-[Amount]} & 
                Record.FieldValues(
                    Record.SelectFields(
                        _, 
                        {"A", "B", "C", "D", "E"}, 
                        MissingField.Ignore
                    )
                )
        )
    )[[Account Code], [Account Name], [Cost Centre], [updated_amount]],
    shared_costs = Table.UnpivotOtherColumns(
        Table.RemoveColumns(Source, {"Cost Centre", "Staff", "Amount"}), 
        {"Account Code", "Account Name"}, 
        "Cost Centre", "updated_amount"
    ),
    output = Table.Group(
        upd_amount & shared_costs, 
        {"Account Code", "Account Name", "Cost Centre"}, 
        {{"Amount", each List.Sum([updated_amount])}}
    )
in
    output

is there anyway to avoid listing out the cost centres. The full list of cost centres might change on a monthly basis, and it would require constant updates to the code.

 

Thank you!

use Record.RemoveFields instead

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