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
CharC
Frequent Visitor

Count non-null rows in a group

Hi, I'm not very good at using the group by function. I want to add a column to my table to flag how many non-null rows each group has, example below. 

 

Original table

NameItem
Jamesapple
Sarah 
Charlesorange
Charlesgraphs

 

Desired output:

 

NameItemCount item
Jamesapple1
Sarah 0
Charlesorange2
Charlesgraphs2
1 ACCEPTED SOLUTION
BA_Pete
Super User
Super User

Hi @CharC ,

 

To do this in Power Query, try the following example code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8krMTS1W0lFKLCjISVWK1YlWCk4sSswAiiiAec4ZiUU5YBX5RYl56alogulFiQUZxUqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Item = _t]),
    repBlankNull = Table.ReplaceValue(Source," ",null,Replacer.ReplaceValue,{"Item"}),
    groupName = Table.Group(repBlankNull, {"Name"}, {{"data", each _, type table [Name=nullable text, Item=nullable text]}}),
    addNonNullCount = Table.AddColumn(groupName, "NonNullCount", each List.NonNullCount([data][Item])),
    expandNestedItem = Table.ExpandTableColumn(addNonNullCount, "data", {"Item"}, {"Item"})
in
    expandNestedItem

 

Summary:

repBlankNull (not essential): Ensures cells are actually null, not just blank ("")

groupName: Group table on [Name] column, using 'All Rows' aggregator

addNonNullCount: Add new column to count non-null values in nested tables

expandNestedItem: Expand the [Item] column back out from the nested tables.

 

Output:

BA_Pete_0-1756373260433.png

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

3 REPLIES 3
BA_Pete
Super User
Super User

Hi @CharC ,

 

To do this in Power Query, try the following example code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8krMTS1W0lFKLCjISVWK1YlWCk4sSswAiiiAec4ZiUU5YBX5RYl56alogulFiQUZxUqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Item = _t]),
    repBlankNull = Table.ReplaceValue(Source," ",null,Replacer.ReplaceValue,{"Item"}),
    groupName = Table.Group(repBlankNull, {"Name"}, {{"data", each _, type table [Name=nullable text, Item=nullable text]}}),
    addNonNullCount = Table.AddColumn(groupName, "NonNullCount", each List.NonNullCount([data][Item])),
    expandNestedItem = Table.ExpandTableColumn(addNonNullCount, "data", {"Item"}, {"Item"})
in
    expandNestedItem

 

Summary:

repBlankNull (not essential): Ensures cells are actually null, not just blank ("")

groupName: Group table on [Name] column, using 'All Rows' aggregator

addNonNullCount: Add new column to count non-null values in nested tables

expandNestedItem: Expand the [Item] column back out from the nested tables.

 

Output:

BA_Pete_0-1756373260433.png

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




CharC
Frequent Visitor

Thank you. In my original code I missed the table reference ([data]) and couldn't get the results right. 

([data][Item])

 

rohit1991
Super User
Super User

Hi @CharC 

Could you please try below Calculated Column: 

Count item = 
VAR CurrentName = 'Sample Data'[Name]
RETURN
COUNTROWS (
    FILTER (
        ALL ( 'Sample Data' ),
        'Sample Data'[Name] = CurrentName
            && NOT (
                ISBLANK ( 'Sample Data'[Item] )
                || TRIM ( 'Sample Data'[Item] ) = ""       
            )
    )
)

 

image.png

 


Did it work? ✔ Give a Kudo • Mark as Solution – help others too!

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