Power Query: Remove null value in column, but keep other column

mduntley

Board Regular
Joined
May 23, 2015
Messages
139
Office Version
  1. 365
Platform
  1. Windows
Trying to find a way to remove all of the null column in each of the column, but not delete the other value in column 2

Power Query:
= #table({"Column 1", "Column 2"}, {{"439","772"},{"3241",null},{"1552","1143"},{"1308",null},{"3562","3242"},{null,"2494"},{"2043","1527"},{"1749","2162"},{"3472","1646"}})

Starting Table:
Book3
AB
1Column 1Column 2
2439772
33241
415521143
51308
635623242
72494
820431527
917492162
1034721646
Sheet2


Table I want to get

Book3
AB
1Column 1Column 2
2439772
332411143
415523242
513082494
635621527
720432162
817491646
93472
Sheet3
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I will admit this is kind of lame, but it works. First Query: Column1
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ChangedType = Table.TransformColumnTypes(Source,{{"Column 1", type text}, {"Column 2", type text}}),
    Column1 = Table.FromList( ChangedType[Column 1] ),
    FilteredColumn1 = Table.SelectRows(Column1, each ([Column1] <> "")),
    AddedColumn1Index = Table.AddIndexColumn(FilteredColumn1, "Index", 1, 1, Int64.Type)
in
    AddedColumn1Index
Second Query: Column2:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    ChangedType = Table.TransformColumnTypes(Source,{{"Column 1", type text}, {"Column 2", type text}}),
    Column2 = Table.FromList( ChangedType[Column 2] ),
    RenamedColumn2 = Table.RenameColumns(Column2,{{"Column1", "Column2"}}),
    FilteredColumn2 = Table.SelectRows(RenamedColumn2, each ([Column2] <> "")),
    AddedColumn2Index = Table.AddIndexColumn(FilteredColumn2, "Index", 1, 1, Int64.Type)
in
    AddedColumn2Index
Merge the two queries - here done as a New Query: Merge1:
Power Query:
let
    Source = Table.NestedJoin(Column1, {"Index"}, Column2, {"Index"}, "Column2", JoinKind.LeftOuter),
    ExpandedColumn2 = Table.ExpandTableColumn(Source, "Column2", {"Column2"}, {"Column2"}),
    RemovedOtherColumns = Table.SelectColumns(ExpandedColumn2,{"Column1", "Column2"}),
    ChangedType = Table.TransformColumnTypes(RemovedOtherColumns,{{"Column1", Int64.Type}, {"Column2", Int64.Type}})
in
    ChangedType
I manually changed the last {"Column2"} in the ExpandedColumn2 line from Column2.1.
Merge1 Table:
Book2
DE
1Column1Column2
2439772
332411143
415523242
513082494
635621527
720432162
817491646
93472
Sheet1

I have no doubt there are better solutions!
 
Upvote 0
Power Query:
let
    Source = #table({"Column 1", "Column 2"}, {{"439","772"},{"3241",null},{"1552","1143"},{"1308",null},{"3562","3242"},{null,"2494"},{"2043","1527"},{"1749","2162"},{"3472","1646"}}),
    lst = Table.ToColumns(Source),
    lst1 = List.Transform(lst, each List.RemoveNulls(_)),
    max = List.Accumulate(lst1, 0, (s,c)=> let lc = List.Count(c) in if lc>s then lc else s),
    lst2 = List.Transform(lst1, each _ & List.Repeat({null}, max - List.Count(_))),
    Result = Table.FromColumns(lst2, Table.ColumnNames(Source))
in
    Result
 
Upvote 0
Solution
Power Query:
let
    Source = #table({"Column 1", "Column 2"}, {{"439","772"},{"3241",null},{"1552","1143"},{"1308",null},{"3562","3242"},{null,"2494"},{"2043","1527"},{"1749","2162"},{"3472","1646"}}),
    lst = Table.ToColumns(Source),
    lst1 = List.Transform(lst, each List.RemoveNulls(_)),
    max = List.Accumulate(lst1, 0, (s,c)=> let lc = List.Count(c) in if lc>s then lc else s),
    lst2 = List.Transform(lst1, each _ & List.Repeat({null}, max - List.Count(_))),
    Result = Table.FromColumns(lst2, Table.ColumnNames(Source))
in
    Result
SWEET!
 
Upvote 0

Forum statistics

Threads
1,223,978
Messages
6,175,754
Members
452,667
Latest member
vanessavalentino83

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