Forum Discussion
Split current month values to daily
Hi,
I`m a newbie to Power BI. I`ve run into an issue & couldnt find the solution I`m looking for.
After getting the data from the source, in power query I`d like to split the current month value to daily rows depending on the product type. Below is the data
| Product | Qty | Price | Start Date | End Date |
| A | 50 | 25 | 7/1/2020 | 7/31/2020 |
| B | 100 | 35 | 7/1/2020 | 7/31/2020 |
| A | 75 | 30 | 8/1/2020 | 8/31/2020 |
| B | 30 | 40 | 9/1/2020 | 9/30/2020 |
| B | 85 | 35 | 10/1/2020 | 10/31/2020 |
Expected Result - Assume today is 07/22/2020, the new column should have daily values starting from yesterday, 07/21/2020 for the current month rows & divide the quantity by number of days in the month for product A. Similarly product B is divided by number of working days in the current month.
Next month data rows will stay monthly & when the month becomes current, the logic will split to daily rows.
I used - List.Dates([START DATE],Date.Day(Date.EndOfMonth([START DATE])), #duration(1,0, 0, 0)), but this splits to daily rows for all the months in the data.
| Product | Qty | Price | Start Date | End Date |
| A | 1.613 | 25 | 7/21/2020 | 7/21/2020 |
| A | 1.613 | 25 | 7/22/2020 | 7/22/2020 |
| A | 1.613 | 25 | 7/23/2020 | 7/23/2020 |
| A | 1.613 | 25 | 7/24/2020 | 7/24/2020 |
| A | 1.613 | 25 | 7/25/2020 | 7/25/2020 |
| A | 1.613 | 25 | 7/26/2020 | 7/26/2020 |
| A | 1.613 | 25 | 7/27/2020 | 7/27/2020 |
| A | 1.613 | 25 | 7/28/2020 | 7/28/2020 |
| A | 1.613 | 25 | 7/29/2020 | 7/29/2020 |
| A | 1.613 | 25 | 7/30/2020 | 7/30/2020 |
| A | 1.613 | 25 | 7/31/2020 | 7/31/2020 |
| B | 4.348 | 35 | 7/21/2020 | 7/21/2020 |
| B | 4.348 | 35 | 7/22/2020 | 7/22/2020 |
| B | 4.348 | 35 | 7/23/2020 | 7/23/2020 |
| B | 4.348 | 35 | 7/24/2020 | 7/24/2020 |
| B | 4.348 | 35 | 7/25/2020 | 7/25/2020 |
| B | 4.348 | 35 | 7/26/2020 | 7/26/2020 |
| B | 4.348 | 35 | 7/27/2020 | 7/27/2020 |
| B | 4.348 | 35 | 7/28/2020 | 7/28/2020 |
| B | 4.348 | 35 | 7/29/2020 | 7/29/2020 |
| B | 4.348 | 35 | 7/30/2020 | 7/30/2020 |
| B | 4.348 | 35 | 7/31/2020 | 7/31/2020 |
| A | 75 | 30 | 8/1/2020 | 8/31/2020 |
| B | 30 | 40 | 9/1/2020 | 9/30/2020 |
| B | 85 | 35 | 10/1/2020 | 10/31/2020 |
I understand doing this in power query is performance efficient than a DAX column. Please suggest what`s best here.
Thanks in advance.
Anil
I am still curious what analysis/visual you plan to do, but this was a fun query challenge so I went ahead and did it (I think). To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below.
let Source = Table.FromRows( Json.Document( Binary.Decompress( Binary.FromText( "i45WclTSUTI1ABJGpkDCXN9Q38jAyADMNIayY3WilZyAIoYGIHFjvOpAxpmDVBiD5CwQyiwwjAOrMAERlghllvrGBqjKLExhlhoaINQB2XDzYgE=", BinaryEncoding.Base64 ), Compression.Deflate ) ), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table[Product = _t, Qty = _t, Price = _t, #"Start Date" = _t, #"End Date" = _t] ), #"Changed Type" = Table.TransformColumnTypes( Source, { {"Product", type text}, {"Qty", Int64.Type}, {"Price", Int64.Type}, {"Start Date", type date}, {"End Date", type date} } ), #"Added Custom" = Table.AddColumn( #"Changed Type", "New Start Date", each let thisdate = Date.From(DateTime.LocalNow()) in if Date.Month([Start Date]) = Date.Month(thisdate) then List.Dates( Date.AddDays(thisdate, - 1), Duration.TotalDays(Date.EndOfMonth(thisdate) - thisdate) + 1, #duration(1, 0, 0, 0) ) else {[Start Date]} ), #"Added Custom1" = Table.AddColumn( #"Added Custom", "New Qty", each [Qty] / List.Count([New Start Date]) ), #"Expanded DateList" = Table.ExpandListColumn(#"Added Custom1", "New Start Date"), #"Changed Type1" = Table.TransformColumnTypes( #"Expanded DateList", {{"New Start Date", type date}, {"New Qty", type number}} ) in #"Changed Type1"I didn't add the end date, but that should be straight forward if needed. The above does what you were looking for expanding the current month to daily level and calculating the daily quantity.
If this works for you, please mark it as the solution. Kudos are appreciated too. Please let me know if not.
Regards,
Pat
3 Replies
- mahoneypatMicrosoft Employee
I am still curious what analysis/visual you plan to do, but this was a fun query challenge so I went ahead and did it (I think). To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below.
let Source = Table.FromRows( Json.Document( Binary.Decompress( Binary.FromText( "i45WclTSUTI1ABJGpkDCXN9Q38jAyADMNIayY3WilZyAIoYGIHFjvOpAxpmDVBiD5CwQyiwwjAOrMAERlghllvrGBqjKLExhlhoaINQB2XDzYgE=", BinaryEncoding.Base64 ), Compression.Deflate ) ), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table[Product = _t, Qty = _t, Price = _t, #"Start Date" = _t, #"End Date" = _t] ), #"Changed Type" = Table.TransformColumnTypes( Source, { {"Product", type text}, {"Qty", Int64.Type}, {"Price", Int64.Type}, {"Start Date", type date}, {"End Date", type date} } ), #"Added Custom" = Table.AddColumn( #"Changed Type", "New Start Date", each let thisdate = Date.From(DateTime.LocalNow()) in if Date.Month([Start Date]) = Date.Month(thisdate) then List.Dates( Date.AddDays(thisdate, - 1), Duration.TotalDays(Date.EndOfMonth(thisdate) - thisdate) + 1, #duration(1, 0, 0, 0) ) else {[Start Date]} ), #"Added Custom1" = Table.AddColumn( #"Added Custom", "New Qty", each [Qty] / List.Count([New Start Date]) ), #"Expanded DateList" = Table.ExpandListColumn(#"Added Custom1", "New Start Date"), #"Changed Type1" = Table.TransformColumnTypes( #"Expanded DateList", {{"New Start Date", type date}, {"New Qty", type number}} ) in #"Changed Type1"I didn't add the end date, but that should be straight forward if needed. The above does what you were looking for expanding the current month to daily level and calculating the daily quantity.
If this works for you, please mark it as the solution. Kudos are appreciated too. Please let me know if not.
Regards,
Pat
- mahoneypatMicrosoft Employee
This is doable in the query editor, but can I ask what calculation/visualization you plan to do once the data are in that format? There may be a way to get your result directly with DAX from your data in monthly form.
Regards,
Pat
- AnonymousNot applicable
mahoneypat - Thanks for your response. It worked liked a charm.
As I was exploring, I came across the IsInCurrentMonth function & used the below. I expanded the list & filtered on dates. it worked as expected
if Date.IsInCurrentMonth([START_DATE]) then List.Dates([START_DATE],Date.Day(Date.EndOfMonth([START_DATE])), #duration(1,0, 0, 0)) else nullThe calculation on the quantity was acheieved using another column in the data.
Kudos for your help.
Cheers,
Anil