Forum Discussion

starry's avatar
starry
Frequent Visitor
3 years ago
Solved

Dax - count values for missing months

Hi All I am new to Power BI and trying to make my head around DAX I have the following data.  I need to make a measure to reflect the number of students and teachers in every reported month and be...
  • Jihwan_Kim's avatar
    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
            )
        )