Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Find % of Total

Here is my data columns, i need to find the % for each individual, (so for each name total the amount then take individual values and divide by total) but I need the Name column and the Ordered2023 column..

 

i am trying to figure how to add a total column for each name but cant figure it out, any suggestions?

 

 

currently i have a second duplicate table, groupby name that gives me the total, i merge the total to original data table above, then do the division.... wondering if there is another more efficient way to do it without the need to add a table to get totals.

 

thanks

  • Hi,

    A solution with Group and Expand. 

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTJUitWBsIzALCcgyxTOsoCzLMEsZ7gOZ7gOEMtEKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Names = _t, Count = _t]),
    Type = Table.TransformColumnTypes(Source,{{"Count", type number}}),
    Group = Table.Group(Type, {"Names"}, {{"Sum", each List.Sum([Count])}, {"Count", each [Count]}}),
    Expand = Table.ExpandListColumn(Group, "Count"),
    Percentage = Table.AddColumn(Expand, "%", each [Count] / [Sum], Percentage.Type)
    in
    Percentage

    or with ExpandTableColumn

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTJUitWBsIzALCcgyxTOsoCzLMEsZ7gOZ7gOEMtEKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Names = _t, Count = _t]),
    Type = Table.TransformColumnTypes(Source,{{"Count", type number}}),
    Group = Table.Group(Type, {"Names"}, {{"Sum", each List.Sum([Count])}, {"Data", each _}}),
    Expand = Table.ExpandTableColumn(Group, "Data", {"Count"}, {"Count"}),
    Percentage = Table.AddColumn(Expand, "%", each [Count] / [Sum], Percentage.Type)
    in
    Percentage

    Stéphane 

2 Replies

  • edhans's avatar
    edhans
    Icon for Community Champion rankCommunity Champion

    You can do it in the same query by creating a 2nd table of the group by, but you will still need to merge.

     

    I would step back and ask why are you doing this in Power Query. Getting % of total is usually best done in the DAX side which is optimized for those types of operations. Power Query isn't. Even if you get it to work, and you definitely can, the larger the dataset the slower it becomes until Power Query won't be able to finish. 

    Power Query is horrible at table scans, and not great at analysis in general. It is best suited for transforming data so the Power BI model can best analyze it with DAX.

  • Hi,

    A solution with Group and Expand. 

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTJUitWBsIzALCcgyxTOsoCzLMEsZ7gOZ7gOEMtEKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Names = _t, Count = _t]),
    Type = Table.TransformColumnTypes(Source,{{"Count", type number}}),
    Group = Table.Group(Type, {"Names"}, {{"Sum", each List.Sum([Count])}, {"Count", each [Count]}}),
    Expand = Table.ExpandListColumn(Group, "Count"),
    Percentage = Table.AddColumn(Expand, "%", each [Count] / [Sum], Percentage.Type)
    in
    Percentage

    or with ExpandTableColumn

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTJUitWBsIzALCcgyxTOsoCzLMEsZ7gOZ7gOEMtEKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Names = _t, Count = _t]),
    Type = Table.TransformColumnTypes(Source,{{"Count", type number}}),
    Group = Table.Group(Type, {"Names"}, {{"Sum", each List.Sum([Count])}, {"Data", each _}}),
    Expand = Table.ExpandTableColumn(Group, "Data", {"Count"}, {"Count"}),
    Percentage = Table.AddColumn(Expand, "%", each [Count] / [Sum], Percentage.Type)
    in
    Percentage

    Stéphane