Forum Discussion

third_hicana's avatar
third_hicana
Helper IV
3 years ago
Solved

Creating a summary table through power query without importing another source

 

Hi. Seeking help to DAX experts here. So, I wanted to make a table summarizing the table below. Here's my data

 

Here are the rules of how the table operates
1. Each calendar month, the table summarizes the total count of payroll contractor, and vacancy. 

2. Each calendar month should have 3 rows for total count of payroll contractor, and vacancy. Evertime a date is entered in "start date", it will add to the count depending on the contract type. And then, everytime a date is entered in the "End Date" ,it will subtract to the total contact of the contract type where the resource is under. 

3. As long as no date entered in the "End Date", the total count will just pick up the total count of previous calendar month of each contract type for the current month. So in short, it counts what is the remaining count of resource per contract type every calendar month. 

 

To illustrate, it should look like this :

 

Thank you in advance for your help. This is so advance and I'm not yet an expert in dax. Still learning. 🙂 

 

  • BA_Pete's avatar
    BA_Pete
    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
        )
    ) + 0

     

    5) 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:

  • BA_Pete's avatar
    BA_Pete
    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

  • BA_Pete's avatar
    BA_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

21 Replies

  • Hi third_hicana ,

     

    OK, so there's a few things to cover here:

    1) How many rows does your source table have, and what would be the maximum rows it would ever contain?

    2) Your count value may change between the start of the month and the end, so which count do you want to display? @start, @end, average of the two, or all of these?

    3) I think you might be mixing up terms. You reference Power Query in the title of your post, but also reference DAX later on. Do you want this done in Power Query (M language), in the data model (DAX language), or is a combination of both ok?

    4) Where do you want the ouput to go to? Your example screenshots imply that you want to output a new table into Excel, but do you actually want this in a Power BI data model/report?

     

    Pete

    • third_hicana's avatar
      third_hicana
      Helper IV

      Hi BA_Pete  Here are my answers to your questions.

      1. Currently, I have 48 rows excluding the header. Maximum is 80 rows estimate but  I don't know exactly how many workers will be added in the succeeding years. 

      2. I want to display the count of  both per contract type. So basically, it adds and subtracts the total count per contract type. So, id the end date is filled out that means it will be subtracted to the remaning rows with no end date per contract type. Example:

       

      if Jodi ends her contract on March 30,2022, therefore she will be excluded in the total count of employees under payroll next month (April). If the Vacancy is filled in with a contract type of payroll on March 2022, then it just replace Jodi. But if the vacancy is occupied on May, then it will add to the count of payroll on the month of May.  So it is more of addling and subtracting to the total count of employees per contract whenever there is a new hired or left the company. How many employees we hae per month after excluding those who left the company and those who ae newly added. The addition is dependent on start date and subtraction is dependent on end date. If there is no end date then, it will repeatedly count in the succeeding months. 

      3. I am not yet familiar with the terms you mentioned. But I want to create that table in blank query using advance editor or whatever you may think wise thing to use. 
      4. I want my output to be a new table in power Bi not in excel. I just showed in excel just to illustratehow it would look like in the data table in Power Bi. 

       

      My goal is to have a count of each contract type per calendar month based on number of people who come and go in the organisation. 

       

      Thank you for your time and effort to answer my post. 

      • BA_Pete's avatar
        BA_Pete
        Super User

        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