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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
KoroneL
Frequent Visitor

Cost Analysis in Percentage with Data from Different Tables and Drill Down

Hi everyone,

I'm working on a cost analysis where I need to calculate values as percentages. However, my data is spread across two tables: a main table and a secondary table. Additionally, I need to replace some rows in my main table using data from the drill down available in the secondary table.

To clarify my issue, here is a simple example with sample data and a final table with the result i want to get. My goal is to create all measures from the final table as it looks much more simple than working other way around. 

 

I've been trying to sort things out with merge table but can't solve the fact that the columns don't merge. Any ideas? 

Here are some sample data of the two original table and the one i seek to produce.

 

Thanks.

 

Main table - portfolio  
Asset TypeValue 
alpha,incequity $                 100,00 
Beta AGbond $                 200,00 
C MainOIC $                    75,00*
Delta SEequity $                 150,00 
Gama Debond $                 110,00 
F MainOIC $                    80,00**
    
    
secondary table - OICPortfolio 
OICAssetTypeValue
C MainPhi, SAequity $ 55 000,00
C MainOmega SEbond $   5 000,00
C MainNu Incequity $ 90 000,00
C Mainalpha,incequity $ 54 500,00
F MainKappa BVequity $ 97 000,00
F MainZeta Corpbond $ 69 000,00
F MainLambda ESequity $ 10 000,00
F MainTheta SWequity $   9 000,00
    
example of "new" or "renewed" table  
Asset TypeValue 
alpha,incequity $                 100,00 
Beta AGbond $                 200,00 
Phi, SAequity $                    20,17*
Omega SEbond $                      1,83*
Nu Incequity $                    33,01*
alpha,incequity $                    19,99*
Delta SEequity $                 150,00 
Gama Debond $                 110,00 
Kappa BVequity $                    41,95**
Zeta Corpbond $                    29,84**
Lambda ESequity $                      4,32**
Theta SWequity $                      3,89**
1 ACCEPTED SOLUTION
Omid_Motamedise
Super User
Super User

Thanks for sharing such a lovely question

please find the solution as bellow.

 

let
    Table1 = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSswpyEjUycxLVtJRSi0szSypBDIMDQyUYnWilZxSSxIVHN2BIkn5eSlAyggq4azgm5iZBxTw93QGkuamYFGX1Byg+mBXFKNMITrcE3MTFVxSEUYZGkIk3FCNsgCKxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Asset = _t, Type = _t, Value = _t]),
    CHT1 = Table.TransformColumnTypes(Table1,{{"Value", Int64.Type}}),
    Table2= Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclbwTczMU9JRCsjI1FEIdgSyUgtLM0sqgQxTUwMDA6VYHSRV/rmp6YkKwa5AZlJ+XgpIEYYav1IFz7xkZIMsDTAUJeYUZCTqZKKqMzUxhapzg6nzTiwoSFRwCkMxztwAXVlUakmignN+UQHCYWaWGKp8EnOTUhIVXIORTTM0wFAXkgEyLjgc3Q9KsbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [OIC = _t, Asset = _t, Type = _t, Value = _t]),
    CHT2 = Table.TransformColumnTypes(Table2,{{"Value", Int64.Type}}),
    #"Merged Queries" = Table.NestedJoin(CHT1, {"Asset"}, CHT2, {"OIC"}, "Table2", JoinKind.LeftOuter),
    #"Added Custom" = Table.AddColumn(#"Merged Queries", "Table2 modified", each if Table.IsEmpty([Table2]) then Table.FromRecords({_}) else Table.TransformColumns([Table2],{"Value",(x)=>x*[Value]/List.Sum([Table2][Value])})),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Table2 modified"}),
    #"Expanded Table2 modified" = Table.ExpandTableColumn(#"Removed Other Columns", "Table2 modified", {"Asset", "Type", "Value"}, {"Asset", "Type", "Value"})
in
    #"Expanded Table2 modified"
If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h

View solution in original post

2 REPLIES 2
Omid_Motamedise
Super User
Super User

Thanks for sharing such a lovely question

please find the solution as bellow.

 

let
    Table1 = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSswpyEjUycxLVtJRSi0szSypBDIMDQyUYnWilZxSSxIVHN2BIkn5eSlAyggq4azgm5iZBxTw93QGkuamYFGX1Byg+mBXFKNMITrcE3MTFVxSEUYZGkIk3FCNsgCKxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Asset = _t, Type = _t, Value = _t]),
    CHT1 = Table.TransformColumnTypes(Table1,{{"Value", Int64.Type}}),
    Table2= Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclbwTczMU9JRCsjI1FEIdgSyUgtLM0sqgQxTUwMDA6VYHSRV/rmp6YkKwa5AZlJ+XgpIEYYav1IFz7xkZIMsDTAUJeYUZCTqZKKqMzUxhapzg6nzTiwoSFRwCkMxztwAXVlUakmignN+UQHCYWaWGKp8EnOTUhIVXIORTTM0wFAXkgEyLjgc3Q9KsbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [OIC = _t, Asset = _t, Type = _t, Value = _t]),
    CHT2 = Table.TransformColumnTypes(Table2,{{"Value", Int64.Type}}),
    #"Merged Queries" = Table.NestedJoin(CHT1, {"Asset"}, CHT2, {"OIC"}, "Table2", JoinKind.LeftOuter),
    #"Added Custom" = Table.AddColumn(#"Merged Queries", "Table2 modified", each if Table.IsEmpty([Table2]) then Table.FromRecords({_}) else Table.TransformColumns([Table2],{"Value",(x)=>x*[Value]/List.Sum([Table2][Value])})),
    #"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Table2 modified"}),
    #"Expanded Table2 modified" = Table.ExpandTableColumn(#"Removed Other Columns", "Table2 modified", {"Asset", "Type", "Value"}, {"Asset", "Type", "Value"})
in
    #"Expanded Table2 modified"
If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h
dufoq3
Super User
Super User

Hi @KoroneL, check this:

I don't know how did you calculate [Value] in your sample output, but you should be albe to do it with output below

 

Output

dufoq3_0-1727458248425.png

let
    MainTable = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSswpyEjUycxLVtJRSi0szSypBDIUVBTQgaGBgY6BgVKsTrSSU2pJooKjO1BdUn5eCnblRgjlzgq+iZl5QGX+ns7YFQOBuSlMuUtqDtD4YFdC7jGFW+CemJuo4JKK1z2GhnDlbsS4xwKiPBYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Asset = _t, Type = _t, Value = _t]),
    SecondaryTable = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc69CsIwGIXhWzkExwwRjNJRSwXxFyIKlg5fbbAB20ZpB+/eimhrzHaGh5cTxyzEmkzJONvlhkNN26Vvjakf7cAAUkIIwYVgCe/hbaEvBBW1M63K7G0BP940WJRnJxwIr6WrzYmbPy5HkB2ff/iSrCXMDm580o9/9UnXhLC625/b48CLV1SkGSFSTnsovHyfv+Lq6GigqydP", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [OIC = _t, Asset = _t, Type = _t, Value = _t]),
    MainTableChangedType = Table.TransformColumns(MainTable,{{"Value", each Currency.From(Text.Remove(_, " "), "en-US"), Currency.Type}}),
    SecondaryTableChangedType = Table.TransformColumns(SecondaryTable,{{"Value", each Currency.From(Text.Remove(_, " "), "en-US"), Currency.Type}}),
    MergedQueries = Table.NestedJoin(MainTableChangedType, {"Asset"}, SecondaryTableChangedType, {"OIC"}, "SecondaryTableChangedType", JoinKind.LeftOuter),
    // Necessary if you want to preserve sort order.
    AddedIndex = Table.AddIndexColumn(MergedQueries, "Index", 0, 1, Int64.Type),
    ExpandedSecondaryTableChangedType = Table.ExpandTableColumn(AddedIndex, "SecondaryTableChangedType", {"Asset", "Value"}, {"ST Asset", "ST Value"}),
    ReplacedAsset = Value.ReplaceType(Table.ReplaceValue(ExpandedSecondaryTableChangedType,
        each [St Asset] <> null,
        each [ST Asset],
        (x,y,z)=> if y then z else x,
        {"Asset"} ), Value.Type(Table.FirstN(ExpandedSecondaryTableChangedType, 0))),
    RemovedColumns = Table.RemoveColumns(ReplacedAsset,{"ST Asset", "Index"})
in
    RemovedColumns

 


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
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.

Top Solution Authors