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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
amansingh316
New Member

Help with determining division of cost

Hi All, 

I need some help with a scenario where I have a list of items with a cost

ItemCost
Computer100
Storage10
Printer20

 

Then a list of locations and sub-locations that consume said items, where i must divide the cost equally by location and then by sub-location

ItemLocationSub-Location
ComputerAA1
ComputerAA2
ComputerBB1
ComputerCC1
ComputerCC2
ComputerDD1

 

How would i link and the determine the cost if in the above scenario I wanted to charge in the following manner:

Location A-D get charged 25 and then sub-location can be an equal split of the location cost i.e. A1 and A2 get 12.5 each and B1 just gets 25?

Then to be able to sum the total location cost and sub location costs independently in the dashboard, which should always equal the original item cost. Much appreciated.

1 ACCEPTED SOLUTION
dufoq3
Super User
Super User

Here you are. It could be done easier, but I'd like you to have possibility to see every single step.

 

let
    TotalCostTable =
        #table(
            type table
            [Item = text, Cost = Currency.Type],
            {{"Computer", 100}, {"Storage", 10}, {"Printer", 20}}
        ),
    DetailTable = 
        #table(
            type table
            [Item = text, #"Sub-Location" = text],
            {{"ComputerA", "A1"}, {"ComputerA", "A2"}, {"ComputerB", "B1"}, {"ComputerC", "C1"}, {"ComputerC", "C2"}, {"ComputerD", "D1"} }
        ),
    AddedLocation = Table.AddColumn(DetailTable, "Location", each Text.Start([#"Sub-Location"], 1), type text),
    ExtractItemMainCategory = Table.TransformColumns(
            AddedLocation,
            {{"Item", each Text.Start(_, Text.Length(_) -1), type text}}
        
    ),
    DetailTable_GroupBy = Table.Group(ExtractItemMainCategory, {"Location"}, {{"Detail", each _, type table [Item=text, #"Sub-Location"=text, Location=text]}, {"Sublocations Qty", each Table.RowCount(_), Int64.Type}}),
    ExpandedDetail = Table.ExpandTableColumn(DetailTable_GroupBy, "Detail", {"Item", "Sub-Location"}, {"Item", "Sub-Location"}),
    // Merge with query itself. Left Side = 'ExpandedDetail' step, Right Side = 'TotalCostTable' step.
    MergedQueries = Table.NestedJoin(ExpandedDetail, {"Item"}, TotalCostTable, {"Item"}, "ExtractItemMainCategory", JoinKind.LeftOuter),
    ExpandedQuery = Table.ExpandTableColumn(MergedQueries, "ExtractItemMainCategory", {"Cost"}, {"Total Item Cost"}),
    AddedLocationsQty = Table.AddColumn(ExpandedQuery, "Locations Qty", each List.Count(List.Distinct(ExpandedQuery[Location])), Int64.Type),
    #"Reordered Columns" = Table.ReorderColumns(AddedLocationsQty,{"Item", "Location", "Sub-Location", "Locations Qty", "Sublocations Qty", "Total Item Cost"}),
    AddedLocationCost = Table.AddColumn(#"Reordered Columns", "Location Cost", each [Total Item Cost] / [Locations Qty], Currency.Type),
    AddedSubLocationCost = Table.AddColumn(AddedLocationCost, "Sub-Location Cost", each [Location Cost] / [Sublocations Qty], Currency.Type),
    FinalCostTable = Table.SelectColumns(AddedSubLocationCost,{"Item", "Location", "Sub-Location", "Total Item Cost", "Location Cost", "Sub-Location Cost"}),
    LocationDetail_GroupBy = Table.Group(FinalCostTable, {"Location", "Item"}, {{"Location Cost", each List.Average([Location Cost]), Currency.Type}}),
    SubLocationDetail_GroupBy = Table.Group(FinalCostTable, {"Sub-Location", "Item"}, {{"Sub-Location Cost", each List.Average([#"Sub-Location Cost"]), Currency.Type}})
in
    SubLocationDetail_GroupBy

 

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

1 REPLY 1
dufoq3
Super User
Super User

Here you are. It could be done easier, but I'd like you to have possibility to see every single step.

 

let
    TotalCostTable =
        #table(
            type table
            [Item = text, Cost = Currency.Type],
            {{"Computer", 100}, {"Storage", 10}, {"Printer", 20}}
        ),
    DetailTable = 
        #table(
            type table
            [Item = text, #"Sub-Location" = text],
            {{"ComputerA", "A1"}, {"ComputerA", "A2"}, {"ComputerB", "B1"}, {"ComputerC", "C1"}, {"ComputerC", "C2"}, {"ComputerD", "D1"} }
        ),
    AddedLocation = Table.AddColumn(DetailTable, "Location", each Text.Start([#"Sub-Location"], 1), type text),
    ExtractItemMainCategory = Table.TransformColumns(
            AddedLocation,
            {{"Item", each Text.Start(_, Text.Length(_) -1), type text}}
        
    ),
    DetailTable_GroupBy = Table.Group(ExtractItemMainCategory, {"Location"}, {{"Detail", each _, type table [Item=text, #"Sub-Location"=text, Location=text]}, {"Sublocations Qty", each Table.RowCount(_), Int64.Type}}),
    ExpandedDetail = Table.ExpandTableColumn(DetailTable_GroupBy, "Detail", {"Item", "Sub-Location"}, {"Item", "Sub-Location"}),
    // Merge with query itself. Left Side = 'ExpandedDetail' step, Right Side = 'TotalCostTable' step.
    MergedQueries = Table.NestedJoin(ExpandedDetail, {"Item"}, TotalCostTable, {"Item"}, "ExtractItemMainCategory", JoinKind.LeftOuter),
    ExpandedQuery = Table.ExpandTableColumn(MergedQueries, "ExtractItemMainCategory", {"Cost"}, {"Total Item Cost"}),
    AddedLocationsQty = Table.AddColumn(ExpandedQuery, "Locations Qty", each List.Count(List.Distinct(ExpandedQuery[Location])), Int64.Type),
    #"Reordered Columns" = Table.ReorderColumns(AddedLocationsQty,{"Item", "Location", "Sub-Location", "Locations Qty", "Sublocations Qty", "Total Item Cost"}),
    AddedLocationCost = Table.AddColumn(#"Reordered Columns", "Location Cost", each [Total Item Cost] / [Locations Qty], Currency.Type),
    AddedSubLocationCost = Table.AddColumn(AddedLocationCost, "Sub-Location Cost", each [Location Cost] / [Sublocations Qty], Currency.Type),
    FinalCostTable = Table.SelectColumns(AddedSubLocationCost,{"Item", "Location", "Sub-Location", "Total Item Cost", "Location Cost", "Sub-Location Cost"}),
    LocationDetail_GroupBy = Table.Group(FinalCostTable, {"Location", "Item"}, {{"Location Cost", each List.Average([Location Cost]), Currency.Type}}),
    SubLocationDetail_GroupBy = Table.Group(FinalCostTable, {"Sub-Location", "Item"}, {{"Sub-Location Cost", each List.Average([#"Sub-Location Cost"]), Currency.Type}})
in
    SubLocationDetail_GroupBy

 

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

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!

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.