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

Try your skills in the Power BI Dataviz World Championship! Round one ends June 26. Join now

Reply
dhendus
Frequent Visitor

Condionally fill down multiple columns

I want to condionally fill down multiple columns (approximatively 200 columns) as below :
When the value's column is "E" => begin to fill down, and stop when the value "H" is reached.

 

Example of 2 columns input data  :

Column1Column2
AW
BX
CY
DZ
nullnull
nullE
nullnull
EF
nullnull
nullnull
nullnull
FG
GH
nullnull
nullnull
Hnull
nullnull

 

Desired output :

Column1Column2
AW
BX
CY
DZ
nullnull
nullE
nullE
EF
EF
EF
EF
FG
GH
Gnull
Gnull
Hnull
nullnull

 

Thanks

1 ACCEPTED SOLUTION
AlienSx
Super User
Super User

let
    Source = your_table,
    to_cols = Table.ToColumns(Source),
    g = (lst) => 
        [st = [E = true, H = false],
        gen = List.Generate(
            () => [i = 0, c = lst{0}, fd = c = "E"],
            (x) => x[i] < List.Count(lst),
            (x) => 
                [
                    i = x[i] + 1, 
                    c = if x[fd] then lst{i} ?? x[c] else lst{i}, 
                    fd = Record.FieldOrDefault(st, c ?? "", x[fd])
                ],
            (x) => x[c]
        )][gen],
    tra = List.Transform(to_cols, g),
    to_tbl = Table.FromColumns(tra)
in
    to_tbl

View solution in original post

5 REPLIES 5
dufoq3
Community Champion
Community Champion

Hi @dhendus, different approach here. Test speed and let me know:

 

Result:

dufoq3_0-1717158237666.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUQpXitWJVnICsiLALGcgKxLMcgGyosAsIANGu6IIuAIZbuhKUGg3IMMdzHIHsjywqvFAF/DEYponioAXkoyXUmwsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
    // You can probably delete this step.
    ReplaceBlankToNull = Table.TransformColumns(Source, {}, each if _ = "" then null else _),
    Transform = List.Accumulate(
    List.Buffer(Table.ColumnNames(ReplaceBlankToNull)),
    #table({"_Temp_"}, {{null}}),
    (state, current)=> Table.AddColumn(state, current, each
                [ a = Table.SelectColumns(ReplaceBlankToNull, current),
                  b = Table.Group(a, {current}, {{"All", each _, type table}}, GroupKind.Local, (w,u)=> Byte.From( (Record.Field(w, current) <> "E" and Record.Field(u, current) = "E") or (Record.Field(w, current) = "E" and Record.Field(u, current) = "H") ) ),
                  c = Table.AddColumn(b, "Filled", (x)=> if Record.Field(x, current) = "E" then Table.FillDown(x[All], {current}) else x[All], type table),
                  d = Table.Combine(c[Filled]),
                  e = Table.Column(d, current)
                ][e], type list)
),
    RemovedColumns = Table.RemoveColumns(Transform,{"_Temp_"}),
    ToTable = Table.FromColumns(Table.ToRows(RemovedColumns){0})
in
    ToTable

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

AlienSx
Super User
Super User

let
    Source = your_table,
    to_cols = Table.ToColumns(Source),
    g = (lst) => 
        [st = [E = true, H = false],
        gen = List.Generate(
            () => [i = 0, c = lst{0}, fd = c = "E"],
            (x) => x[i] < List.Count(lst),
            (x) => 
                [
                    i = x[i] + 1, 
                    c = if x[fd] then lst{i} ?? x[c] else lst{i}, 
                    fd = Record.FieldOrDefault(st, c ?? "", x[fd])
                ],
            (x) => x[c]
        )][gen],
    tra = List.Transform(to_cols, g),
    to_tbl = Table.FromColumns(tra)
in
    to_tbl

That's what I'm talking about, thank you.
Unfortunately, I have memory issues when loading data, in step "Table.FromColumns(tra)" (can not load my data, I've been waiting for an hour and yet not finishing) 
can you suggest something else for this step ?

try to buffer to_cols step: 

to_cols = List.Buffer(Table.ToColumns(Source))

Thanks, it dropped to 10 min 🙂

Helpful resources

Announcements
Fabric Data Days is here Carousel

Fabric Data Days 2026

Don't miss out on Data Days, June 15 through August 7. Learn Fabric, Power BI, SQL, AI and more.

May Power BI Update Carousel

Power BI Monthly Update - May 2026

Check out the May 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.