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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

Reply
QZ
Helper I
Helper I

(M language) How to create new column base on another column conditial value in same table in Query

i have original data like bellow:

QZ_0-1680242702725.png

 

How to transform to bellow in Power Query?
QZ_1-1680242769793.png

 

1 ACCEPTED SOLUTION
AlienSx
Super User
Super User

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtY31DdU0lHyTUzOyMxLVQCxDfUMlWJ1sEobAdlGuKWNgWxjVGkjDMONcElDDMcpDTEcRdoYzXA0u43RDMejG2K4sVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Process Date" = _t, Machine = _t, #"Electricity at 00:00:00" = _t]),
    groups = Table.Group(Source, {"Machine"}, {{"rows", each Table.Sort(_, "Process Date")}}),
    f = (lst as list) as list =>
        let 
            positions = List.Positions(lst),
            new_col =
                List.Accumulate(
                    positions,
                    {},
                    (s, c) => s & {try lst{c + 1} otherwise null}
                )
        in new_col,
    add_column = 
        Table.TransformColumns(
            groups, 
            {
                "rows",
                (x) =>
                    let 
                        next_day = f( x[#"Electricity at 00:00:00"] ),
                        new_tbl = Table.FromColumns(Table.ToColumns(x) & {next_day}, Table.ColumnNames(x) & {"Electricity at 00:00:00(Next Day)"})
                    in new_tbl
            }
        ),
    combine = Table.Sort(Table.Combine(add_column[rows]), {"Process Date"})

in
    combine

View solution in original post

3 REPLIES 3
AlienSx
Super User
Super User

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwMtY31DdU0lHyTUzOyMxLVQCxDfUMlWJ1sEobAdlGuKWNgWxjVGkjDMONcElDDMcpDTEcRdoYzXA0u43RDMejG2K4sVJsLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Process Date" = _t, Machine = _t, #"Electricity at 00:00:00" = _t]),
    groups = Table.Group(Source, {"Machine"}, {{"rows", each Table.Sort(_, "Process Date")}}),
    f = (lst as list) as list =>
        let 
            positions = List.Positions(lst),
            new_col =
                List.Accumulate(
                    positions,
                    {},
                    (s, c) => s & {try lst{c + 1} otherwise null}
                )
        in new_col,
    add_column = 
        Table.TransformColumns(
            groups, 
            {
                "rows",
                (x) =>
                    let 
                        next_day = f( x[#"Electricity at 00:00:00"] ),
                        new_tbl = Table.FromColumns(Table.ToColumns(x) & {next_day}, Table.ColumnNames(x) & {"Electricity at 00:00:00(Next Day)"})
                    in new_tbl
            }
        ),
    combine = Table.Sort(Table.Combine(add_column[rows]), {"Process Date"})

in
    combine

It works, thx!
Let me study the code first, anyway, thank you!

QZ
Helper I
Helper I

If you can shed some light, it would by much appreciated.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors