Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
derekmac
Helper I
Helper I

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!

1 ACCEPTED SOLUTION

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

View solution in original post

6 REPLIES 6
mlsx4
Super User
Super User

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

 

 

 

Thank you

AlienSx
Super User
Super User

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

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

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

Thanks a lot

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors