Forum Discussion

O-K's avatar
O-K
Frequent Visitor
2 years ago
Solved

Data Modeling

Hello, I'm creating an FTE (Full-time equivelant) overview, but the date is as it follows:  1- End Date of 31/12/9999 means the present day + if the status is active then still Active (in service)....
  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi,

    Thanks for the solution johnbasha33  provided, and i want to offer some information for user to refer to.

    hello O-K, you can create a blank query and put the following code to advanced editor.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nZE7CgIxEECvIqkX5pNdkymjWHiGZQuRFIK/QgRv72Q1uErU1XSveLzJTNsaadCayix2x+3hEuMkKLAFbIDEcQIPaIGRSSGsT5tzNF01QrQExCD6FJb71UPlxg3NWTLrVCHx05uJrpAc4WnxX680qZAMzbkC0n0f6fcWgTBD+MF7vxvXT/+ksgD6pOZ7cCH5VfzUdL40bp+pX9Xc7K4=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Emp.No = _t, #"Emp Name" = _t, #"Start date" = _t, #"End Date" = _t, Status = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Emp.No", Int64.Type}, {"Emp Name", type text}, {"Start date", type text}, {"End Date", type text}, {"Status", type text}}),
        #"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type", {{"Start date", type date}, {"End Date", type date}}, "en-GB"),
        #"Filtered Rows" = Table.SelectRows(#"Changed Type with Locale", each ([Emp.No]=List.Max(Table.SelectRows(#"Changed Type with Locale",(x)=>x[Emp Name]=[Emp Name])[Emp.No]))),
        #"Grouped Rows" = Table.Group(#"Filtered Rows", {"Emp.No"}, {{"Count", each Table.AddIndexColumn(_,"Index",1,1), type table }}),
        #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Emp Name", "Start date", "End Date", "Status", "Index"}, {"Emp Name", "Start date", "End Date", "Status", "Index"}),
        #"Added Custom" = Table.AddColumn(#"Expanded Count", "Custom", each let a=try Table.SelectRows(#"Expanded Count",(x)=>x[Emp Name]=[Emp Name] and x[Status]="Inactive")[End Date]{0} otherwise null,
    b=if a<>null then List.Max(Table.SelectRows(#"Expanded Count",(x)=>x[Emp Name]=[Emp Name] and x[End Date]<a)[End Date]) else null
    in if [Status]="Inactive" then null  else if [Status]="Active" and [End Date]=b then "Inactive" else [Status]),
        #"Filtered Rows1" = Table.SelectRows(#"Added Custom", each ([Custom] <> null)),
        #"Replaced Value" = Table.ReplaceValue(#"Filtered Rows1",each [Start date],each if [Index]=1 then [Start date] else Table.SelectRows(#"Filtered Rows1",(x)=>x[Emp Name]=[Emp Name] and x[Index]=[Index]-1)[End Date]{0},Replacer.ReplaceValue,{"Start date"}),
        #"Removed Columns1" = Table.RemoveColumns(#"Replaced Value",{"Status", "Index"}),
        #"Replaced Value1" = Table.ReplaceValue(#"Removed Columns1",#date(9999, 12, 31),DateTime.Date(DateTime.LocalNow()),Replacer.ReplaceValue,{"End Date"})
    in
        #"Replaced Value1"

     

    Output

     

    Best Regards!

    Yolo Zhu

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.