Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
YoungLearning
Helper III
Helper III

Getting help with this complicated DAX question

YoungLearning_0-1722265571715.png

 

Can someone explain to me why CountRows since the employee name will be recorded for every month he/she stays employed, and why that filter LastDate Date?

 

1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

The LASTDATE filter will adjust the filter context to only return a single date. COUNTROWS then calculates the employee count for that date (month).

View solution in original post

13 REPLIES 13
TomMartens
Super User
Super User

Next what is the initial question?
I think @AlexisOlson already answered the question.

 

Due to the nature of headcount (it's called a semi-additive measure) and the type of the fact table. Because employees are counted every month this fact table has to be considered a snapshot fact table.

 

Assuming that an employee enters on january, this employee will be counted three times (jan, feb, and mar). But this doesn't mean that there are 3 employees at the end of Q1.

 

The measure

 

cnt of employees =
CALCULATE(
COuNTROWS('employeetable'),
LASTDATE('calendar'[date])
)

 

only counts the rows for the after the filter modifier of the CALCULATE are applied.

This means the headcount is filtered down to the 31st of March. Using LASTDATE prevents double counting for semiadditive measures like Headcount.

 

This article explains some of the intircate workings of LASTDATE: https://dax.guide/lastdate/

This article by Jeffrey Wang, one of the inventors of DAX explains what makes dates special: http://mdxdax.blogspot.com/2011/01/dax-time-intelligence-functions.html

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

I am not allowed to attach any files, but perhaps i can paste some snapshots.

 

First, the original excel files imported into PBI desktop:

YoungLearning_0-1723621906360.png

YoungLearning_1-1723621919616.png

 


1) Calculated column Headcount Count and measure last date[date] (given answer)

Headcount Count = CALCULATE(COUNTROWS(Headcount), LASTDATE(Date_X[Date]))
last date[date] = LASTDATE(DATE[DATE])

YoungLearning_5-1723622445072.png

 

 

2 ) Measure Headcount_Count_new and measure last date [date]

 

YoungLearning_4-1723622378050.png

 


3) Calculated column Headcount Count and measure last headcount [date]

YoungLearning_6-1723622598542.png

 

Headcount Count = CALCULATE(COUNTROWS(Headcount), LASTDATE(Headcount[Date]))
last headcount [date] = LASTDATE(Headcount[DATE])


4) Measure Headcount_Count_new and measure last headcount [date]

YoungLearning_7-1723622754114.png



After using slicer to select, say quarter 4 it basically shows the same table visuals.

 

What would you do?

 

 

TomMartens
Super User
Super User

Hey @YoungLearning ,

 

please provide the pbix that you use, this enables us to be on the same page.

 

"Sum of Headcount Count" doesn't look like a measure is used.

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany
AlexisOlson
Super User
Super User

The LASTDATE filter will adjust the filter context to only return a single date. COUNTROWS then calculates the employee count for that date (month).

None of this though, seems to specify about returning the lastdate value in a specified month. How does this DAX function work for this use case? Thanks for your reply...

LASTDATE returns the latest date within whatever filter context is set, so if there is a slicer or a filter specifying a month, you get the last date in that month.

I have tested it out and I kinda see how it works, on a visual level. But does DAX know that the last date of the month is e.g. 31st of December? How does using different date columns of different table affect the count, how does it know without using like Distinct Count or something?

It's referencing a date table which should have all dates you need, so it's just a max over the dates in the date context.

fyi when i did this just to test it out... why is it like this?

MeasureLast = LASTDATE(Date_X[Date])

YoungLearning_0-1723568999852.png

 

Hey @YoungLearning ,

 

as @AlexisOlson mentioned LASTDATE returns the last date of the filter context. The date hierarchy present in the table view is "contributing" to the filter context of the cell that shows the result of your LASTDATE measure. Each cell has a different filter context, for this reason the measure returns a different value in each cell. If you remove the day column from the table you will see that LASTDATE will return the last date of the month.

 

Regards,

Tom 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

Yes, seems like it. But back to the original question now. Why not the Headcount[date] column, why Date[date]? This columns have a relationship.

It wants to count the number of employees, year, month and quarter basis. COUNTROWS definitely double count if I do it by year or quarter.

YoungLearning_0-1723604180520.png

example data

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.