Forum Discussion
Anonymous
3 years agoNot applicable
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 c...
- 3 years ago
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
Percentageor 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
PercentageStéphane
edhans
Community Champion
3 years agoYou 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.