Search code examples
excelpowerbipowerquerypowerbi-desktopm

How to fill up / fill down in power query , grouped by another column


Suppose I have this table

Customer No Ending Balance
C001 NULL
C001 100
C001 NULL
C002 NULL
C002 NULL
C002 499
C002 NULL
C002 599
C002 NULL

And I want this result

Customer No Ending Balance
C001 100
C001 100
C001 100
C002 499
C002 499
C002 499
C002 499
C002 599
C002 599

I was think to use fill down and fill up, after the fill down, I get this

Customer No Ending Balance
C001 100
C001 100
C001 100
C002 100 (Wrong)
C002 100 (Wrong)
C002 499
C002 499
C002 599
C002 599

Is there a way to use fill down / fill up based on a group column?


Solution

  • Here you go.

    enter image description here

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcjYwMFRQ0lHyC/XxUYrVQQgYGhig8JEVGBEnYGJpiV+BKVYFsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Customer No" = _t, #"Ending Balance" = _t]),
        #"Replaced Value" = Table.ReplaceValue(Source,"NULL",null,Replacer.ReplaceValue,{"Ending Balance"}),
        #"Changed Type" = Table.TransformColumnTypes(#"Replaced Value",{{"Customer No", type text}, {"Ending Balance", type text}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Customer No"}, {{"All", each _, type table [Customer No=nullable text, Ending Balance=nullable text]}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.FillUp( Table.FillDown([All], {"Ending Balance"}), {"Ending Balance"})),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"All"}),
        #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Ending Balance"}, {"Ending Balance"})
    in
        #"Expanded Custom"
    

    Steps

    Group your table as follows:

    enter image description here

    Add a custom column as follows:

    enter image description here

    Expand and remove redundant columns and voila.