Forum Discussion

Quemi's avatar
Quemi
Frequent Visitor
4 years ago
Solved

Append and add rows with data if not found

Hi - I posted the other day and realized my target was unclear!

 

I want to append a row if not found, pull data over from Tables and add a flag column.

 

I have two tables, one with required values at a minimum for, and one with a Unique Variable + obtained values -- The Unique Variable MUST have all "fruits" for the Group + Time, if the unique variable does not contain the requried value(s), I would like to append a row with the Unique ID + Missing values along with a Flaged column.

 

Table 1 (required values)

GroupTimeFruit
High5Banana
High5Orange
Low3Watermelon
LowBanana
High4Banana

 

 Table 2 (Obtained Values):

UniqueIDGroupTimeFruit
A64High5Banana
B32High5Orange
B34Low3Watermelon
C64High4Banana

 

Table 3: Desired Output   -- Append with Unique ID + expected Group/Time/Fruit and Flag. Font color to show what I would like to append

UniqueIDGroupTimeFruitFlag
A64High5BananaTrue
A64High5OrangeMissing
B32High5Banana

Missing

B32High5Orange

True

B34Low3Watermelon

True

B34LowBananaMissing
C64High4BananaTrue
  • Hi Quemi ,

     

    How about this:

     

    Here the code in Power Query M that you can paste into the advanced editor (if you do not know, how to exactly do this, please check out this quick walkthrough)

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8shMz1DSUTIFYqfEPCBUitVBEfUvSsxLTwWL+uSXAwWMgTg8sSS1KDc1Jz8PWUIBqyEmSKKxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Group = _t, Time = _t, Fruit = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Group", type text}, {"Time", Int64.Type}, {"Fruit", type text}}),
        #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Group", "Time"}, Table16b, {"Group", "Time"}, "Table16b", JoinKind.LeftOuter),
        #"Expanded Table16b" = Table.ExpandTableColumn(#"Merged Queries", "Table16b", {"UniqueID", "Group", "Time", "Fruit"}, {"Table16b.UniqueID", "Table16b.Group", "Table16b.Time", "Table16b.Fruit"}),
        #"Added Custom" = Table.AddColumn(#"Expanded Table16b", "Flag", each if [Fruit] = [Table16b.Fruit] then "True" else "missing"),
        #"Reordered Columns" = Table.ReorderColumns(#"Added Custom",{"Table16b.UniqueID", "Group", "Time", "Fruit", "Flag", "Table16b.Group", "Table16b.Time", "Table16b.Fruit"}),
        #"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"Table16b.Group", "Table16b.Time", "Table16b.Fruit"})
    in
        #"Removed Columns"

     

    Let me know if this solves your issue ๐Ÿ™‚

     

    /Tom
    https://www.tackytech.blog/
    https://www.instagram.com/tackytechtom/

3 Replies

  • tackytechtom's avatar
    tackytechtom
    Most Valuable Professional

    Hi Quemi ,

     

    How about this:

     

    Here the code in Power Query M that you can paste into the advanced editor (if you do not know, how to exactly do this, please check out this quick walkthrough)

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8shMz1DSUTIFYqfEPCBUitVBEfUvSsxLTwWL+uSXAwWMgTg8sSS1KDc1Jz8PWUIBqyEmSKKxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Group = _t, Time = _t, Fruit = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Group", type text}, {"Time", Int64.Type}, {"Fruit", type text}}),
        #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Group", "Time"}, Table16b, {"Group", "Time"}, "Table16b", JoinKind.LeftOuter),
        #"Expanded Table16b" = Table.ExpandTableColumn(#"Merged Queries", "Table16b", {"UniqueID", "Group", "Time", "Fruit"}, {"Table16b.UniqueID", "Table16b.Group", "Table16b.Time", "Table16b.Fruit"}),
        #"Added Custom" = Table.AddColumn(#"Expanded Table16b", "Flag", each if [Fruit] = [Table16b.Fruit] then "True" else "missing"),
        #"Reordered Columns" = Table.ReorderColumns(#"Added Custom",{"Table16b.UniqueID", "Group", "Time", "Fruit", "Flag", "Table16b.Group", "Table16b.Time", "Table16b.Fruit"}),
        #"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"Table16b.Group", "Table16b.Time", "Table16b.Fruit"})
    in
        #"Removed Columns"

     

    Let me know if this solves your issue ๐Ÿ™‚

     

    /Tom
    https://www.tackytech.blog/
    https://www.instagram.com/tackytechtom/