Forum Discussion
HELP urgently needed!
- 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
Hi Anonymous, From your sample data, do you consider 'Open' documents to be where 'Date of Finalized' is blank? If so, you have 7 open documents by 'Date of Issue' Year and Month. Is this correct? See the attached .pbix file.
Hello amustafa ! Thanks for your reply, it looks great btw)
Yes, if Status is open, date finalized column must be blank.
What I need is, while counting total of certain month, should be counted all open dates (without considering a year) in one but without that certain month closed ones. For example, I need to count how many open documents I've got by February, so those documents that have closure date in February doesn't count, but those with March dates must be included cause they were open in February. So counting should go by Priorities, status and date of closure.