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
Anonymous
Not applicable

TRICKY - Move cell up in certain columns

How would you do the following in Power Query in Power BI. See start and end results in the Image below....

 

In a nutshall i need to shift the cell up in the last 3 columns and replace the null values with the real data which is sitting at the botttom..

 

Reciepts.JPG

1 ACCEPTED SOLUTION

There isn't a nice button in the ribbon to do this, so I go into the Advanced Editor to write the step needed.

 

Here's what the M code for that particular step look like (my StackOverflow answer):

Table.FromColumns(
    List.Transform(
        Table.ToColumns(#"Prev Step"),
        List.RemoveNulls
    ),
    Table.ColumnNames(#"Prev Step")
)

 

Now since you had "Null" instead of null in the file you shared, I had to add a step to replace those before doing the above-mentioned step.

 

Here's the whole query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfLKz8gDUn6lOTloVKxOtJIRSEVibmoxbiXGQE5wYi5uBSZATkgmHgWmYEvwWGEG5HjjM8EcyPFJLErErQJVGOStcF8gYWCoD0RGBkaGWFSB3BUMU2WKSxXIbtdwqCoTXKosDEGe8IuEqrPAqQ6uylgfiHC5DOQBZ0cfqGnmuNShKoNZGgsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Name = _t, Profit = _t, State = _t, Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Name", type text}, {"Profit", type text}, {"State", type text}, {"Date", type text}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type","Null",null,Replacer.ReplaceValue,{"ID", "Name", "Profit", "State", "Date"}),
    RemoveNulls = Table.FromColumns(List.Transform(Table.ToColumns(#"Replaced Value"), List.RemoveNulls), Table.ColumnNames(#"Replaced Value"))
in
    RemoveNulls

View solution in original post

8 REPLIES 8
Syndicate_Admin
Administrator
Administrator

Hello. Excuse me I have a similar case, but what I need is to move up the data in column2, so that they are confronted with the data in column1 in the image that I attach, how is it done?Col_b vs col_a.png

Kumail
Post Prodigy
Post Prodigy

Hello @Anonymous 

 

You unpivot the table and remove null from the columns. 

 

If you could send a sample .pbix that demonstrates what you are looking to get. It would really help to provide you with a quick solution.


You can send the sample .pbix file by adding it to your drive or dropbox and add the link here. 


Regards
Kumail Raza

If this answers your question, mark it as solution.

Kudos are appreciated!!

 

There's is a .pbix link in this thread.

 

Here it is again:
https://www.dropbox.com/s/f10sso718xq6zt3/SampleFile.pbix?dl=0

AlexisOlson
Super User
Super User

Here's a very similar question I answered previously:

Move up values when null Power Query

 

The basic approach is to turn the table into a list of columns, remove the nulls from each, and then put it back together.

Anonymous
Not applicable

Hi Alexis - firstly thanks, how do you turn the table unto a list of column and can you kindly detail the steps as i found that solution but could not complete the execrise sucessfully to get the end result.

There isn't a nice button in the ribbon to do this, so I go into the Advanced Editor to write the step needed.

 

Here's what the M code for that particular step look like (my StackOverflow answer):

Table.FromColumns(
    List.Transform(
        Table.ToColumns(#"Prev Step"),
        List.RemoveNulls
    ),
    Table.ColumnNames(#"Prev Step")
)

 

Now since you had "Null" instead of null in the file you shared, I had to add a step to replace those before doing the above-mentioned step.

 

Here's the whole query:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfLKz8gDUn6lOTloVKxOtJIRSEVibmoxbiXGQE5wYi5uBSZATkgmHgWmYEvwWGEG5HjjM8EcyPFJLErErQJVGOStcF8gYWCoD0RGBkaGWFSB3BUMU2WKSxXIbtdwqCoTXKosDEGe8IuEqrPAqQ6uylgfiHC5DOQBZ0cfqGnmuNShKoNZGgsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Name = _t, Profit = _t, State = _t, Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Name", type text}, {"Profit", type text}, {"State", type text}, {"Date", type text}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type","Null",null,Replacer.ReplaceValue,{"ID", "Name", "Profit", "State", "Date"}),
    RemoveNulls = Table.FromColumns(List.Transform(Table.ToColumns(#"Replaced Value"), List.RemoveNulls), Table.ColumnNames(#"Replaced Value"))
in
    RemoveNulls

If you post your data as a table I can copy and paste, I can demonstrate more easily.

Anonymous
Not applicable

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.