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
KH_Mike
Helper III
Helper III

How to sum if in Power Query

Hi All,

 

I have two table with linkup key. Table A is store for the transction, table B is store for the breakdown for each transaction PL information. Like below two tables.

 

Table 1

Linkup.KeyYearMonthProductAmount
1111111111202201A10
1111111112202201B9
1111111113202201A8
1111111114202202C7
1111111115202203D6

 

Table 2

Linkup.KeyTypePrice
1111111111Revenue100
1111111112Cost90
1111111113Revenue80
1111111114Cost70
1111111115Revenue60
1111111111Cost50
1111111112Revenue40
1111111113Cost30
1111111114Revenue20
1111111115Cost10
1111111111Revenue21

 

Before that I load both tables and calculate the related Revenue / Cost for each Product (Key). Since the dataset become bigger and bigger, I want to know if there is any way to calculate Revenue / Cost and make it on table A so that I don't need to load the Table 2.

 

Thank you.

1 ACCEPTED SOLUTION
SpartaBI
Community Champion
Community Champion

@KH_Mike 
Yes you can.
This is one example of a code that will do that:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8snMyy4t0PNOrVTSUYpMTSzyzc8ryQCyA4ryU0qTS4Asx9z80rwSpVidaCVDOACKGxkYGRmAGI5AbGiAqsAIWYETEFuiyhujG2CBKm8CkwcZ5AzE5qjypjB5kEEuQGymFBsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t, #"(blank).3" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}, {"(blank).2", type text}, {"(blank).3", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Linkup.Key", Int64.Type}, {"YearMonth", Int64.Type}, {"Product", type text}, {"Amount", Int64.Type}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type1", {"Linkup.Key"}, Table2, {"Linkup.Key"}, "Table2", JoinKind.LeftOuter),
    #"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Type", "Price"}, {"Type", "Price"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded Table2", each ([Type] = "Revenue")),
    #"Merged Queries1" = Table.NestedJoin(#"Filtered Rows", {"Linkup.Key"}, Table2, {"Linkup.Key"}, "Table2", JoinKind.LeftOuter),
    #"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries1", "Table2", {"Type", "Price"}, {"Type.1", "Price.1"}),
    #"Filtered Rows1" = Table.SelectRows(#"Expanded Table1", each ([Type.1] = "Cost")),
    #"Renamed Columns" = Table.RenameColumns(#"Filtered Rows1",{{"Price", "Revenue"}, {"Price.1", "Cost"}}),
    #"Removed Columns" = Table.RemoveColumns(#"Renamed Columns",{"Type", "Type.1"})
in
    #"Removed Columns"


What I did is to bring both your tables and then I continued transform table1 so the result is still in table1.
Take only the steps starting from the first Merge, you will probably need to change the names to your names in the query.
Aftet that, just disable the load for table 2.
In case it answered your question please mark this as a solution for community visibility. Appreciate Your Kudos 🙂

View solution in original post

1 REPLY 1
SpartaBI
Community Champion
Community Champion

@KH_Mike 
Yes you can.
This is one example of a code that will do that:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8snMyy4t0PNOrVTSUYpMTSzyzc8ryQCyA4ryU0qTS4Asx9z80rwSpVidaCVDOACKGxkYGRmAGI5AbGiAqsAIWYETEFuiyhujG2CBKm8CkwcZ5AzE5qjypjB5kEEuQGymFBsLAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t, #"(blank).3" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}, {"(blank).2", type text}, {"(blank).3", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Linkup.Key", Int64.Type}, {"YearMonth", Int64.Type}, {"Product", type text}, {"Amount", Int64.Type}}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type1", {"Linkup.Key"}, Table2, {"Linkup.Key"}, "Table2", JoinKind.LeftOuter),
    #"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"Type", "Price"}, {"Type", "Price"}),
    #"Filtered Rows" = Table.SelectRows(#"Expanded Table2", each ([Type] = "Revenue")),
    #"Merged Queries1" = Table.NestedJoin(#"Filtered Rows", {"Linkup.Key"}, Table2, {"Linkup.Key"}, "Table2", JoinKind.LeftOuter),
    #"Expanded Table1" = Table.ExpandTableColumn(#"Merged Queries1", "Table2", {"Type", "Price"}, {"Type.1", "Price.1"}),
    #"Filtered Rows1" = Table.SelectRows(#"Expanded Table1", each ([Type.1] = "Cost")),
    #"Renamed Columns" = Table.RenameColumns(#"Filtered Rows1",{{"Price", "Revenue"}, {"Price.1", "Cost"}}),
    #"Removed Columns" = Table.RemoveColumns(#"Renamed Columns",{"Type", "Type.1"})
in
    #"Removed Columns"


What I did is to bring both your tables and then I continued transform table1 so the result is still in table1.
Take only the steps starting from the first Merge, you will probably need to change the names to your names in the query.
Aftet that, just disable the load for table 2.
In case it answered your question please mark this as a solution for community visibility. Appreciate Your Kudos 🙂

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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