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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
huynq
Frequent Visitor

How to count values of columns???

Hi everyone, I'm sorry for my bad English. I've tried to find the solution for this but didn't work so I post this message for your help.

I have this data:

 

A  B  C  D

a  b  c  d

a  b  a  a

c  a  d  o

 

What I'm looking for
     A  B  C  D
a   2  1  1  1

b   0  2  0  0

c   1  0  1  0

d   0  0  1  1

o   0  0  0  1

 

I would like to say thanks in advance. 

2 ACCEPTED SOLUTIONS
Jimmy801
Community Champion
Community Champion

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

View solution in original post

Jimmy801
Community Champion
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 
	Pivot

 

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

View solution in original post

4 REPLIES 4
Jimmy801
Community Champion
Community Champion

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

You are the life savior, by the way how can I replace null value by 0? 

Jimmy801
Community Champion
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 
	Pivot

 

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

@Jimmy801 Bingo, this is what I need. Thank you very much.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.