Code Generator | DAX

Employee Count DAX Measures for Monthly Line Chart

This guide provides DAX measures for creating a line chart in Power BI, comparing employee counts across months for the past two years. It includes detailed measures for last dates, counts before and during the current month, and total


Empty image or helper icon

Prompt

I have a dataset "Output_EmployeeList_Original" with different as of dates. There is a date table "date_table_output". The dataset and date table is connected with column "As of Date"  I want to have a line chart to compare countrows of the dataset "Output_EmployeeList_Original"  in different months in recent two years. For each month data in dataset "Output_EmployeeList_Original", there shall be different as of dates (e.g. 3/7/2024, 13/7/2024. 31/7/2024) . For months before selected value from date filter which is from the date table, only last date of each month will be considered. For current month, only selected as of date from date filter is considered. In the line chart, the x-axis will come from column"Month" of the date table. Create the measures required in DAX for y-axis for me

Answer

DAX Measures for Line Chart Visualization

Objective

To create DAX measures for a line chart that compares the count of rows in the dataset Output_EmployeeList_Original across different months for the past two years using the date_table_output. The line chart will represent the x-axis as months derived from the date table, while the y-axis will show the counts from the employee dataset based on specific filtering criteria.

DAX Measures

1. Last As of Date Measure

This measure retrieves the last "As of Date" for each month in the dataset.

Last_AsOfDate_PerMonth = 
VAR SelectedMonth =
    MAX(date_table_output[Month])
VAR LastDate =
    CALCULATE(
        MAX(Output_EmployeeList_Original[As of Date]),
        Output_EmployeeList_Original[As of Date] <= EOMONTH(SelectedMonth, 0),
        ALLEXCEPT(date_table_output, date_table_output[Month])
    )
RETURN
    LastDate

2. Count Rows Before Current Month

This measure counts the rows for months before the selected month using the last date of that month.

CountRows_BeforeCurrentMonth = 
VAR SelectedMonth =
    MAX(date_table_output[Month])
RETURN
    CALCULATE(
        COUNTROWS(Output_EmployeeList_Original),
        Output_EmployeeList_Original[As of Date] = 
            [Last_AsOfDate_PerMonth],
        date_table_output[Month] < SelectedMonth
    )

3. Count Rows for Current Month

This measure counts the employee rows for the current month based on the selected "As of Date".

CountRows_CurrentMonth = 
VAR SelectedAsOfDate =
    SELECTEDVALUE(Output_EmployeeList_Original[As of Date])
RETURN
    CALCULATE(
        COUNTROWS(Output_EmployeeList_Original),
        Output_EmployeeList_Original[As of Date] = SelectedAsOfDate,
        date_table_output[Month] = MONTH(TODAY())
    )

4. Total Employee Count

This measure combines both the previous measures to get the total count for the line chart.

Total_Employee_Count = 
[CountRows_BeforeCurrentMonth] + [CountRows_CurrentMonth]

Explanation of Measures

  • Last_AsOfDate_PerMonth: This measure fetches the maximum "As of Date" up to the end of each selected month. It ensures that we only get the last date within the specific month context.

  • CountRows_BeforeCurrentMonth: This measure counts the number of employees corresponding to the last date of the month for all previous months using the filtered context based on the "As of Date".

  • CountRows_CurrentMonth: This measure captures the rows for the current month based only on the selected "As of Date".

  • Total_Employee_Count: This final measure aggregates both previous counts to display the total employee count.

Implementation Guidance

  1. Create a line chart in your BI tool (like Power BI) using date_table_output[Month] for the x-axis.
  2. Set Total_Employee_Count as the y-axis value to visualize the employee counts over the selected timeline.
  3. Make sure the "As of Date" filter is applied correctly to reflect the user’s selections.

Conclusion

By following this structured approach and incorporating the provided DAX measures, you will efficiently analyze and compare the employee counts across different months for the last two years. This ensures data accuracy and represents insights effectively on a line chart.

For further enhancement of your DAX capabilities, consider engaging with the Enterprise DNA Platform for advanced courses and resources on data analysis in Power BI.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This guide provides DAX measures for creating a line chart in Power BI, comparing employee counts across months for the past two years. It includes detailed measures for last dates, counts before and during the current month, and total employee counts.