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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
starry
Frequent Visitor

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 between reported months on a graph.  (Assumption: number of students do not change between reported months) Will appreciate any help with this Dax expression, thank you!

So for example for Big the numbers for 2018/01/01  and 2018/02/01 should be the same as for 2017/12/01. Currently, I am getting 0 on the graph.

 

Schoolnumber of studentsnumber of teachersDate Reported
Big60320171001
Big80420171101
Big80420171201
Big80520180301
Large120720180501
Large130720180601
Hefty140820181001
Hefty145820181201
Hefty150820190302

 

 

 

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

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.

 

Jihwan_Kim_0-1666754569727.png

 

 

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

 

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

View solution in original post

4 REPLIES 4
Jihwan_Kim
Super User
Super User

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.

 

Jihwan_Kim_0-1666754569727.png

 

 

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

 

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

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.. 

 

starry_0-1666820848411.png

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

 

Number of teachers: =
VAR _lastinformationdate =
MAXX (
FILTER (
ALL ( 'Date' ),
'Date'[Date]
= CALCULATE ( MAX ( 'fact'[DateReported]), REMOVEFILTERS ( 'Date' ) )
),
'Date'[Date]
 
 
Second issue was, as I have created Date table with Calendarauto function I don't have column Month-Year. I have used column Date instead in the code and it doesn't work.
 
 
RETURN
IF (
MIN ( 'Date'[Date] ) <= _lastinformationdate
&& HASONEVALUE ( 'Date'[Date] ),
CALCULATE (
SUM ( fact[Number Of Teachers] ),
'Date'[Date] = _lastnonblankdate
 

 

 

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.


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.
WanderingBI
Resolver III
Resolver III

Hi,

 

I realize you are asking for a solution with DAX.

 

Considering that you call yourself a beginner, I would suggest to instead transform the data and introduce a calendar table in Power Query.

 

Under the assumption that data is only reported once a year (so that the day information can be ignored) i would do it like this:

 

1) Create a Calendar table that has all the Years and Months in scope (i.e. 2017-01, 2017-02,...)

2) In your table, use extract to create two columns for Year and Month (i.e. Year 2017, Month 01)

3) Create a merged column (YYYY-MM) in both queries

4) Duplicate the Calendar query

5) Merge the newly created query with your data query

6) Sort descending by the YYYY-MM

7) Use the fill down function to fill all months with data that have no data originally

😎double check if this yields the desired results

 

Personally i feel this approach better suits the general logic of doing things in Power BI, I'm curious however if others can provide an elegant solution in DAX.

 

It is also good to know that all the calculations and transformations you do in Power Query only need to calculated once (at the time of the refresh). If you implement this in DAX, then basically every time a user interacts with visuals/slicers/etc. everything outside of the data model will be computed again.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.