Forum Discussion

marktvc's avatar
marktvc
Helper I
5 years ago
Solved

Add an "Active Record" column

Hi All,

I have a Table - RatePlan and 2 specifc Columns I'm focusing on:

 

UNIQLIN (Id from another table)

INSERTDATE (date)

 

The field UNIQLIN appears multiple times but with different dates in INSERTDATE.

 

At the Query level, I would like to add a "CURRENT_RECORD" Y/N field.

 

so it would like this

 

 

 

  • Hello marktvc 

     

    you can add a Group-function on your ID with 2 function. AllRows and MaxDate of your date-column.

    After that expand your grouped table and check whether your date-column is the same as the MaxDate.

    Here some code

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIw1TMw0jMyUIrVgQgYG+gZGCMLGBnoGcJVOAEFDC3QBYAqDMACsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [UNIQLIN = _t, INSERTDATE = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"UNIQLIN", type text}, {"INSERTDATE", type date}}, "de-DE"),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"UNIQLIN"}, {{"MaxDate", each List.Max([INSERTDATE]), type date}, {"AllRows", each _, type table [UNIQLIN=text, INSERTDATE=date]}}),
        #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"INSERTDATE"}, {"INSERTDATE"}),
        #"Added Custom" = Table.AddColumn(#"Expanded AllRows", "CURRENT_RECORD", each if [MaxDate]=[INSERTDATE] then "Y" else "N"),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"MaxDate"})
    in
        #"Removed Columns"

    transforms this

     

    into this

     

    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

5 Replies

  • Jimmy801's avatar
    Jimmy801
    Community Champion

    Hello marktvc 

     

    you can add a Group-function on your ID with 2 function. AllRows and MaxDate of your date-column.

    After that expand your grouped table and check whether your date-column is the same as the MaxDate.

    Here some code

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIw1TMw0jMyUIrVgQgYG+gZGCMLGBnoGcJVOAEFDC3QBYAqDMACsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [UNIQLIN = _t, INSERTDATE = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"UNIQLIN", type text}, {"INSERTDATE", type date}}, "de-DE"),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"UNIQLIN"}, {{"MaxDate", each List.Max([INSERTDATE]), type date}, {"AllRows", each _, type table [UNIQLIN=text, INSERTDATE=date]}}),
        #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"INSERTDATE"}, {"INSERTDATE"}),
        #"Added Custom" = Table.AddColumn(#"Expanded AllRows", "CURRENT_RECORD", each if [MaxDate]=[INSERTDATE] then "Y" else "N"),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"MaxDate"})
    in
        #"Removed Columns"

    transforms this

     

    into this

     

    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

    • marktvc's avatar
      marktvc
      Helper I

      Almost there,  I have a number of other columns which disappear in the "Group" step.  I would like to see these.

       

    • marktvc's avatar
      marktvc
      Helper I

      here is my revise code.  it isn't quite perfecet as it generates a duplicate to UniqLine (UniqLine.1).  

       

      Thanks Jimmy for pointing me in the right direction.  

       

      #"Grouped Rows" = Table.Group(dbo_CFRatePlan, {"UniqLine"}, {{"MaxDate", each List.Max([InsertedDate]), type date}, {"AllRows", each _, type table [UniqLine=text, InsertedDate=date, UniqCFRatePlan=text]}}),
      #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"UniqLine", "InsertedDate", "UniqCFRatePlan"}, {"UniqLine.1", "InsertedDate", "UniqCFRatePlan"}),
      #"Added Custom" = Table.AddColumn(#"Expanded AllRows", "CURRENT_RECORD", each if [MaxDate]=[InsertedDate] then "Y" else "N"),
      #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"MaxDate"}),
      #"Removed Columns1" = Table.RemoveColumns(#"Removed Columns",{"UniqLine.1"})
      in
      #"Removed Columns1"

      • marktvc's avatar
        marktvc
        Helper I

        Jimmy801 

         

        I'm getting this error on the reload of data

         

        let
        Source = CFRatePlan,
        #"Detected Type Mismatches" = let
        tableWithOnlyPrimitiveTypes = Table.SelectColumns(Source, Table.ColumnsOfType(Source, {type nullable number, type nullable text, type nullable logical, type nullable date, type nullable datetime, type nullable datetimezone, type nullable time, type nullable duration})),
        recordTypeFields = Type.RecordFields(Type.TableRow(Value.Type(tableWithOnlyPrimitiveTypes))),
        fieldNames = Record.FieldNames(recordTypeFields),
        fieldTypes = List.Transform(Record.ToList(recordTypeFields), each [Type]),
        pairs = List.Transform(List.Positions(fieldNames), (i) => {fieldNames{i}, (v) => if v = null or Value.Is(v, fieldTypes{i}) then v else error [Message = "The type of the value does not match the type of the column.", Detail = v], fieldTypes{i}})
        in
        Table.TransformColumns(Source, pairs),
        #"Added Index" = Table.AddIndexColumn(#"Detected Type Mismatches", "Row Number" ,1),
        #"Kept Errors" = Table.SelectRowsWithErrors(#"Added Index", {"UniqLine", "InsertedDate", "UniqCFRatePlan", "CURRENT_RECORD"}),
        #"Reordered Columns" = Table.ReorderColumns(#"Kept Errors", {"Row Number", "UniqLine", "InsertedDate", "UniqCFRatePlan", "CURRENT_RECORD"})
        in
        #"Reordered Columns"