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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
JustinDoh1
Post Prodigy
Post Prodigy

Filter by pure month (not relative date) using measure

I am sharing my Pbix file here.

 

I am trying to apply filter on month to display data of (1) current month,  (2) 1 month prior,  (3) 2 month prior etc.

So, for example (today is 10/7/2021), if we are talking about 1 month prior, it would be 9/1 - 9/30, not relative dates like 9/6 - 10/6.

 

I looked online for example:

The closest that I found was using a measure like this:

(source1, source2 )

 

last_month_data =
var current_month= MONTH(TODAY())
       return
          CALCULATE(SUM('Table1'[Data]),
                                           FILTER(
                                                      'Table1',
                                                      MONTH('Table1'[Month]) = current_month -1
                                                   )
                                      )

 

On my Pbx file, date field is called "IncidentDate".

 

Bottom is screenshot of my visuals:

JustinDoh1_0-1633642878414.png

On these visuals, I am using relative date like below. but that is not what I want.

For 2 month prior, it should be 8/1/2021 - 8/31/2021.

JustinDoh1_0-1633643805468.png

 

 

How do I apply the logic into filter?

 

I guess I am stuck here:

How do I apply measure [Falls Count] into sum?

JustinDoh1_0-1633643653128.png

OR..

JustinDoh1_0-1633650482415.png

It appears that second screenshot (measure) seems to be working (I need to double check), but I am curious why I have to break down the measure "Falls Count" into this

JustinDoh1_0-1633650658664.png

to make it work..

 

Thanks.

 

2 ACCEPTED SOLUTIONS
mahoneypat
Employee
Employee

You can just add this column to your Dates table to get the relative months from Today, and then add that column to your visual with is -1 and is -2.

 

Months From Today = DATEDIFF(Dates[Date], TODAY(), MONTH)

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

AllisonKennedy
Super User
Super User

@JustinDoh1 

 

I have added my Date table - I recommend deleting your Dates table to avoid confusion of too many tables. You need to make sure to mark as date table, whichever option you choose: https://excelwithallison.blogspot.com/search?q=dimdate 

 

I also created a DimHour table to simplify a bit, this means you don't need to create those calculated columns in the Fact table. 

 

Here's what it looks like now: 

 

AllisonKennedy_0-1633651320885.png

 

Measure pattern is:

 

Falls Count -1 Month =
VAR _Today = MAX(DimDate[Date])
VAR _NumberMonths = -1
var _StartDate = EOMONTH(_Today, _NumberMonths)
VAR _Result =
CALCULATE(
[Falls Count],
DATESINPERIOD(DimDate[Date], _StartDate, -1, MONTH)
)

RETURN
_Result
 
Note: You could get the -1 Month using the relative date filter, just choose Calendar month: 
AllisonKennedy_1-1633651378655.png

But that won't work for 2 month prior as it will include both 1 and 2 months prior, thus the need for the DAX I gave above.

 

See attached updated file below signature. 😀


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

View solution in original post

6 REPLIES 6
AllisonKennedy
Super User
Super User

@JustinDoh1 

 

I have added my Date table - I recommend deleting your Dates table to avoid confusion of too many tables. You need to make sure to mark as date table, whichever option you choose: https://excelwithallison.blogspot.com/search?q=dimdate 

 

I also created a DimHour table to simplify a bit, this means you don't need to create those calculated columns in the Fact table. 

 

Here's what it looks like now: 

 

AllisonKennedy_0-1633651320885.png

 

Measure pattern is:

 

Falls Count -1 Month =
VAR _Today = MAX(DimDate[Date])
VAR _NumberMonths = -1
var _StartDate = EOMONTH(_Today, _NumberMonths)
VAR _Result =
CALCULATE(
[Falls Count],
DATESINPERIOD(DimDate[Date], _StartDate, -1, MONTH)
)

RETURN
_Result
 
Note: You could get the -1 Month using the relative date filter, just choose Calendar month: 
AllisonKennedy_1-1633651378655.png

But that won't work for 2 month prior as it will include both 1 and 2 months prior, thus the need for the DAX I gave above.

 

See attached updated file below signature. 😀


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

@AllisonKennedy 

I have a question. I see that you included a slicer that selects Year and Month. I am trying to make the current month as a default value (more dynamic) instead of choosing the value from this filter. How do I that? I guess I do not need this filter at all.  Thanks. 

 

@JustinDoh1 


@JustinDoh1 wrote:

@AllisonKennedy 

I have a question. I see that you included a slicer that selects Year and Month. I am trying to make the current month as a default value (more dynamic) instead of choosing the value from this filter. How do I that? I guess I do not need this filter at all.  Thanks. 

 


 

Correct, you could use Relative Date slicer on the date column and choose this month - that will be dynamic, or use Page/All Pages level filter in the filter pane.

 

AllisonKennedy_0-1633729678138.png

 


Please @mention me in your reply if you want a response.

Copying DAX from this post? Click here for a hack to quickly replace it with your own table names

Has this post solved your problem? Please Accept as Solution so that others can find it quickly and to let the community know your problem has been solved.
If you found this post helpful, please give Kudos C

I work as a Microsoft trainer and consultant, specialising in Power BI and Power Query.
www.excelwithallison.com

mahoneypat
Employee
Employee

You can just add this column to your Dates table to get the relative months from Today, and then add that column to your visual with is -1 and is -2.

 

Months From Today = DATEDIFF(Dates[Date], TODAY(), MONTH)

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


@mahoneypat I think your solution totally makes sense to me. Going back to my struggle with coming up with measure, is there any solution? Thanks.

You should be able to just use a measure expression like this.  Is that what you mean?

 

NewMeasure =
VAR vThisMonthIndex =
    MIN ( Date[Months From Today] )
RETURN
    CALCULATE (
        [YourMeasure],
        ALL ( Date ),
        Date[Months From Today] = vThisMonthIndex - 1
    )

 

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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