Forum Discussion

rodiba's avatar
rodiba
Regular Visitor
3 years ago
Solved

Create dynamic table for cost allocation

Good morning,

We have users managing files and we would like, for accounting purposes, to calculate the analytic cost associated to any file, distributing the cost of any user over all the files he is managing.

 

I have a table containing costs per user, with a specific date. I would like to distribute those costs equally on all the files the user is managing, keeping the reference to that specific date.

Data is organised as follows.

 

I have a table with the costs per user (table A), referred to a specific date:

DateUserCost
01/01/2023Bob100
03/01/2023Tom60

 

In another table (table B), I have the list of files assigned to the users:

FileAssigned to
1Bob
2Bob
3Bob
4Bob
5Tom
6Tom
7Tom
8Tom
9Tom
10Tom

 

I would like to calculate a table containing the costs distributed per file and date. The result should be:

DateFileCost per fileNote
01/01/2023125Bob's cost divided by the number of assigned files (=4)
01/01/2023225 
01/01/2023325 
01/01/2023425 
03/01/2023510Tom's cost divided by the number of assigned files (=6)
03/01/2023610 
03/01/2023710 
03/01/2023810 
03/01/2023910 
03/01/20231010 

 

Thanks in adavnce for your help.

 

Roberto

  • Hi rodiba ,

     

    Please try:

    Duplicate Table A

    Then Append Queries(Table A left outer join Table B)

    Expand Table B

    Add custom column

    remove other columns

    Here is the M code:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUByIjAyNjJR0lp/wkIGloYKAUqwOUMkaSCsnPBZJmUBlTDE0gmVgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, User = _t, Cost = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"User", type text}, {"Cost", Int64.Type}}),
        #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"User"}, #"Table B", {"Assigned to"}, "Table B", JoinKind.LeftOuter),
        #"Expanded Table B" = Table.ExpandTableColumn(#"Merged Queries", "Table B", {"File", "Assigned to"}, {"Table B.File", "Table B.Assigned to"}),
        #"Added Custom" = Table.AddColumn(#"Expanded Table B", "Custom", each [Cost]/
    Table.RowCount(
    Table.SelectRows(#"Table B",(x)=>x [Assigned to]=[User]))),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Cost", "Table B.Assigned to"})
    in
        #"Removed Columns"

    Final output:

    Best Regards,

    Jianbo Li

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

3 Replies

    • rodiba's avatar
      rodiba
      Regular Visitor

      Hi DimaMD ,

      thanks for your response. It works for my simplified data, but doesn't work anymore if Table A has multiple rows for a user on a different date, that can be a possibile situation.
      If, for example Bob has another cost (red line below), result table shold have more extra lines.

       

       

      DateUserCost
      01/01/2023Bob100
      03/01/2023Tom60
      05/01/2023Bob60

       

      Result shoud be:

      DateFileCost per fileNote
      01/01/2023125Bob's cost divided by the number of assigned files (=4)
      01/01/2023225 
      01/01/2023325 
      01/01/2023425 
      03/01/2023510Tom's cost divided by the number of assigned files (=6)
      03/01/2023610 
      03/01/2023710 
      03/01/2023810 
      03/01/2023910 
      03/01/20231010 
      05/01/2023115 
      05/01/2023215 
      05/01/2023315 
      05/01/2023415 
  • Hi rodiba ,

     

    Please try:

    Duplicate Table A

    Then Append Queries(Table A left outer join Table B)

    Expand Table B

    Add custom column

    remove other columns

    Here is the M code:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUByIjAyNjJR0lp/wkIGloYKAUqwOUMkaSCsnPBZJmUBlTDE0gmVgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, User = _t, Cost = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"User", type text}, {"Cost", Int64.Type}}),
        #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"User"}, #"Table B", {"Assigned to"}, "Table B", JoinKind.LeftOuter),
        #"Expanded Table B" = Table.ExpandTableColumn(#"Merged Queries", "Table B", {"File", "Assigned to"}, {"Table B.File", "Table B.Assigned to"}),
        #"Added Custom" = Table.AddColumn(#"Expanded Table B", "Custom", each [Cost]/
    Table.RowCount(
    Table.SelectRows(#"Table B",(x)=>x [Assigned to]=[User]))),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Cost", "Table B.Assigned to"})
    in
        #"Removed Columns"

    Final output:

    Best Regards,

    Jianbo Li

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