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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
rodiba
Regular Visitor

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

1 ACCEPTED SOLUTION
v-jianboli-msft
Community Support
Community Support

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:

vjianbolimsft_0-1675403897176.png

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.

View solution in original post

3 REPLIES 3
v-jianboli-msft
Community Support
Community Support

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:

vjianbolimsft_0-1675403897176.png

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.

DimaMD
Solution Sage
Solution Sage

Hi @rodiba Please review the attached file and tell me if this is your desired result
Screenshot_37.jpg


__________________________________________

Thank you for your like and decision

__________________________________________

Greetings from Ukraine

To help me grow PayPal: embirddima@gmail.com

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 

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.