Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

HELP urgently needed!

Hi All,   Another day another challenge) Please help me if you faced someting similar   I've got some data where shown some documents priority, date of issue, date of finalized, and status. What...
  • ronrsnfld's avatar
    ronrsnfld
    2 years ago

    Here is M-Code that should do what you want.

     

    You may be able to omit the line that cleans the data where you have no Finalize date for Closed items (by replacing that null with the Issue date).

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCjBU0lEyNNI3NNc3MjAyAnL8C1LzgJSCUqwOQtrIEibtnJNfnJoCEtU3sgAJmiAUGusbGoOEjNHUGSKpA8mZ6BsawtShWgcSMdM3MsIuC7LfFGocih1EyhuC5Q0tsLnRAM2NIE/jdoWZvqElpmwsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Priority = _t, #"Date of Issue" = _t, Status = _t, #"Date of Finalized" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{
            {"Priority", type text}, {"Date of Issue", type date}, {"Status", type text}, {"Date of Finalized", type date}}),
    
    //If Status=Closed and Finalize = null make Finalize = Date of Issue
    //May not need this if your data is clean
        #"Replace null Finalized" = Table.ReplaceValue(
            #"Changed Type",
            each [Date of Issue],
            each [Status],
            (final,issue, status)=> if status="Closed" and final=null then issue else final,
            {"Date of Finalized"}
        ),
    
    //Task is "Open" in month that it is opened up to the month before closed or Month(Today) if not closed.
        #"Add Months Open" = Table.AddColumn(#"Replace null Finalized", "Months Open", (c)=>
            List.Generate(
                ()=>[m=Date.StartOfMonth(c[Date of Issue])],
                each if c[Status] = "Closed" 
                        then [m] < Date.StartOfMonth(c[Date of Finalized]) 
                        else [m] < Date.EndOfMonth(Date.From(DateTime.FixedLocalNow())),
                each [m = Date.AddMonths([m],1)],
                each [m]), type {date}
            ),
        #"All Months" = List.Transform(List.Sort(List.Distinct(List.Combine(#"Add Months Open"[Months Open])), Order.Ascending),
                            each Date.ToText(_, "MMM-yyyy")),
        #"Table Types" = 
            let 
                //columnNames = List.Transform(#"Removed Columns1"[all], each _{0}[Column1]),
                columnTypes = List.Repeat({Int64.Type}, List.Count(#"All Months")),
                rowColumnTypes = List.Transform(columnTypes, (t) => [Type = t, Optional = false]),
                rowType = Type.ForRecord(Record.FromList(rowColumnTypes, #"All Months"), false)
            in 
                rowType,
    
        #"Removed Columns" = Table.RemoveColumns(#"Add Months Open",{"Date of Issue", "Status", "Date of Finalized"}),
        #"Grouped Rows" = Table.Group(#"Removed Columns", {"Priority"}, {
            {"Month Count", (t)=> let 
                #"Expand Date List" = Table.SelectRows(Table.ExpandListColumn(t,"Months Open"), each [Months Open] <> null),
                #"Format Dates" = Table.TransformColumns(#"Expand Date List", {"Months Open", each Date.ToText(_,"MMM-yyyy"), type text}),
                #"Pivoted Column" = Table.Pivot(#"Format Dates", 
                    List.Distinct(#"Format Dates"[#"Months Open"]), "Months Open", "Priority", List.Count)
            in #"Pivoted Column", type table #"Table Types"
                }
            }),
        #"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"Priority", Order.Ascending}}),
        #"Expanded Month Count" = Table.ExpandTableColumn(#"Sorted Rows", "Month Count", #"All Months")
    in
        #"Expanded Month Count"

    Data Sample:

    Results