Forum Discussion
Group By 3 fields and Count another field based on Condition
Hi, i have a table with 4 fields and i want to Group By 3 of them and the 4th field has values of {"CM","PM","TOB"} and i want to Count each of them related to the Grouping. How can i apply this please ?
Hello
check out this approach. It uses Table.Group and a special function to count your specific items.
Use this variable to input your items to be counted
ItemsToCount = {"CM", "PM", "TOB"},
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSgWNnX6VYHVShAHKFQvydMMSg5icBmTAcgCkE1BmTB9OOJkGMfpAtsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}}), #"Grouped Rows" = Table.Group ( #"Changed Type", {"Column1", "Column2", "Column3"}, { { "Count", (tbl)=> let ItemsToCount = {"CM", "PM", "TOB"}, CountItems = Text.Combine(List.Transform(ItemsToCount, (trans)=> trans &": " & Text.From(List.Count(List.Select(tbl[Column4],each _ = trans)))), ", ") in CountItems } } ) in #"Grouped Rows"Copy paste this code to the advanced editor in a new blank query to see how the solution works.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
JimmyHi, Anonymous
Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.
Table:
You may create a measure as below.
Result = var cm = COALESCE( COUNTROWS( FILTER( 'Table', [WoType]="CM" ) ),0 ) var pm = COALESCE( COUNTROWS( FILTER( 'Table', [WoType]="PM" ) ),0 ) var tob = COALESCE( COUNTROWS( FILTER( 'Table', [WoType]="TOB" ) ),0 ) return "CM:"&cm&" PM:"&pm&" TOB:"&tobResult:
Best Regards
Allan
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
5 Replies
- Jimmy801
Community Champion
Hello
check out this approach. It uses Table.Group and a special function to count your specific items.
Use this variable to input your items to be counted
ItemsToCount = {"CM", "PM", "TOB"},
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSlTSgWNnX6VYHVShAHKFQvydMMSg5icBmTAcgCkE1BmTB9OOJkGMfpAtsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}}), #"Grouped Rows" = Table.Group ( #"Changed Type", {"Column1", "Column2", "Column3"}, { { "Count", (tbl)=> let ItemsToCount = {"CM", "PM", "TOB"}, CountItems = Text.Combine(List.Transform(ItemsToCount, (trans)=> trans &": " & Text.From(List.Count(List.Select(tbl[Column4],each _ = trans)))), ", ") in CountItems } } ) in #"Grouped Rows"Copy paste this code to the advanced editor in a new blank query to see how the solution works.
If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too
Have fun
Jimmy - shaowu459
Resolver II
Hi Anonymous , could you post some sample data?
- AnonymousNot applicable
Hi shawou459 , thank you for your reply, here is a screenshoot for my power bi desktop interface.
- CNENFRNL
Community Champion
Anonymous , DAX is born for such a statistical job. It's only a matter of several drag-n-drop into a matrix viz. It takes 10 seconds only; then you can sit back and relax...
- v-alq-msft
Community Support
Hi, Anonymous
Based on your description, I created data to reproduce your scenario. The pbix file is attached in the end.
Table:
You may create a measure as below.
Result = var cm = COALESCE( COUNTROWS( FILTER( 'Table', [WoType]="CM" ) ),0 ) var pm = COALESCE( COUNTROWS( FILTER( 'Table', [WoType]="PM" ) ),0 ) var tob = COALESCE( COUNTROWS( FILTER( 'Table', [WoType]="TOB" ) ),0 ) return "CM:"&cm&" PM:"&pm&" TOB:"&tobResult:
Best Regards
Allan
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.