Forum Discussion
How to count values of columns???
- 6 years ago
Hello huynq
this involves some unpivoting, grouping and pivoting. Check out this code example
let Source = #table ( { "A", "B", "C", "D" }, { { "a", "b", "c", "d" }, { "a", "b", "a", "a" }, { "c", "a", "d", "o" } } ), Unpivot = Table.UnpivotOtherColumns ( Source, { }, "Attribute", "Value" ), Group = Table.Group ( Unpivot, { "Attribute", "Value" }, { { "Count", each Table.RowCount ( _ ), type number } } ), Pivot = Table.Pivot ( Group, List.Distinct ( Group [ Attribute ] ), "Attribute", "Count", List.Sum ) in PivotCopy paste this code to the advanced editor in a new blank query to see how the solution works. If this solution fits your need, copy and past a part of it and implement it in your query, or I could create a custom function what makes it easier to apply if you are not used that much to power query.
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 - 6 years ago
Hello huynq
just use this code instead
let Source = #table ( { "A", "B", "C", "D" }, { { "a", "b", "c", "d" }, { "a", "b", "a", "a" }, { "c", "a", "d", "o" } } ), Unpivot = Table.UnpivotOtherColumns ( Source, { }, "Attribute", "Value" ), Group = Table.Group ( Unpivot, { "Attribute", "Value" }, { { "Count", each Table.RowCount ( _ ), type number } } ), Pivot = Table.Pivot ( Group, List.Distinct ( Group [ Attribute ] ), "Attribute", "Count", each if List.IsEmpty(_) then 0 else List.Sum (_) ) in PivotIf 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
Hello huynq
this involves some unpivoting, grouping and pivoting. Check out this code example
let
Source = #table
(
{ "A", "B", "C", "D" },
{ { "a", "b", "c", "d" }, { "a", "b", "a", "a" }, { "c", "a", "d", "o" } }
),
Unpivot = Table.UnpivotOtherColumns
(
Source,
{ },
"Attribute",
"Value"
),
Group = Table.Group
(
Unpivot,
{ "Attribute", "Value" },
{ { "Count", each Table.RowCount ( _ ), type number } }
),
Pivot = Table.Pivot
(
Group,
List.Distinct
(
Group [ Attribute ]
),
"Attribute",
"Count",
List.Sum
)
in
Pivot
Copy paste this code to the advanced editor in a new blank query to see how the solution works. If this solution fits your need, copy and past a part of it and implement it in your query, or I could create a custom function what makes it easier to apply if you are not used that much to power query.
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
- huynq6 years agoFrequent Visitor
You are the life savior, by the way how can I replace null value by 0?
- Jimmy8016 years ago
Community Champion
Hello huynq
just use this code instead
let Source = #table ( { "A", "B", "C", "D" }, { { "a", "b", "c", "d" }, { "a", "b", "a", "a" }, { "c", "a", "d", "o" } } ), Unpivot = Table.UnpivotOtherColumns ( Source, { }, "Attribute", "Value" ), Group = Table.Group ( Unpivot, { "Attribute", "Value" }, { { "Count", each Table.RowCount ( _ ), type number } } ), Pivot = Table.Pivot ( Group, List.Distinct ( Group [ Attribute ] ), "Attribute", "Count", each if List.IsEmpty(_) then 0 else List.Sum (_) ) in PivotIf 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