Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
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 |
1 | Commence | null | Commence |
2 | null | End | End |
3 | Commence | null | Commence |
4 | null | null | null |
5 | null | null | null |
6 | null | null | null |
7 | null | End | End |
8 | null | null | null |
9 | Commence | null | Commence |
10 | null | null | null |
11 | null | null | null |
12 | null | End | End |
13 | null | null | null |
14 | Commence | null | Commence |
15 | null | null | null |
Solved! Go to Solution.
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"
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
@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"
@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"
@Anonymousdid you try the above?
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"
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