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
vineshparekh
Helper I
Helper I

Move adjacent column data to the left if the current column is nil

Hi,

 

I imported data from a folder where there are few PDFs and all the PDFs have tables inside. However, when I import that into PQ, the data is not consistent. Some are pasted in Col2, some are in Col3 etc..

 

I wanted to ask if I can put a formula using MCode that move all the adjacent cells data to left if the curent column is nil.

 

For example,

Col2               Col3                          Col4

null                 07-Aug-23               112281

null                 22-Sep-23                232281

21-Sep-23      202254                     null 

 

Result Required:

Col2               Col3                          Col4

07-Aug-23     112281                     null 

22-Sep-23      232281                     null

21-Sep-23      202254                     null 

1 ACCEPTED SOLUTION
v-xinruzhu-msft
Community Support
Community Support

Hi  @vineshparekh 

You can put the of the following code to advanced editor 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUtJRMjIwMtY30TcHMg0NjYwsDJVidRASlvpGRiC2MVwGJmwIUWJkagJkKMXGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t]),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(Source, {}, "Attribute", "Value"),
    #"Grouped Rows" = Table.Group(#"Unpivoted Columns", {"Attribute"}, {{"Count", each _, type table [Attribute=text, Value=text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Count],"Index",1,1)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Count"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Value", "Index"}, {"Value", "Index"}),
    #"Split Column by Position" = Table.SplitColumn(#"Expanded Custom", "Attribute", Splitter.SplitTextByPositions({0, 6}, false), {"Attribute.1", "Attribute.2"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Position",{{"Attribute.1", type text}, {"Attribute.2", Int64.Type}}),
    #"Added Custom2" = Table.AddColumn(#"Changed Type", "Custom", each if [Value]="" then "" else if Text.Contains([Value],"/") then "Date" else "Number"),
    #"Added Custom1" = Table.AddColumn(#"Added Custom2", "Custom1", each Table.SelectRows(#"Added Custom2",(x)=>x[Attribute.2]=[Attribute.2]+1 and x[Index]=[Index])),
    #"Expanded Custom1" = Table.ExpandTableColumn(#"Added Custom1", "Custom1", {"Value", "Custom"}, {"Value.1", "Custom.1"}),
    #"Added Custom3" = Table.AddColumn(#"Expanded Custom1", "Custom.2", each if [Value]="" then [Value.1] else if Table.RowCount(    Table.SelectRows(#"Expanded Custom1",(x)=> x[Attribute.2]=[Attribute.2] and x[Index]<[Index] and x[Custom.1]=[Custom]))>0 then [Value] else [Value.1]),
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Added Custom3", {{"Attribute.2", type text}}, "en-US"),{"Attribute.1", "Attribute.2"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged"),
    #"Removed Columns1" = Table.RemoveColumns(#"Merged Columns",{"Value", "Custom", "Value.1", "Custom.1"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns1", List.Distinct(#"Removed Columns1"[Merged]), "Merged", "Custom.2"),
    #"Removed Columns2" = Table.RemoveColumns(#"Pivoted Column",{"Index"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns2",{{"Column1", type date}, {"Column2", Int64.Type}})
in
    #"Changed Type1"

Output

vxinruzhumsft_0-1695621369143.png

Best Regards!

Yolo Zhu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Best Regards!

Yolo Zhu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-xinruzhu-msft
Community Support
Community Support

Hi  @vineshparekh 

You can put the of the following code to advanced editor 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUtJRMjIwMtY30TcHMg0NjYwsDJVidRASlvpGRiC2MVwGJmwIUWJkagJkKMXGAgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t]),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(Source, {}, "Attribute", "Value"),
    #"Grouped Rows" = Table.Group(#"Unpivoted Columns", {"Attribute"}, {{"Count", each _, type table [Attribute=text, Value=text]}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Count],"Index",1,1)),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Count"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Value", "Index"}, {"Value", "Index"}),
    #"Split Column by Position" = Table.SplitColumn(#"Expanded Custom", "Attribute", Splitter.SplitTextByPositions({0, 6}, false), {"Attribute.1", "Attribute.2"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Position",{{"Attribute.1", type text}, {"Attribute.2", Int64.Type}}),
    #"Added Custom2" = Table.AddColumn(#"Changed Type", "Custom", each if [Value]="" then "" else if Text.Contains([Value],"/") then "Date" else "Number"),
    #"Added Custom1" = Table.AddColumn(#"Added Custom2", "Custom1", each Table.SelectRows(#"Added Custom2",(x)=>x[Attribute.2]=[Attribute.2]+1 and x[Index]=[Index])),
    #"Expanded Custom1" = Table.ExpandTableColumn(#"Added Custom1", "Custom1", {"Value", "Custom"}, {"Value.1", "Custom.1"}),
    #"Added Custom3" = Table.AddColumn(#"Expanded Custom1", "Custom.2", each if [Value]="" then [Value.1] else if Table.RowCount(    Table.SelectRows(#"Expanded Custom1",(x)=> x[Attribute.2]=[Attribute.2] and x[Index]<[Index] and x[Custom.1]=[Custom]))>0 then [Value] else [Value.1]),
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Added Custom3", {{"Attribute.2", type text}}, "en-US"),{"Attribute.1", "Attribute.2"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged"),
    #"Removed Columns1" = Table.RemoveColumns(#"Merged Columns",{"Value", "Custom", "Value.1", "Custom.1"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns1", List.Distinct(#"Removed Columns1"[Merged]), "Merged", "Custom.2"),
    #"Removed Columns2" = Table.RemoveColumns(#"Pivoted Column",{"Index"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns2",{{"Column1", type date}, {"Column2", Int64.Type}})
in
    #"Changed Type1"

Output

vxinruzhumsft_0-1695621369143.png

Best Regards!

Yolo Zhu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Best Regards!

Yolo Zhu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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