Forum Discussion
Creating a summary table through power query without importing another source
- 3 years ago
No problem. If you get stuck later on, just drop a note on this thread or me and I'll help you out.
1) First thing to do is create a filled end date column in Power Query. This will give our measures proper date bounds on every row to calculate between:
if [End Date] = null then Date.From(DateTime.LocalNow()) else [End Date]2) Next, you'll need a calendar table. As a minimum, it will need a date column and a month column. I've attached an example PBIX to the bottom of this post so, if you don't have one already, there's a basic calendar table in there you can use.
3) Once these have both been created in PowerQuery, apply them to your data model. Check the model tab in Power BI Desktop once they've loaded to ensure the two tables are NOT related. PBI might do this automatically for you so, if it has, delete the relationship.
4) Create the following measures:
_startOfMonth = VAR __cDate = MIN('calendar'[date]) RETURN CALCULATE( COUNTROWS(factResource), FILTER( factResource, factResource[Start Date] <= __cDate && factResource[End Date Filled] >= __cDate ) ) + 0_endOfMonth = VAR __cDate = MAX('calendar'[date]) RETURN CALCULATE( COUNTROWS(factResource), FILTER( factResource, factResource[Start Date] <= __cDate && factResource[End Date Filled] >= __cDate ) ) + 05) VISUALISE! Always use your calendar table to provide date dimensions to your visuals, and the measures will work just fine:
You'll notice that I've left vacancies in the data, but I've given them all relevant start/end dates as per our discussion.If you don't want to usethat process, you can just filter out the whole vacancy contract type in PQ and the remaining type will work fine.
Pete
PBIX attached down here somwhere:
- 3 years ago
Hi third_hicana ,
By default, Power BI will display all the data it has available in the data. In order to control the months that are visible in a visual when the data contains more than required, you'll need to filter the visual accordingly. My personal preference is to use a relative month column in my calendar table:
( Date.Year([date]) * 12 + Date.Month([date]) ) - ( Date.Year(Date.From(DateTime.LocalNow())) * 12 + Date.Month(Date.From(DateTime.LocalNow())) )Change the data type of this new column to whole number (or decimal, if you're folding to an SQL server). You can then use this field in a visual-level filter as calendar[relativeMonth] <= 0 to only show data from current month or before.
The blank rows with zeroes can be fixed in two ways:
1) Filter your visual to only show the Contractor and Payroll contract types, or to not show blank contractor type, or
2) Remove the ' +0 ' from the end of each of the measures that I gave you. Note that doing this will stop rows appearing in your visual when you have no headcount of a particular contract type in any given month.
Pete
- 3 years ago
Hello again 🙂
My immediate guess is that the date bounds we gave the rows that have no end date is too limited for your new requirement. We currently have this, which ends a null duration with today's date:
if [End Date] = null then Date.From(DateTime.LocalNow()) else [End Date]Try using this instead to be able to view three months into the future:
if [End Date] = null then Date.From(Date.StartOfMonth(Date.AddMonths(DateTime.LocalNow(), 4))) else [End Date]This will set a null end date to the first day of the fourth month from today. This gives three full months ahead.
Revert both of the measures back to their original code and this should work fine.
Pete
Hi third_hicana ,
Cool, thanks. You've given me almost everything I need.
One more thing: Your source table vacancies have no Start/End dates. We can work without these, but that would mean that any time a vacancy row is added or removed, this change will be seen across every month, current and history. Is this acceptable and, if not, would it be feasible to manage the vacancies in the same way that you manage the other contracts i.e. with start and end dates for each? This would allow us to keep the temporal vacancies intact for history as they inevitably change moving forward.
Pete
Hi BA_Pete, I need to also reflect in the bar graph the number of vacancies each month. I think what would work is to put a target start date so that it would not be a blank cell and operate the same as other contract type in terms of counting. The latter i think woule be preferrable. Let me know if you need more info. 🙂
Thank you 🙂
- BA_Pete3 years agoSuper User
If you need to be able to track the correct number of vacancies each month, you'll need to manage them in the same way as the other contract types. For example:
Jim leaves and his last working day is 25/06/2021.
A new vacancy row is created with start date 26/06/2021 and no end date.
Jane fills this vacancy and her first working day is 18/07/2021.
You vacancy row is updated with an end date of 17/07/2021.
So your table now looks like this:
Resource Start Date End Date Jim 01/01/1999 25/06/2021 Vacancy 26/06/2021 17/07/2021 Jane 18/07/2021 This would take a bit more management, but give you properly accurate results. Are you happy for me to assume this will be the process for my solution?
Pete
- third_hicana3 years agoHelper IV
That would work. But my concern in that scenario is the vacancy row depends if the organisation plans to add a worker in the future or not. In your example, Jim's position does not automatically create a vacancy for the position that he left. So, it depends, if they will fill in the position Jim left in the future or they will not add a vacancy for Jim's replacement. In that case, maybe Jim's working duration is for a special project only and that is the ony time that they need him. So, the number of vacancies was already planned. They may add vacancies or they may not. The vacancies is also dependent to the skill type that they need. So in short, the position left by Jim's does not automatically create a new vacancy.
Thanks 🙂
- BA_Pete3 years agoSuper User
That's fine, I wasn't suggesting to make this happen automatically. Rather, that whoever manages the source table would need to update these vacancy dates appropriately.
Without doing something along these line, there's no way of getting a true vacancy figure each month, as there's no dates to relate the vacancies to.
I can put something together ignoring the vacancies and you can have a think about how you would like to manage them. At least you will have a template example of how I've managed the other contract types, so you can add them later if you choose to go down the dated-row path.
Pete