Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

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
  • 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"

     

     

3 Replies

  • Jimmy801's avatar
    Jimmy801
    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"

    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

     

  • smpa01's avatar
    smpa01
    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"

     

     

    • smpa01's avatar
      smpa01
      Community Champion

      Anonymousdid you try the above?