cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
JakubHa Frequent Visitor
Frequent Visitor

Last 3 Months Slicer

Hi,

I`m trying to create a Slicer with Last Month, Last 3 months and Overall.

I tried one Dax formula described here: Power BI – Time Period Slicer for Last 7 Days,Last 30 Days.. – Analytics Avenue

DatePeriod =
UNION (
ADDCOLUMNS( SUMMARIZE( CALCULATETABLE('Date' , DATESBETWEEN('Date'[Date],today()-07+1,today()) ), 'Date'[Date]),"Period","Last 07 Days") ,
ADDCOLUMNS( SUMMARIZE( CALCULATETABLE('Date' , DATESBETWEEN('Date'[Date],today()-14+1,today()) ), 'Date'[Date]),"Period","Last 14 Days") ,
ADDCOLUMNS( SUMMARIZE( CALCULATETABLE('Date' , DATESBETWEEN('Date'[Date],today()-30+1,today()) ), 'Date'[Date]),"Period","Last 30 Days") ,
ADDCOLUMNS( SUMMARIZE( CALCULATETABLE('Date' , DATESBETWEEN('Date'[Date],today()-90+1,today()) ), 'Date'[Date]),"Period","Last 90 Days") ,
ADDCOLUMNS( SUMMARIZE( CALCULATETABLE('Date'), 'Date'[Date]),"Period","Overall")
)


But it doesn`t work for me. My dates are formated as only M+Y (no days):

01.01.2015 00:00:00
01.02.2015 00:00:00


Do you know any solution or workaround?

Many thanks,
Jakub

12 REPLIES 12
Moderator v-sihou-msft
Moderator

Re: Last 3 Months Slicer

@JakubHa

 

I suggest you add a column for previous 3 months day in Query Editor.

 

Date.AddMonths(DateTime.Date(DateTime.LocalNow()),-3)

 

Capture.PNG

 

Then add a column to tage if the dates is within 3 months.

 

If Within 3 months =
IF (
    Table[Date]
        >= DATE ( YEAR ( Table[Previous 3 months Day] ), MONTH ( Table[Previous 3 months Day] ), 1 )
        && Table[Date] <= DATE ( YEAR ( TODAY () ), MONTH ( TODAY () ), 1 ),
    1,
    0
)

Now you can use above column to filter your table.

 

 

Regards,

JakubHa Frequent Visitor
Frequent Visitor

Re: Last 3 Months Slicer

Hi @v-sihou-msft,

 

thanks for your reply! It seems to work fine for last 3 months only. Is there any way to extend this field into last month, last 3 months, last 6 months and all months?

 

Many thanks for your support,

Jakub

Pfister Established Member
Established Member

Re: Last 3 Months Slicer

Hi @JakubHa @v-sihou-msft

Add a new column in Power Query (Query Editor):

= Table.AddColumn(YOURLASTSTEP, "MonthDiff", each 
(Date.Year([Date])-1)*12+Date.Month([Date])
-
((Date.Year(DateTime.LocalNow())-1)*12+
Date.Month(DateTime.LocalNow()))
)

The code calculates the differenz between today and the date in your column in months.

Then you'll use this new column as a slicer.

Hope this helps.

Greetings Hp Pfister
Power Query Buch online auf http://www.powerbi-pro.com
JakubHa Frequent Visitor
Frequent Visitor

Re: Last 3 Months Slicer

Hello @Pfister,

 

I`ve got this error: "Expression.Error: The name 'YOURLASTSTEP' wasn't recognized.  Make sure it's spelled correctly."

 

Appreciate your help,

Jakub

Pfister Established Member
Established Member

Re: Last 3 Months Slicer

@JakubHa

YOURLASTSTEP is a placeholder.

Change it to the name of the last step in your Query.

Greetings Hp Pfister
Power Query Buch online auf http://www.powerbi-pro.com
ibarrau Established Member
Established Member

Re: Last 3 Months Slicer

When i need to filter last periods i usually add a column in dax like this:

DateFilter = IF(Sales[Date] >= DATE(YEAR(DATEADD(LASTDATE(ALL(TableDate[Date])) , -4 , MONTH)),
MONTH(DATEADD(LASTDATE(ALL(TableDate[Date])) , -4 , MONTH)),
1),
"Last four months",
IF (AND(
YEAR(Sales[Date]) = YEAR(NOW()),
MONTH(Sales[Date]) = MONTH(NOW())
),
"Last month",
"More than 4 months"
)

 

With the logic you can add different ways. The example contains last 4 months and last month.

 

Regards,

P/D I hope the DAX is okay I didn't have time to run it.

JakubHa Frequent Visitor
Frequent Visitor

Re: Last 3 Months Slicer

Hi @ibarrau,

 

the problem is my periods can overlap each other.

 

I would like to have last month, last 3 months, last 6 months and overall. I think tough your formula won`t apply there Smiley Sad 

ibarrau Established Member
Established Member

Re: Last 3 Months Slicer

Did you try the relative date slicer of the new release of power bi? It is a preview feature you have to activate in power bi desktop. That is the only aproach to have only one column specify as you need.

 

You can read more in the release notes:

https://powerbi.microsoft.com/en-us/blog/power-bi-desktop-may-feature-summary/#relativeDateSlicer

 

Regards, hope this help or you may need more columns I think.

JakubHa Frequent Visitor
Frequent Visitor

Re: Last 3 Months Slicer

Yes, it`s working acceptably but I wanted to build more flexible slicer based on data I am providing. For instance, no day drill option when I have only month-based data. By relative date slicer it`s not possible.

 

thank you for your ideas anyway!

Jakub