Forum Discussion
Dax - count values for missing months
- 3 years ago
Hi,
I am not sure how your desired outcome looks like, but please check the below picture and the attahed pbix file whether it suits your requirement.
Number of Students: = VAR _lastinformationdate = MAXX ( FILTER ( ALL ( 'Calendar' ), 'Calendar'[DateKey] = CALCULATE ( MAX ( Data[Date Reported] ), REMOVEFILTERS ( 'Calendar' ) ) ), 'Calendar'[Date] ) VAR _lastnonblankdate = MAXX ( FILTER ( ADDCOLUMNS ( FILTER ( ALL ( 'Calendar'[Date] ), 'Calendar'[Date] <= MAX ( 'Calendar'[Date] ) ), "@studentcount", CALCULATE ( SUM ( Data[Number Of Students] ) ) ), [@studentcount] <> BLANK () ), 'Calendar'[Date] ) RETURN IF ( MIN ( 'Calendar'[Date] ) <= _lastinformationdate && HASONEVALUE ( 'Calendar'[Month-Year] ), CALCULATE ( SUM ( Data[Number Of Students] ), 'Calendar'[Date] = _lastnonblankdate ) )Number of teachers: = VAR _lastinformationdate = MAXX ( FILTER ( ALL ( 'Calendar' ), 'Calendar'[DateKey] = CALCULATE ( MAX ( Data[Date Reported] ), REMOVEFILTERS ( 'Calendar' ) ) ), 'Calendar'[Date] ) VAR _lastnonblankdate = MAXX ( FILTER ( ADDCOLUMNS ( FILTER ( ALL ( 'Calendar'[Date] ), 'Calendar'[Date] <= MAX ( 'Calendar'[Date] ) ), "@teacherscount", CALCULATE ( SUM ( Data[Number Of Teachers] ) ) ), [@teacherscount] <> BLANK () ), 'Calendar'[Date] ) RETURN IF ( MIN ( 'Calendar'[Date] ) <= _lastinformationdate && HASONEVALUE ( 'Calendar'[Month-Year] ), CALCULATE ( SUM ( Data[Number Of Teachers] ), 'Calendar'[Date] = _lastnonblankdate ) )
Thank you Jihwan_Kim , you've got a perfect result with your solution, however when I tried to use your code it didn't work for me. I am trying to understand why..
What is the logic behind this code line?
FILTER (
ALL ( 'Calendar' ),
'Calendar'[DateKey]
= CALCULATE ( MAX ( Data[Date Reported] ), REMOVEFILTERS ( 'Calendar' ) )
),
You have used DateKey and Date Reported to make relationship between Calendar and Data tables.
What I've done is, I have created Date Table with autocalendar function, changed Date Reported in the fact (your Data) table to date type and made relationship between Date Table [Date] and fact table [Date Reported]. So my code looks like
Hi,
Thank you for your feedback.
Please share your sample pbix file's link, and then I can try to look into it.
Thank you.