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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
jfish29
Regular Visitor

DAX expression to filter column for dates only in last month?

Hey everyone,

 

I am trying to generate a measure that filters a column with dates for those which are only from the last month or last 30 days.

 

I would like to use this in a calcuate function to see if the amount of rows which have the date in the last month and which meet antoher critera are greater than 0. 

 

Thanks!

1 ACCEPTED SOLUTION
v-jianboli-msft
Community Support
Community Support

Hi @jfish29 ,

 

Here is the DAX to calculate the number of records with a status of closed in the last thirty days:

 

Count record =

CALCULATE (

    COUNT ( 'Table'[status] ),

    FILTER (

        'Table',

        [status] = "Closed"

            && [report date] <= TODAY ()

            && [report date]

                >= ( TODAY () - 30 )

    )

)

 

If you just want to know if the result is greater than 0, you can use this DAX

 

result =

VAR _c =

    CALCULATE (

        COUNT ( 'Table'[status] ),

        FILTER (

            'Table',

            [status] = "Closed"

                && [report date] <= TODAY ()

                && [report date]

                    >= ( TODAY () - 30 )

        )

    )

RETURN

IF ( _c > 0, "Yes", "No" )

 

Final output:

vjianbolimsft_0-1657014869610.png

 

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

7 REPLIES 7
v-jianboli-msft
Community Support
Community Support

Hi @jfish29 ,

 

Here is the DAX to calculate the number of records with a status of closed in the last thirty days:

 

Count record =

CALCULATE (

    COUNT ( 'Table'[status] ),

    FILTER (

        'Table',

        [status] = "Closed"

            && [report date] <= TODAY ()

            && [report date]

                >= ( TODAY () - 30 )

    )

)

 

If you just want to know if the result is greater than 0, you can use this DAX

 

result =

VAR _c =

    CALCULATE (

        COUNT ( 'Table'[status] ),

        FILTER (

            'Table',

            [status] = "Closed"

                && [report date] <= TODAY ()

                && [report date]

                    >= ( TODAY () - 30 )

        )

    )

RETURN

IF ( _c > 0, "Yes", "No" )

 

Final output:

vjianbolimsft_0-1657014869610.png

 

Best Regards,

Jianbo Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

jfish29
Regular Visitor

Hey, thanks for getting back!

 

I proabably should have offered more context about the column I want to look through. It has dates from when projects are closed, and it may not have a date within it from the last month, so I do not think the MAX function would work here?

 

So, it has a bunch of dates of projects which are closed, and then a placeholder value (Sunday, December 31, 1899) for all projects which are still open. Again, the goal would be to see if say, "June" is present in any cell in the column for this month. 

Hi @jfish29 

this a measure. MAX will only retrieve the current month visible in the filter context. Not sure what are slicing by. A screenshot of your visual will be of a great help to understand better. 

The visual is a picture that gets switched by the SWITCH function depending on if the condition is met. It seems like the DAX function has to do all the calcualtions without leveraging a filter?

Hi @jfish29 

can you join a zoom meeting to explain further. I sent you meeting request on private message 

Here is some sample data:

 

We have a "report date" column which is the column i want to filter for only those dates in the past month.

 

Tuesday, January 18, 2022

Thursday, February 17, 2022

Sunday, December 31, 1899

Sunday, December 31, 1899

Tuesday, June 14, 2022

 

^ Above is the formatt of the dates in this column, the one from 1899 is a placeholder value for unfinished projects.

 

We have another column to filter by, called "status"

 

Closed

Open

 

^ This column holds these values

 

It seems to me that I need a switch function, which checks if is it is true that, there are > 0 projects within the last month that are closed. 

 

I am using the "Image by Cloudscope" as the visualization which holds my images. It holds their values as text which I had formatted as image urls. 

 

 

tamerj1
Super User
Super User

Hi @jfish29 

I have no data to work with but this shall not be very complex

first you need to retrieve the dates in the past month:

VAR CurrentMonth = MAX ( 'Date'[Month] ) --Preferred "year month key" to avoid the previous year problem. 

VAR PreviousMonthDates =
CALCULATE LTABLE (

VALUES ( TableName[Date] ),

'Date'[Month] = CurrentMonth - 1,

REMOVEFILTERS ( )

)

then you need to extract the day number of each of the available dates. 
VAR T1 = 

ADDCOLUMNS ( 

PreviousMonthDates,

"@DayNum", DAY ( [Date] )

)

then select the day number column

VAR PreviousDays =
SELECTCOLUMNS ( T1, "Day Number", [@DayNum] )

then you can calculate for example you sales amount

CALCULATE (

SUM ( TableName[Sales] ),

FILTER ( 'Date', DAY ( 'Date'[Date] ) IN PreviousDays ),

Other filters and/or calculate modifiers as required 

)

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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