-
Notifications
You must be signed in to change notification settings - Fork 76
/
Table.RemoveBlankColumns.pq
59 lines (49 loc) · 2.13 KB
/
Table.RemoveBlankColumns.pq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
// Allows to remove blank (empty) columns from a table like "Remove Empty Rows" UI option.
// Optional parameter for empty strings ("") removing (also as nulls)
// Usage:
Table.RemoveBlankColumns = Load("Table.RemoveBlankColumns"),
// remove column with nulls AND empty strings:
Table.RemoveBlankColumns(Table.FromRecords({[A = null, B = 1],[A = null, B = 2],[A = "", B = 3]}), true)
// Result: Table.FromRecords({[B = 1],[B = 2],[B = 3]})
// remove column with nulls only:
Table.RemoveBlankColumns(Table.FromRecords({[A = null, B = 1],[A = null, B = 2],[A = "", B = 3]}))
// Result: Table.FromRecords({[A = null, B = 1],[A = null, B = 2],[A = "", B = 3]})
*/
(
tab as table, // table to clean
optional EmptStr as nullable logical // flag to remove columns which contains nulls OR empty strings - only
) as table =>
let
ToRemove = {null} & (if EmptStr = true then {""} else {}), // list of "blank" values
tabDemoted = Table.DemoteHeaders(tab),
tabTransposed = Table.Transpose(tabDemoted),
RowsRemoved = Table.SelectRows(
tabTransposed,
each not List.IsEmpty(
List.RemoveMatchingItems(
Record.FieldValues(Record.RemoveFields(_, "Column1")), // after demote+transpose first column is allways with columns headers
ToRemove)
)
),
tabTransposedAgain = Table.Transpose(RowsRemoved)
in
Table.PromoteHeaders(tabTransposedAgain)
/*
// Alternative version (performance didn't checked between versions):
let
ToRemove = {null} & (if EmptStr = true then {""} else {}) // list of "blank" values
in
List.Accumulate(
Table.ColumnNames(tab),
tab,
(state, current)=>
if List.IsEmpty(
List.RemoveMatchingItems(
Table.Column(state, current),
ToRemove)
)
then Table.RemoveColumns(state, current)
else state
)
*/