Forum Discussion
Split Rows based on Days
Hello Community,
I want to split rows based on days.Please see below Row Data and Final Data for sample.
If Days =<364 Days then not split just keep as it is but if Days>364 then split every row by 364 Days.
Example:
If Days are 1094 then split 3 rows for 364 Days & 364 Days & 364 Days.
if Days are 584 then split rows into 2,first rows is for 364 Days and second rows for 220.
| ROW DATA | |||
| ID | Begin | End | Days |
| A | 8/15/2018 | 8/13/2021 | 1094 |
| B | 3/13/2018 | 10/18/2019 | 584 |
| C | 9/1/2018 | 8/31/2019 | 364 |
| D | 11/1/2018 | 7/31/2019 | 272 |
| FINAL DATA | |||
| ID | Begin | End | Days |
| A | 8/15/2018 | 8/14/2019 | 364 |
| A | 8/15/2019 | 8/13/2020 | 364 |
| A | 8/14/2020 | 8/13/2021 | 364 |
| B | 3/13/2018 | 3/12/2019 | 364 |
| B | 3/13/2019 | 10/18/2019 | 220 |
| C | 9/1/2018 | 8/31/2019 | 364 |
| D | 11/1/2018 | 7/31/2019 | 272 |
Hi,
This M code works
let Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content], #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", type text}, {"Begin", type date}, {"End", type date}, {"Days", Int64.Type}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Remainder", each Number.Mod([Days],364)), #"Added Custom1" = Table.AddColumn(#"Added Custom", "Integer", each Number.IntegerDivide([Days],364)), #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Rows to be created", each if [Integer]=0 then 1 else if [Remainder]=0 then [Integer] else [Integer]+1), #"Added Custom3" = Table.AddColumn(#"Added Custom2", "Custom", each {Number.From(1)..Number.From([Rows to be created])}), #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom3", "Custom"), #"Added Custom4" = Table.AddColumn(#"Expanded Custom", "Number of days", each if [Days]<=364 then [Days] else if [Custom]*364<=[Days] then 364 else [Remainder]), #"Removed Columns" = Table.RemoveColumns(#"Added Custom4",{"Days", "Remainder", "Integer", "Rows to be created", "Custom"}) in #"Removed Columns"Hope this helps.
12 Replies
- parry2kSuper User
Anonymous seems like there is mistake in calculation, if you look at A, 1094 days divided by 3 times x 364 make it is 1092 and I believe there should be fourth row with 2 days, isn't it?
- AnonymousNot applicable
Hi Perry,
Thank you so much for your comments.
As per the requirements it should go by 364 Days +364 Days+366 Days and require only 3 rows (36 Months).
Thanks
- AnonymousNot applicable
Hi Perry,
You can think in that way,
Days=1094 is 36 Months
Days=584 is 19 Months
Days=364 is 12 Months
Days=272 is 9 Months
- parry2kSuper User
Anonymous just checking the days values are always going to be on of these options:
1094
584
364
272
Reason is to understand what should be the best logic to achieve it, if these are the only four options it will be a different logic