Forum Discussion
Transform Data to a per day view
- 5 years ago
Yes THU - see this code:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCkkszlYwVNJRMgJhAz0Dcz0jAyOwgBGcE6sDVQhSZIwqB+SYYio0hploiKzQGFOhCVAcLGdCwERTmNUGBEw0g1mNYqIZpkJzmIlmyAotMRVawEy0RFJojPBZbCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Task = _t, Duration = _t, #"Start Date " = _t, #"End Date" = _t]), #"Changed Type with Locale" = Table.TransformColumnTypes(Source, {{"Start Date ", type date}, {"End Date", type date}}, "en-PG"), #"Changed Type" = Table.TransformColumnTypes(#"Changed Type with Locale",{{"Duration", Int64.Type}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Date Range", each {Number.From([#"Start Date "])..Number.From([End Date])}), #"Expanded Date Range" = Table.ExpandListColumn(#"Added Custom", "Date Range"), #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Date Range",{{"Date Range", type date}}), #"Grouped Rows" = Table.Group(#"Changed Type1", {"Date Range"}, {{"Tasks per Day", each Table.RowCount(_), Int64.Type}}) in #"Grouped Rows"It turns this:
into this:
I created a new column called Date Range that is a list of the dates from Start Date to End Date, then expanded that list into rows, and finally grouped by that date and counted the rows (tasks). See below on how to copy that code to a blank query, but the crux of the logic is this list:
={Number.From([#"Start Date "])..Number.From([End Date])}Then I expanded that column to "New Rows" and changed it back to a date format. List ranges cannot contain dates, but you might recognize those numbers - they are the same "dates" Excel shows when it isn't formatted as a date.
How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.