Forum Discussion
Distributing total hours by month from start date
Hi guys. I'm desperate for help.
I don't know how many hours I've already spent looking into this, and you are my last resort.
I've a table with the folowing data:
| ID | Abs cod | Abs desc | Hours | Daily Hours | Days | Start |
| 6910049 | 6090 | Vacation | 8,00 | 8 | 1 | 30/12/2022 |
| 6910057 | 6100 | Vacation | 8,00 | 8 | 1 | 30/12/2022 |
| 6910066 | 6090 | Vacation | 8,00 | 8 | 1 | 30/12/2022 |
| 6910113 | 6090 | Vacation | 8,00 | 8 | 1 | 30/12/2022 |
| 6910137 | 6090 | Vacation | 24,00 | 8 | 3 | 30/12/2022 |
| 6910419 | 6080 | Maternity Leave | 757,50 | 7,5 | 101 | 06/08/2022 |
How can I add the columns at the right with the splitting of hours by the avaiable days by month?
| ID | Abs cod | Abs desc | Hours | Daily Hours | Days | Start | 01/08/2022 | 01/09/2022 | 01/10/2022 | 01/11/2022 | 01/12/2022 | 01/01/2023 |
| 6910049 | 6090 | Vacation | 8,00 | 8 | 1 | 30/12/2022 | 8 | |||||
| 6910057 | 6100 | Vacation | 8,00 | 8 | 1 | 30/12/2022 | 8 | |||||
| 6910066 | 6090 | Vacation | 8,00 | 8 | 1 | 30/12/2022 | 8 | |||||
| 6910113 | 6090 | Vacation | 8,00 | 8 | 1 | 30/12/2022 | 8 | |||||
| 6910137 | 6090 | Vacation | 24,00 | 8 | 3 | 30/12/2022 | 16 | 8 | ||||
| 6910419 | 6080 | Maternity Leave | 757,50 | 7,5 | 101 | 06/08/2022 | 195 | 217,5 | 225 | 120 |
After I get them, I know i'll have to unpivot them, but that's the last step.
As a side note, the table will have more data with dates from 2022 to the future, and I've already created a Calendar Table.
I've tryed a lot of things on PowerQuery, but I really don't master M language.
Can anybody help?
Thanks for you help in advance!
- Anonymous2 years ago
Hi, pquintela
For M language, you may be able to try the following expressions and modify them based on your data:
let Source = Excel.Workbook(File.Contents("C:\\Users\\v-yohua\\Documents\\Distributing total hours by month from start date.xlsx"), null, true), Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data], #"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"ID", Int64.Type}, {"Abs cod", Int64.Type}, {"Abs desc", type text}, {"Hours", type number}, {"Daily Hours", type number}, {"Days", Int64.Type}, {"Start", type date}}), AddEndDate = Table.AddColumn(#"Changed Type", "End", each Date.AddDays([Start], [Days]-1)), AddDateList = Table.AddColumn(AddEndDate, "DateList", each {Number.From([Start])..Number.From([End])}), ExpandDateList = Table.ExpandListColumn(AddDateList, "DateList"), ChangeToDate = Table.TransformColumnTypes(ExpandDateList,{{"DateList", type date}}) in ChangeToDateThis code will add an "End" column to your table that represents the end date of each ID. It then creates a "DateList" column that contains each day from the start date to the end date. Finally, it expands the DateList column into new rows, with each row corresponding to a date
How to Get Your Question Answered Quickly
If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data)
Best Regards
Yongkang Hua
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
1 Reply
- AnonymousNot applicable
Hi, pquintela
For M language, you may be able to try the following expressions and modify them based on your data:
let Source = Excel.Workbook(File.Contents("C:\\Users\\v-yohua\\Documents\\Distributing total hours by month from start date.xlsx"), null, true), Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data], #"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"ID", Int64.Type}, {"Abs cod", Int64.Type}, {"Abs desc", type text}, {"Hours", type number}, {"Daily Hours", type number}, {"Days", Int64.Type}, {"Start", type date}}), AddEndDate = Table.AddColumn(#"Changed Type", "End", each Date.AddDays([Start], [Days]-1)), AddDateList = Table.AddColumn(AddEndDate, "DateList", each {Number.From([Start])..Number.From([End])}), ExpandDateList = Table.ExpandListColumn(AddDateList, "DateList"), ChangeToDate = Table.TransformColumnTypes(ExpandDateList,{{"DateList", type date}}) in ChangeToDateThis code will add an "End" column to your table that represents the end date of each ID. It then creates a "DateList" column that contains each day from the start date to the end date. Finally, it expands the DateList column into new rows, with each row corresponding to a date
How to Get Your Question Answered Quickly
If it does not help, please provide more details with your desired output and pbix file without privacy information (or some sample data)
Best Regards
Yongkang Hua
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.