cancel
Showing results for
Did you mean:

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a \$100 discount. Register Now

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.

 ID Date Cal. Column 1 1 2 4/15/2021 3 3/20/2020 4 5/3/2021 1 5 6/2/2021 1 6 4/28/2021

Thank you -

1 ACCEPTED SOLUTION
Resident Rockstar

@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 ()
)
``````

Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!

4 REPLIES 4
Resident Rockstar

@lilych - Try:

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

Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!

Helper II

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?

Resident Rockstar

@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 ()
)
``````

Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!

Helper II

Thank you @ChrisMendoza, that worked!

Announcements

#### Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

#### Power BI Monthly Update - February 2024

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

#### Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

#### Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors