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 August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Overwrite columns with logic

I have the following in my Power Query. I would like to overwrite the Enter/Exit column. 

The logic that I am looking for is for every "End" to follow with a "Commence" immediately.  

Say for Time interval 7 to 9, it would ideally be (7) End, (8) Commence, (9) null

 

Any idea how I can do this?

 

Time interval Enter               Exit          Enter/Exit
1CommencenullCommence
2nullEndEnd
3CommencenullCommence
4nullnullnull
5nullnullnull
6nullnullnull
7nullEndEnd
8nullnullnull
9CommencenullCommence
10nullnullnull
11nullnullnull
12nullEndEnd
13nullnullnull
14CommencenullCommence
15nullnullnull
2 ACCEPTED SOLUTIONS
Jimmy801
Community Champion
Community Champion

Hello @Anonymous 

 

add a new column like this. The variable ChangeType has to reference to your prior step.

try if [#"Exit          "]="End" then "End" else if ChangedType{[#"Time interval "= [#"Time interval "]-1]}[#"Exit          "]="End" then "Commence" else null otherwise null

 Here a complete example

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXLOz81NzUtOBTLzSnNykEVidaKVjBDirnkpUBIkYUxYrwlCHJkCSZniljLDLWWOyzEWuPVYEnanoQFu7YaGeORwBo6hMR5dJkS4CFf4xAIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Time interval " = _t, #"Enter               " = _t, #"Exit          " = _t, #"Enter/Exit" = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Time interval ", Int64.Type}, {"Enter               ", type text}, {"Exit          ", type text}, {"Enter/Exit", type text}}),
    #"Added Custom" = Table.AddColumn(ChangedType, "Custom", each try if [#"Exit          "]="End" then "End" else if ChangedType{[#"Time interval "= [#"Time interval "]-1]}[#"Exit          "]="End" then "Commence" else null otherwise null)
in
    #"Added Custom"

Jimmy801_0-1612264328407.png

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

 

View solution in original post

smpa01
Super User
Super User

@Anonymous  can you please try this

 

let
    Source = Web.BrowserContents("https://community.powerbi.com/t5/Power-Query/Overwrite-columns-with-logic/m-p/1637835#M50128"),
    #"Extracted Table From Html" = Html.Table(Source, {{"Column1", "TABLE:nth-child(5) > * > TR > :nth-child(1)"}, {"Column2", "TABLE:nth-child(5) > * > TR > :nth-child(2)"}, {"Column3", "TABLE:nth-child(5) > * > TR > :nth-child(3)"}, {"Column4", "TABLE:nth-child(5) > * > TR > :nth-child(4)"}}, [RowSelector="TABLE:nth-child(5) > * > TR"]),
    #"Promoted Headers" = Table.PromoteHeaders(#"Extracted Table From Html", [PromoteAllScalars=true]),
    T_1 = Table.TransformColumnTypes(#"Promoted Headers",{{"Time interval", Int64.Type}, {"Enter", type text}, {"Exit", type text}, {"Enter/Exit", type text}}),
    L= T_1 [#"Enter/Exit"],
    Terminate = List.Count(L),
    Loop = List.Generate(
                          ()=>
                              [i=0, currentItem =L{i}, nextItem=try L{i-1} otherwise null ],
                              each [i]<Terminate,
                              each [i=[i]+1, currentItem=L{[i]+1},nextItem=try L{i-1} otherwise null],
                              each [nextItem]
                              
    ),
    name = Table.ColumnNames(T_1),
    val = Table.ToColumns(T_1),
    newName=  List.Combine({name,{"newVal"}}),
    newT= Table.FromColumns(val&{Loop},newName),
    #"Added Custom" = Table.AddColumn(newT, "finalVal", each if ([#"Enter/Exit"]="null" and [newVal]="End") then "Commence" else if ([#"Enter/Exit"]="Commence" and [newVal]="null") then "null"else [#"Enter/Exit"]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"newVal"})
in
    #"Removed Columns"

 

 

Capture.PNG

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

3 REPLIES 3
smpa01
Super User
Super User

@Anonymous  can you please try this

 

let
    Source = Web.BrowserContents("https://community.powerbi.com/t5/Power-Query/Overwrite-columns-with-logic/m-p/1637835#M50128"),
    #"Extracted Table From Html" = Html.Table(Source, {{"Column1", "TABLE:nth-child(5) > * > TR > :nth-child(1)"}, {"Column2", "TABLE:nth-child(5) > * > TR > :nth-child(2)"}, {"Column3", "TABLE:nth-child(5) > * > TR > :nth-child(3)"}, {"Column4", "TABLE:nth-child(5) > * > TR > :nth-child(4)"}}, [RowSelector="TABLE:nth-child(5) > * > TR"]),
    #"Promoted Headers" = Table.PromoteHeaders(#"Extracted Table From Html", [PromoteAllScalars=true]),
    T_1 = Table.TransformColumnTypes(#"Promoted Headers",{{"Time interval", Int64.Type}, {"Enter", type text}, {"Exit", type text}, {"Enter/Exit", type text}}),
    L= T_1 [#"Enter/Exit"],
    Terminate = List.Count(L),
    Loop = List.Generate(
                          ()=>
                              [i=0, currentItem =L{i}, nextItem=try L{i-1} otherwise null ],
                              each [i]<Terminate,
                              each [i=[i]+1, currentItem=L{[i]+1},nextItem=try L{i-1} otherwise null],
                              each [nextItem]
                              
    ),
    name = Table.ColumnNames(T_1),
    val = Table.ToColumns(T_1),
    newName=  List.Combine({name,{"newVal"}}),
    newT= Table.FromColumns(val&{Loop},newName),
    #"Added Custom" = Table.AddColumn(newT, "finalVal", each if ([#"Enter/Exit"]="null" and [newVal]="End") then "Commence" else if ([#"Enter/Exit"]="Commence" and [newVal]="null") then "null"else [#"Enter/Exit"]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"newVal"})
in
    #"Removed Columns"

 

 

Capture.PNG

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

@Anonymousdid you try the above?

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
Jimmy801
Community Champion
Community Champion

Hello @Anonymous 

 

add a new column like this. The variable ChangeType has to reference to your prior step.

try if [#"Exit          "]="End" then "End" else if ChangedType{[#"Time interval "= [#"Time interval "]-1]}[#"Exit          "]="End" then "Commence" else null otherwise null

 Here a complete example

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXLOz81NzUtOBTLzSnNykEVidaKVjBDirnkpUBIkYUxYrwlCHJkCSZniljLDLWWOyzEWuPVYEnanoQFu7YaGeORwBo6hMR5dJkS4CFf4xAIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Time interval " = _t, #"Enter               " = _t, #"Exit          " = _t, #"Enter/Exit" = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Time interval ", Int64.Type}, {"Enter               ", type text}, {"Exit          ", type text}, {"Enter/Exit", type text}}),
    #"Added Custom" = Table.AddColumn(ChangedType, "Custom", each try if [#"Exit          "]="End" then "End" else if ChangedType{[#"Time interval "= [#"Time interval "]-1]}[#"Exit          "]="End" then "Commence" else null otherwise null)
in
    #"Added Custom"

Jimmy801_0-1612264328407.png

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

 

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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