Forum Discussion

derekmac's avatar
derekmac
Icon for Helper I rankHelper I
2 years ago
Solved

Move Values to the Left in Columns Side by Sides, removing Nulls

Hi All

 

I have a set of fields as below

 

Manager4_NameManager3_NameManager2_NameManager1_NameReportingToFullname
nullnullnullnullJane UptonNicole Peters
nullnullnullAaron JohnAaron JohnJan Orchard
nullnullAaron JohnPam ClementPam ClementElliot Paul
nullAdam JohnsonAnna KournikovaAdiam AhmadAdiam AhmadAzir Mohamed
Darren JonesAlan PatelPaul SmithAlan SmithAlan SmithAlexandra Inniss

 

I would like to change this to the following format in Power Query

 

Manager4_NameManager3_NameManager2_NameManager1_NameReportingToFullname
Jane UptonNicole Peters    
Aaron JohnAaron JohnJan Orchard   
Aaron JohnPam ClementPam ClementElliot Paul  
Adam JohnsonAnna KournikovaAdiam AhmadAdiam AhmadAzir Mohamed 
Darren JonesAlan PatelPaul SmithAlan SmithAlan Smith

Alexandra Inniss

 

So basically remove all Nulls and move the values across so line up the levels

All help appreciated!

  • AlienSx's avatar
    AlienSx
    2 years ago
    let
        Source = your_table,
        to_rows = List.Buffer(Table.ToRows(Source)),
        count = Table.ColumnCount(Source) - 1,
        txform = List.Transform(
            to_rows, 
            (x) => 
                [a = List.RemoveNulls(List.RemoveLastN(x, 1)),
                b = a & List.Repeat({null}, count - List.Count(a)) & {List.Last(x)}][b]
        ),
        z = Table.FromRows(txform, Table.ColumnNames(Source))
    in
        z

6 Replies

  • Hello, derekmac 

    let
        Source = your_table,
        to_rows = List.Buffer(Table.ToRows(Source)),
        count = Table.ColumnCount(Source),
        txform = List.Transform(
            to_rows, 
            (x) => 
                [a = List.RemoveNulls(x),
                b = a & List.Repeat({null}, count - List.Count(a))][b]
        ),
        z = Table.FromRows(txform, Table.ColumnNames(Source))
    in
        z
    • derekmac's avatar
      derekmac
      Icon for Helper I rankHelper I

      Thanks a Lot

       

      Manager4_NameManager3_NameManager2_NameManager1_NameReportingToFullnameID
      nullnullnullnullJane UptonNicole Peters1
      nullnullnullAaron JohnAaron JohnJan Orchard2
      nullnullAaron JohnPam ClementPam ClementElliot Paul3
      nullAdam JohnsonAnna KournikovaAdiam AhmadAdiam AhmadAzir Mohamed4
      Darren JonesAlan PatelPaul SmithAlan SmithAlan SmithAlexandra Inniss5

       

      I omitted the ID Column which I would like to stay where it is, is that possible?

       

      Manager4_NameManager3_NameManager2_NameManager1_NameReportingToFullnameID
      Jane UptonNicole Peters    1
      Aaron JohnAaron JohnJan Orchard   2
      Aaron JohnPam ClementPam ClementElliot Paul  3
      Adam JohnsonAnna KournikovaAdiam AhmadAdiam AhmadAzir Mohamed 4
      Darren JonesAlan PatelPaul SmithAlan SmithAlan Smith

      Alexandra Inniss

      5
      • AlienSx's avatar
        AlienSx
        Icon for Super User rankSuper User
        let
            Source = your_table,
            to_rows = List.Buffer(Table.ToRows(Source)),
            count = Table.ColumnCount(Source) - 1,
            txform = List.Transform(
                to_rows, 
                (x) => 
                    [a = List.RemoveNulls(List.RemoveLastN(x, 1)),
                    b = a & List.Repeat({null}, count - List.Count(a)) & {List.Last(x)}][b]
            ),
            z = Table.FromRows(txform, Table.ColumnNames(Source))
        in
            z
  • mlsx4's avatar
    mlsx4
    Icon for Memorable Member rankMemorable Member

    Hi derekmac 

     

    Add this step from your last step (remember to replace #"Headers promoted" by the name of your last step):

     

     

    = Table.ReverseRows(Table.FromRecords(List.Transform(List.Reverse(List.Transform(Table.ToRows(#"Headers promoted"),List.RemoveNulls)),each Record.FromList(_,List.FirstN(Table.ColumnNames(#"Headers promoted"),List.Count(_)))),null,MissingField.UseNull))