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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Singh_10
Helper I
Helper I

How to add 1 to the previous row value if current row is null in POWERQuery

Hi All,

 

I am looking for workaround for the below cae .

 

Current row is null and we add 1 to the previous row value and put it in current row

 

Singh_10_0-1720080423536.png

I want to fill these null by adding 1 to the previous row value in Power Query

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

Hi @Singh_10 ,

Based on your description, I created this data.

vlinhuizhmsft_1-1720089073084.png

 

Please follow these steps:

 

1. Add a custom column.

 

if [Column1] = null then "NULL" else Text.From([Column1])

 

vlinhuizhmsft_2-1720089279473.png

2.Grouped rows

 

vlinhuizhmsft_3-1720089539822.png

3.Expand the data.

 

vlinhuizhmsft_4-1720089585956.png

4.Choose column1 and fill down.

 

vlinhuizhmsft_5-1720089608920.png

 

5. Add a custom column.

 

if [Custom] = "NULL" then [Column1] + [Index] else Number.FromText([Custom])

 

vlinhuizhmsft_6-1720089670398.png

The end result is as shown in the image below, and you can remove the columns you don't need.

 

vlinhuizhmsft_7-1720089733060.png

 

The overall M code is as follows:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjRUitWJVjI2BlMIwtQUTcDMTCk2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [Column1] = null then "NULL" else Text.From([Column1])),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"Custom"}, {{"Data", each Table.AddIndexColumn(_, "Index", 1, 1, Int64.Type)}}, GroupKind.Local),
    #"Expanded Data" = Table.ExpandTableColumn(#"Grouped Rows", "Data", {"Column1", "Index"}, {"Column1", "Index"}),
    #"Filled Down" = Table.FillDown(#"Expanded Data",{"Column1"}),
    #"Added Custom1" = Table.AddColumn(#"Filled Down", "Custom2", each if [Custom] = "NULL" then [Column1] + [Index] else Number.FromText([Custom]))
in
    #"Added Custom1"

 

If you have any other questions please feel free to contact me.

 

Best Regards,
Zhu
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

View solution in original post

3 REPLIES 3
v-linhuizh-msft
Community Support
Community Support

Hi @Singh_10 ,

Based on your description, I created this data.

vlinhuizhmsft_1-1720089073084.png

 

Please follow these steps:

 

1. Add a custom column.

 

if [Column1] = null then "NULL" else Text.From([Column1])

 

vlinhuizhmsft_2-1720089279473.png

2.Grouped rows

 

vlinhuizhmsft_3-1720089539822.png

3.Expand the data.

 

vlinhuizhmsft_4-1720089585956.png

4.Choose column1 and fill down.

 

vlinhuizhmsft_5-1720089608920.png

 

5. Add a custom column.

 

if [Custom] = "NULL" then [Column1] + [Index] else Number.FromText([Custom])

 

vlinhuizhmsft_6-1720089670398.png

The end result is as shown in the image below, and you can remove the columns you don't need.

 

vlinhuizhmsft_7-1720089733060.png

 

The overall M code is as follows:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjRUitWJVjI2BlMIwtQUTcDMTCk2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [Column1] = null then "NULL" else Text.From([Column1])),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"Custom"}, {{"Data", each Table.AddIndexColumn(_, "Index", 1, 1, Int64.Type)}}, GroupKind.Local),
    #"Expanded Data" = Table.ExpandTableColumn(#"Grouped Rows", "Data", {"Column1", "Index"}, {"Column1", "Index"}),
    #"Filled Down" = Table.FillDown(#"Expanded Data",{"Column1"}),
    #"Added Custom1" = Table.AddColumn(#"Filled Down", "Custom2", each if [Custom] = "NULL" then [Column1] + [Index] else Number.FromText([Custom]))
in
    #"Added Custom1"

 

If you have any other questions please feel free to contact me.

 

Best Regards,
Zhu
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

collinsg
Super User
Super User

Good day @Singh_10 ,

An approach is to generate a new list for each column by iterating through each column and generating a value which is either the value in the original list or the previous value plus one. These new lists can then replace the original columns. Here is some code.

  1. The first thing to notice is the function fnFillNulls (which is towards the end of the code) - it takes a list (in this case a column) and generates a new list.
  2. fnFillNulls is called for the value_min column and the value_max column.
  3. A new table is constructed using the original table's headings as its headings but the two new lists as its columns.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjYwMjUyMlfSAbEsLAwMlWJ1oiFsYyOwqLGRsYk5VNTYyMTMDCJqamluARbNK83JAQqBKVrwTQzAACgEZBmCWBiqYgE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [value_min = _t, value_max = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"value_min", Int64.Type}, {"value_max", Int64.Type}}),

fillMins = fnFillNulls(#"Changed Type"[value_min]),
fillMaxs = fnFillNulls(#"Changed Type"[value_max]),
tblHeaders = Table.ColumnNames(#"Changed Type"),
tblColumns = {fillMins} & {fillMaxs},
combined = Table.FromColumns(tblColumns, tblHeaders),
#"Changed Type1" = Table.TransformColumnTypes(combined,{{"value_min", Int64.Type}, {"value_max", Int64.Type}}),

fnFillNulls = (col as list) as list =>
List.Generate( ()=>
[i=0, output=col{0}],
each [i] < List.Count(col),
each [i=[i]+1, output = if col{i}=null then [output]+1 else col{i}],
each [output]
)
in
#"Changed Type1"

The result is (I added rows after your data to show "return to normal" after a run of nulls).

collinsg_0-1720083724799.png

Hope this helps.

 

Hi @collinsg 

 

Thank you for the reply.

 

I need this on the basis of ID-- need to group by ID-- one ID have multiple rows on the basis of dates where some dates ahve null values and i need to fill them by adding 1 to previous value min or max on the bases of date or datetime value  and ID

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

AugPowerBI_Carousel

Power BI Monthly Update - August 2024

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

August Carousel

Fabric Community Update - August 2024

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