Hi Folks,
Today I got a call from my friend who encountered a problem related to Power BI and contacted me to help him out.
Problem Statment:
The client has provided him a budget file that looks like this
![](https://static.wixstatic.com/media/6f79f1_871bb967160c4f20976c410b3b2c4423~mv2.png/v1/fill/w_684,h_99,al_c,q_85,enc_auto/6f79f1_871bb967160c4f20976c410b3b2c4423~mv2.png)
Now the issue is, he has recorded the total annual budget against an expense on the last date of the year. and when he creates a monthly report, it looks like this
![](https://static.wixstatic.com/media/6f79f1_5c0f15fdf8cd4871babc3f1b44189211~mv2.png/v1/fill/w_420,h_632,al_c,q_85,enc_auto/6f79f1_5c0f15fdf8cd4871babc3f1b44189211~mv2.png)
As you see the total budget is showing against the last month and any kind of monthly analysis is quite difficult.
Question: Is there any way to convert this annual budget to daily/monthly budget in power query?
Answer = Yes
Solution:
Let's breakdown the solution in easy to understand steps
Create the start date for the budget year (In our case Fiscal calendar starts in July)
Create a List of dates between the start date and end date.
Get the end of the month for each date (as in my case budget is recorded at month end).
Recalculate budget value for months.
Step 1: Start Date for the year
For this, we will go to add a column and select the custom column
![](https://static.wixstatic.com/media/6f79f1_0de913528f8044c98742cafa45e77a47~mv2.png/v1/fill/w_624,h_87,al_c,q_85,enc_auto/6f79f1_0de913528f8044c98742cafa45e77a47~mv2.png)
Now we will write a small piece of code to achieve our purpose
![](https://static.wixstatic.com/media/6f79f1_46c1b8dae18a44818f9e9402a537017f~mv2.png/v1/fill/w_624,h_387,al_c,q_85,enc_auto/6f79f1_46c1b8dae18a44818f9e9402a537017f~mv2.png)
When we click ok, a Year start column will be there in our data
![](https://static.wixstatic.com/media/6f79f1_ccf2d4cacfa34028aa0ef09419e9a585~mv2.png/v1/fill/w_607,h_110,al_c,q_85,enc_auto/6f79f1_ccf2d4cacfa34028aa0ef09419e9a585~mv2.png)
Step 2: Date range between Start and end date
Once the start date and end date are created, now we can write our main query which will look like this
![](https://static.wixstatic.com/media/6f79f1_d00cd25b7b3f4454944e208af55e7bfd~mv2.png/v1/fill/w_980,h_588,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/6f79f1_d00cd25b7b3f4454944e208af55e7bfd~mv2.png)
Let’s break down the query to understand how it is working
![](https://static.wixstatic.com/media/6f79f1_864d3027eef54d4e93e7f4783cb5156c~mv2.png/v1/fill/w_624,h_77,al_c,q_85,enc_auto/6f79f1_864d3027eef54d4e93e7f4783cb5156c~mv2.png)
Whenever we need to create a list between two numbers we can easily create it by use of curly brackets { }. if you write {1..5} power query will give you a list of numbers from 1 to 5. So I used the same concept here
Converted the “Year Start” column type from Date to Number by using “Number.From”. as the sequential list will only be created between numbers.
Use two dotes for Range.
Converted “Budget Year” column type from Date to Number by using “Number.From”.
Closed the query in Curly brackets.
If I use only this query, it will generate a number sequence which when converted to date type will give a sequential date list
![](https://static.wixstatic.com/media/6f79f1_ffa57d94cc3a452b9f46d45fbd984929~mv2.png/v1/fill/w_419,h_359,al_c,q_85,enc_auto/6f79f1_ffa57d94cc3a452b9f46d45fbd984929~mv2.png)
But, I only want to get the last date of each month in the given year instead of the date range.
Step 3: Get the end-of-month date
Now let's focus on the other half of our code
![](https://static.wixstatic.com/media/6f79f1_a6202912476e492da3d658eefe7d84dd~mv2.png/v1/fill/w_840,h_239,al_c,q_85,enc_auto/6f79f1_a6202912476e492da3d658eefe7d84dd~mv2.png)
“List.Transform” will require to link the list that we have just created “Date”
“Date.EndOfMonth” function will transform each date to the last date of that particular month.
“each” function will look into each date from the list and push it into the end of month query
End of Month query will require data type as Date for input, But we have used Number.From query to convert our Date into number in our Step 2, now we will use Date.From query to convert our number list into date.
“(_)” this underscore after Date.From will look into the current row and convert it into number.
After performing these steps, we will pack this query into “List.Distinct” function. This function will help us to get only a distinct month-end date instead of a month-end date for each date. For example, if we do not use this function, there will be 365 rows (approx 30 rows of each month's end date), after applying this function, there will only be 12 rows.
Now our query is complete, we just need to close the loop by passing the “in” statement
![](https://static.wixstatic.com/media/6f79f1_b2581d384119421291a44f5a6307788c~mv2.png/v1/fill/w_177,h_140,al_c,q_85,enc_auto/6f79f1_b2581d384119421291a44f5a6307788c~mv2.png)
Now click on expand, and we now have 12 lines of each expense type for every month end
![](https://static.wixstatic.com/media/6f79f1_ed3c2c2dd18d4b7a93f9e00e2b01aff8~mv2.png/v1/fill/w_980,h_536,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/6f79f1_ed3c2c2dd18d4b7a93f9e00e2b01aff8~mv2.png)
Step 4: Monthly budget value
The last step is to divide the budget value by twelve to get the proportional budget amount for each month
![](https://static.wixstatic.com/media/6f79f1_f7b34898318d4674b9af99a9b3e0be57~mv2.png/v1/fill/w_980,h_310,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/6f79f1_f7b34898318d4674b9af99a9b3e0be57~mv2.png)
Divide pop-up will open, as I have an annual number, I simply divided it by 12 to get a monthly figure
![](https://static.wixstatic.com/media/6f79f1_e9272f5e8ddf40f59e2b9e9acba0930f~mv2.png/v1/fill/w_777,h_241,al_c,q_85,enc_auto/6f79f1_e9272f5e8ddf40f59e2b9e9acba0930f~mv2.png)
![](https://static.wixstatic.com/media/6f79f1_072ce0c7972d40c8ac68eb62dad46bc3~mv2.png/v1/fill/w_494,h_523,al_c,q_85,enc_auto/6f79f1_072ce0c7972d40c8ac68eb62dad46bc3~mv2.png)
Result:
Once all steps have been performed, load the close and apply the power query and change the create the relationship of date table with the budget table on month end date
![](https://static.wixstatic.com/media/6f79f1_5e5f07ff2d9645d7b5839ffd0da1a743~mv2.png/v1/fill/w_354,h_523,al_c,q_85,enc_auto/6f79f1_5e5f07ff2d9645d7b5839ffd0da1a743~mv2.png)
Now we have automated the conversion of the annual budget to a monthly budget. The same step will be applied to convert the annual budget to the daily budget except
Step 3
and will use the number of days in a year for division in step 4
Cheers!!!
Comments