(get list with) count of non-null values of each of multiple columns

ooptennoort

Board Regular
Joined
Mar 29, 2021
Messages
67
Office Version
  1. 365
Platform
  1. Windows
I have a data set with >100 cols, most of which contain only 1 value (useless). I need to remove these so I have cols left with >1 value.

So it seems to me I somehow need to List.Select all the List.NonNullCount >1 for each _column (which suggests using Table.ColumnNames) but I just can't figure out how to put these together followed of course by Table.SelectColumns with that list (the easy bit)

Any ideas (chatGPT provided absolute crap)?
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tcn = Table.ColumnNames(Source),
    cols = List.Accumulate(tcn, {},(s,c)=> if List.Count(List.RemoveNulls(Table.Column(Source,c))) > 1 then s & {c} else s), 
    Result = Table.SelectColumns(Source, cols)
in
    Result

Book1
ABCDEFGHIJKLMNOP
1Table1
2Col1Col2Col3Col4Col5Col6Col7Col8Col9Col10Col11Col12Col13Col14Col15
31310171RGI1
4515112LAH
519183FDQ2
671234OCB
76425EPM3
8161486KJN
9
10Query Output
11Col1Col2Col3Col10Col11Col12
12131017RGI
1351511LAH
141918FDQ
157123OCB
16642EPM
1716148KJN
18
Sheet1
 
Upvote 1
Solution
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tcn = Table.ColumnNames(Source),
    cols = List.Accumulate(tcn, {},(s,c)=> if List.Count(List.RemoveNulls(Table.Column(Source,c))) > 1 then s & {c} else s),
    Result = Table.SelectColumns(Source, cols)
in
    Result
@JGordon11: Nice!

Just to mention another function name by using your solution, an alternative by using List.NonNullCount() instead of List.Count() and List.RemoveNulls() combination.
Power Query:
cols = List.Accumulate(tcn, {},(s,c)=> if List.NonNullCount(Table.Column(Source,c)) > 1 then s & {c} else s),
 
Upvote 0
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    tcn = Table.ColumnNames(Source),
    cols = List.Accumulate(tcn, {},(s,c)=> if List.Count(List.RemoveNulls(Table.Column(Source,c))) > 1 then s & {c} else s),
    Result = Table.SelectColumns(Source, cols)
in
    Result

Book1
ABCDEFGHIJKLMNOP
1Table1
2Col1Col2Col3Col4Col5Col6Col7Col8Col9Col10Col11Col12Col13Col14Col15
31310171RGI1
4515112LAH
519183FDQ2
671234OCB
76425EPM3
8161486KJN
9
10Query Output
11Col1Col2Col3Col10Col11Col12
12131017RGI
1351511LAH
141918FDQ
157123OCB
16642EPM
1716148KJN
18
Sheet1
Thank you!!! Of course, iteration... that scary list.accumulate... one day I will be able to (re)produce it! Thanks again!!
 
Upvote 0

Forum statistics

Threads
1,223,936
Messages
6,175,499
Members
452,649
Latest member
mr_bhavesh

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top