Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Fill Up and Fill Down row values based on conditions

Hi everyone.

 

Seeking help in querying my table. 

 

My table currently looks like this

 

EmployeeDateTaggingCreditsBal
AAAnullStart100100
AAA10/1/2021Accrual8108
AAA10/27/2021Taken-4104
AAAnullEnd104104

 

I need the dates to be indicated in the Date column instead of Null. In such a way that if the tagging is "Start" I will obtain the date from the row BELOW it, and if the tagging is "End" I will obtain the date from the row ABOVE it. It will look like this:

 

EmployeeDateTaggingCreditsBal
AAA10/1/2021Start100100
AAA10/1/2021Accrual8108
AAA10/27/2021Taken-4104
AAA10/27/2021End104104

 

  • Hi Anonymous 

     

    Download example PBIX file

     

    This query will do it

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnR0VNJRyivNyQFSwSWJRSVA2tDAAErG6sCUGBroG+obGRgZAtmOyclFpYkgHRZgGQtUdUbmMIUhidmpeUBa1wQsY4KkDmqla14KVAqqIBYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Employee = _t, Date = _t, Tagging = _t, Credits = _t, Bal = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Employee", type text}, {"Tagging", type text}, {"Credits", Int64.Type}, {"Bal", Int64.Type}}),
        #"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type", {{"Date", type date}}, "en-US"),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type with Locale", "Index", 0, 1, Int64.Type),
        #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each if [Date] is null then if [Tagging] = "Start" then #"Changed Type with Locale"[Date]{[Index]+1} else #"Changed Type with Locale"[Date]{[Index]-1} else [Date]),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Date", "Index"}),
        #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "Date"}}),
        #"Reordered Columns" = Table.ReorderColumns(#"Renamed Columns",{"Employee", "Date", "Tagging", "Credits", "Bal"})
    in
        #"Reordered Columns"

     

     

    Regards

     

    Phil

2 Replies

  • Hi Anonymous 

     

    Download example PBIX file

     

    This query will do it

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnR0VNJRyivNyQFSwSWJRSVA2tDAAErG6sCUGBroG+obGRgZAtmOyclFpYkgHRZgGQtUdUbmMIUhidmpeUBa1wQsY4KkDmqla14KVAqqIBYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Employee = _t, Date = _t, Tagging = _t, Credits = _t, Bal = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Employee", type text}, {"Tagging", type text}, {"Credits", Int64.Type}, {"Bal", Int64.Type}}),
        #"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type", {{"Date", type date}}, "en-US"),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type with Locale", "Index", 0, 1, Int64.Type),
        #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each if [Date] is null then if [Tagging] = "Start" then #"Changed Type with Locale"[Date]{[Index]+1} else #"Changed Type with Locale"[Date]{[Index]-1} else [Date]),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Date", "Index"}),
        #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "Date"}}),
        #"Reordered Columns" = Table.ReorderColumns(#"Renamed Columns",{"Employee", "Date", "Tagging", "Credits", "Bal"})
    in
        #"Reordered Columns"

     

     

    Regards

     

    Phil

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks Phil!

       

      Can you elaborate further the syntax for the conditional column? Particularly when you mentioned a previous step (#"Changed Type with Locale"), and Date field while referencing the Index field.  Or if there's an online ref material which I can look up to about this. Thank you very much.