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

Try your skills in the Power BI Dataviz World Championship! Round one ends June 26. Join now

Reply
Dicken
Post Prodigy
Post Prodigy

Counting nulls


Hi, 
    counting null values,    i have; 

= let alist = 
{1,2,2,"a",null, null, 2,"a",3,2,null, "a"} 
in 
List.Count( alist) - List.NonNullCount( alist)


or 

= let alist = 
{1,2,2,"a",null, null, 2,"a",3,2,null, "a"} 
in 
List.Count( List.Select( alist, (x)=> 
 Value.Is(x, type null)) )



woud anyone like to suggest differerent / better way  ?  

2 ACCEPTED SOLUTIONS
Parchitect
Responsive Resident
Responsive Resident

Hi @Dicken,

For pure null values, I would keep your first approach. It is probably the cleanest and easiest to read:

let
    alist = {1,2,2,"a",null, null, 2,"a",3,2,null, "a"}
in
    List.Count(alist) - List.NonNullCount(alist)

 

List.NonNullCount is built to count non-null items, so total count minus non-null count gives the null count.

 

View solution in original post

slorin
Super User
Super User

List.Accumulate(alist, 0, (i,x) => i + Number.From(x=null))
List.Sum(List.Transform( alist, each Number.From(_=null)))

 

View solution in original post

13 REPLIES 13
slorin
Super User
Super User

You don't need List.Accumulate, just List.Transform

List.Transform( alist , each Number.From(_ = null))

 

I know, I just liked it. 

Dicken
Post Prodigy
Post Prodigy

a take on the accumulate version, so not to count but show where null;

= let alist = {"a",1,null, null,"a",3,"b"} 
, t = "a" 
in   List.Accumulate( alist, {}  , (s,c)=> 
  s & { Number.From( c = null ) } )
slorin
Super User
Super User

List.Accumulate(alist, 0, (i,x) => i + Number.From(x=null))
List.Sum(List.Transform( alist, each Number.From(_=null)))

 

penny dropped,  coerecing  true / false to 1 ,0   

really like that, only now i have to look into why it works as 
Number.From(_  = null) 
Number.From(_ ) = null)
are not the same 

AlienSx
Super User
Super User

let
    fx = (x, y) => if List.Count(x) = 0 then y else @ fx(List.Skip(x), y + Number.From(x{0} = null))
in
    fx({1, 2, 2, "a", null, null, 2, "a", 3, 2, null, "a"}, 0)
Dicken
Post Prodigy
Post Prodigy

just to add recgarding the count , remove nul,   i did say count nulls. 

My bad. I removed my answer as a solution. You could do this...

let 
    alist = 
    {1,2,2,"a",null, null, 2,"a",3,2,null, "a"} 
in 
    List.Count( alist ) - List.Count( List.RemoveNulls( alist ))




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

Proud to be a Super User!





slorin
Super User
Super User

Hi,

List.Count(List.PositionOf(alist, null, Occurrence.All))

Stéphane

Thanks to all responses,  always nice to see what I had not thought of.

jgeddes
Super User
Super User

This should work too...

let 
    alist = 
    {1,2,2,"a",null, null, 2,"a",3,2,null, "a"} 
in 
    List.Count( List.RemoveNulls( alist ))

 





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

Proud to be a Super User!





Parchitect
Responsive Resident
Responsive Resident

Hi @Dicken,

For pure null values, I would keep your first approach. It is probably the cleanest and easiest to read:

let
    alist = {1,2,2,"a",null, null, 2,"a",3,2,null, "a"}
in
    List.Count(alist) - List.NonNullCount(alist)

 

List.NonNullCount is built to count non-null items, so total count minus non-null count gives the null count.

 

Helpful resources

Announcements
Fabric Data Days is here Carousel

Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.