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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Tim123
New Member

Count number of times a value appears in multiple columns

I'm new to Power Query, so was hoping to get some help with the following:

 

Say I have a table "Snacks" that records which snack my children eat each day:

 

DateAliceBrandonCharlie
Jan-1AppleBananaPear
Jan-2AppleBananaPear
Jan-3PearApplePear
Jan-4AppleApple

Banana

Jan-5PearBananaBanana
Jan-6AppleBananaBanana

 

I would like to know how many times my children have eaten each type of snack:

 

 AliceBrandonCharlie
Apple420
Banana043
Pear203

 

Any suggestions on how to do this with Power Query?

 

Note that the list of snacks isn't fixed (e.g. Alice might decide to eat Grapes on Jan-7).

1 ACCEPTED SOLUTION
jgeddes
Super User
Super User

Try this...

Select 'Date' and unpivot other columns

Group Rows by Snack and Name, counting the rows.

Pivot the Name column

Sample code...

let
    Source = Table,
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Date"}, "Name", "Snack"),
    #"Grouped Rows" = Table.Group(#"Unpivoted Other Columns", {"Snack", "Name"}, {{"Count", each Table.RowCount(_), Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Name]), "Name", "Count", List.Sum)
in
    #"Pivoted Column"

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





View solution in original post

2 REPLIES 2
jgeddes
Super User
Super User

Try this...

Select 'Date' and unpivot other columns

Group Rows by Snack and Name, counting the rows.

Pivot the Name column

Sample code...

let
    Source = Table,
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(Source, {"Date"}, "Name", "Snack"),
    #"Grouped Rows" = Table.Group(#"Unpivoted Other Columns", {"Snack", "Name"}, {{"Count", each Table.RowCount(_), Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Name]), "Name", "Count", List.Sum)
in
    #"Pivoted Column"

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Exactly what I was looking for, thanks!

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors