Forum Discussion

SaberSuM's avatar
SaberSuM
Frequent Visitor
2 years ago
Solved

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

3 Replies

  • 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
    • SaberSuM's avatar
      SaberSuM
      Frequent Visitor

      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!

      • AlienSx's avatar
        AlienSx
        Super User

        use Record.RemoveFields instead