Forum Discussion

Dicken's avatar
Dicken
Icon for Post Prodigy rankPost Prodigy
2 months ago
Solved

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  ?  

  • 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.

     

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

     

13 Replies

  • 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.

     

  • This should work too...

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

     

  • Hi,

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

    Stéphane

    • Dicken's avatar
      Dicken
      Icon for Post Prodigy rankPost Prodigy

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

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

     

    • Dicken's avatar
      Dicken
      Icon for Post Prodigy rankPost Prodigy

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

    • Dicken's avatar
      Dicken
      Icon for Post Prodigy rankPost Prodigy

      penny dropped,  coerecing  true / false to 1 ,0   

  • Dicken's avatar
    Dicken
    Icon for Post Prodigy rankPost 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 ) } )
  • You don't need List.Accumulate, just List.Transform

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

     

  • Dicken's avatar
    Dicken
    Icon for Post Prodigy rankPost Prodigy

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

    • jgeddes's avatar
      jgeddes
      Icon for Super User rankSuper User

      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 ))
  • 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)