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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

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
Community Champion
Community Champion

@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: Live Sound
• Beautiful News: Women in Parliament, Energy Mix, Shrinking Armies
• Visual Capitalist: Working Hrs
• Others: Easing Graph, Animated Calendar
MayViz Submissions
• Week 1: View
• Week 2: View
• Week 3: View
• Week 4: View
========================

View solution in original post

3 REPLIES 3
smpa01
Community Champion
Community Champion

@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: Live Sound
• Beautiful News: Women in Parliament, Energy Mix, Shrinking Armies
• Visual Capitalist: Working Hrs
• Others: Easing Graph, Animated Calendar
MayViz Submissions
• Week 1: View
• Week 2: View
• Week 3: View
• Week 4: View
========================
smpa01
Community Champion
Community Champion

@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: Live Sound
• Beautiful News: Women in Parliament, Energy Mix, Shrinking Armies
• Visual Capitalist: Working Hrs
• Others: Easing Graph, Animated Calendar
MayViz Submissions
• Week 1: View
• Week 2: View
• Week 3: View
• Week 4: View
========================
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
Fabric Data Days is here Carousel

Data Days 2026

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

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.