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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
jk8979356
Regular Visitor

How to sum multiple rows from another table

I have a table that has [Subsidiary] [Category] [2019 Sales] [2020 Sales] columns with thousands of rows and I need to either sum the 2019 & 2029 sales rows in this table or in a new reference table so that the output is

 [Subsidiary] [Category][2019 Sales][2020 Sales]

{Sub1}{Cat1}{19 sales total}{20 sales total}

{Sub1}{Cat2}{19 sales total}{20 sales total}

{Sub2}{Cat1}{19 sales total}{20 sales total}

{Sub2}{Cat2}{19 sales total}{20 sales total}

 

Where each row sales total is the total for only that specific subsidiary>category combo. What is the cost to do this?

2 REPLIES 2
edhans
Super User
Super User

Can you provide some sample data @jk8979356 per the links below? For a rough overview on the direction to go, see this code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSUTIEYlMgtlSK1YlWSgKyjKAi5uZgoWQg0xiIzYDYAiySAmQVgRSAFcbGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Subsidiary = _t, Category = _t, #"2019 Sales" = _t, #"2020 Sales" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"2019 Sales", Int64.Type}, {"2020 Sales", Int64.Type}}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Subsidiary", "Category"}, "Attribute", "Value"),
    #"Extracted First Characters" = Table.TransformColumns(#"Unpivoted Other Columns", {{"Attribute", each Text.Start(_, 4), type text}}),
    #"Renamed Columns" = Table.RenameColumns(#"Extracted First Characters",{{"Attribute", "Year"}, {"Value", "Sales"}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Year", Int64.Type}})
in
    #"Changed Type1"

Turns this:

edhans_0-1602546408525.png

into this:

edhans_1-1602546439525.pngWhich is where @MattAllington was going as far as properly modeling your data before bringing it into the Data Model. Now you can connect that to a Date Table and do all sorts of things easily.

 

How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.

 

How to get good help fast. Help us help you.
How to Get Your Question Answered Quickly
How to provide sample data in the Power BI Forum

 

 



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
MattAllington
Community Champion
Community Champion

I'm not sure what you are asking, but the better way to shape your data is to load a table that looks like this

Subsidiary, Category, Year, Sales

 

with a table structured like this, it is easy to build summary matrix visual by dragging columns

Subsidiary, Category, to the rows of the matrix

Year, to the column

Sales to the values



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.
I will not give you bad advice, even if you unknowingly ask for it.

Helpful resources

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

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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