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

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
Anonymous
Not applicable

Power Query - Sum by Category WITHOUT Grouping

Hi,

 

I have a table that has the total population figures for a specific demographic across 5 cities for 2017, 2018 and 2019. I want to work out the percentage of population each city has for each year. Here's an example of the data I'm working with:

 

01.PNG

 

Is there any way to do this where I can create another column with the total across the locations for each year?

 

Thanks,

MarkJames

1 ACCEPTED SOLUTION
Nolock
Resident Rockstar
Resident Rockstar

Hi @Anonymous,

you can group the table and then join it back with the original one.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTI0MgaSRgaG5kqxOjAhU4iQBULI2AAiZAkWcgJzDJA1goSMYUIWcCETA4TGWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Market = _t, Population = _t, Year = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Market", type text}, {"Population", Int64.Type}, {"Year", Int64.Type}}),

    // group by year and sum population
    Aggs = Table.Group(ChangedType, "Year", {"Total Population per Year", each List.Sum([Population])}),
    // rename Year to AggsYear in the table Aggs
    RenameAggsYear = Table.RenameColumns(Aggs, {{"Year", "AggsYear"}}),

    // join source table with aggregations
    JoinWithAggs = Table.Join(ChangedType, "Year", RenameAggsYear, "AggsYear", JoinKind.Inner),
    // remove column AggsYear
    RemoveAggsYearColumn = Table.RemoveColumns(JoinWithAggs, "AggsYear")
in
    RemoveAggsYearColumn

And the result:

Capture.PNG

Next time, please provide also same sample data that we can import. It simplifies the work. Thank you Smiley Happy

View solution in original post

1 REPLY 1
Nolock
Resident Rockstar
Resident Rockstar

Hi @Anonymous,

you can group the table and then join it back with the original one.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTI0MgaSRgaG5kqxOjAhU4iQBULI2AAiZAkWcgJzDJA1goSMYUIWcCETA4TGWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Market = _t, Population = _t, Year = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Market", type text}, {"Population", Int64.Type}, {"Year", Int64.Type}}),

    // group by year and sum population
    Aggs = Table.Group(ChangedType, "Year", {"Total Population per Year", each List.Sum([Population])}),
    // rename Year to AggsYear in the table Aggs
    RenameAggsYear = Table.RenameColumns(Aggs, {{"Year", "AggsYear"}}),

    // join source table with aggregations
    JoinWithAggs = Table.Join(ChangedType, "Year", RenameAggsYear, "AggsYear", JoinKind.Inner),
    // remove column AggsYear
    RemoveAggsYearColumn = Table.RemoveColumns(JoinWithAggs, "AggsYear")
in
    RemoveAggsYearColumn

And the result:

Capture.PNG

Next time, please provide also same sample data that we can import. It simplifies the work. Thank you Smiley Happy

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.