cancel
Showing results for 
Search instead for 
Did you mean: 

Fabric is Generally Available. Browse Fabric Presentations. Work towards your Fabric certification with the Cloud Skills Challenge.

Reply
lilych
Helper II
Helper II

DAX to count rows where date is null or in last 2 months

Hello,

 

I'm struggling with DAX. I am trying to create a calculated column that count rows in a table where the date is either blank OR within the past 2 calendar months based on today's date. How would I write out the DAX for this? The main part I am struggling with is if the date is within the current month or previous month.

 

IDDateCal. Column
1 1
24/15/2021 
33/20/2020 
45/3/20211
56/2/20211
64/28/2021 

 

Thank you -

1 ACCEPTED SOLUTION

@lilych - Ah! I see what you mean. You can do as an IF:

Column =
IF (
    AND (
        MONTH ( TableName[Date] )
            > ( MONTH ( TODAY () ) - 2 ),
        YEAR ( TableName[Date] ) = YEAR ( TODAY () )
    )
        || ISBLANK ( TableName[Date] ),
    1,
    BLANK ()
)

or you can do as SWITCH:

SWITCHColumn = 
SWITCH (
    TRUE (),
    ISBLANK ( TableName[Date] ), 1,
    AND (
        MONTH ( TableName[Date] )
            > ( MONTH ( TODAY () ) - 2 ),
        YEAR ( TableName[Date] ) = YEAR ( TODAY () )
    ), 1,
    BLANK ()
)

image.png






Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



View solution in original post

4 REPLIES 4
ChrisMendoza
Resident Rockstar
Resident Rockstar

@lilych - Try:

Column = 
IF(
    MONTH(TableName[Date]) > (MONTH(TODAY()) - 2)
    || ISBLANK(TableName[Date]),
    1,
    BLANK()
)

image.png






Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



Thanks @ChrisMendoza! It works for dates in this year where it excludes rows from Jan-April 2021, however in my file, it seems to be including rows where the date is in 2020. How can I exclude that? 

@lilych - Ah! I see what you mean. You can do as an IF:

Column =
IF (
    AND (
        MONTH ( TableName[Date] )
            > ( MONTH ( TODAY () ) - 2 ),
        YEAR ( TableName[Date] ) = YEAR ( TODAY () )
    )
        || ISBLANK ( TableName[Date] ),
    1,
    BLANK ()
)

or you can do as SWITCH:

SWITCHColumn = 
SWITCH (
    TRUE (),
    ISBLANK ( TableName[Date] ), 1,
    AND (
        MONTH ( TableName[Date] )
            > ( MONTH ( TODAY () ) - 2 ),
        YEAR ( TableName[Date] ) = YEAR ( TODAY () )
    ), 1,
    BLANK ()
)

image.png






Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



Thank you @ChrisMendoza, that worked!

Helpful resources

Announcements
PBI November 2023 Update Carousel

Power BI Monthly Update - November 2023

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

Community News

Fabric Community News unified experience

Read the latest Fabric Community announcements, including updates on Power BI, Synapse, Data Factory and Data Activator.

Power BI Fabric Summit Carousel

The largest Power BI and Fabric virtual conference

130+ sessions, 130+ speakers, Product managers, MVPs, and experts. All about Power BI and Fabric. Attend online or watch the recordings.

Top Solution Authors
Top Kudoed Authors