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

The FabCon + SQLCon recap series starts April 14th at 8am Pacific. If you’re tracking where AI is going inside Fabric, this first session is a can't miss. Register now

Reply
StenX
Frequent Visitor

Using DATEADD() and cross-filtering

Hi All !

 we have 2 simple datasets + Calendar:

 

StenX_0-1638562540526.png

our screen:

 

StenX_1-1638562689446.png

 

Our steps:

 

1. choice Products in slicer Prod or leave it unselected
2. choice month in slicer Month (one or more)
3. Measure [back_month] - contains the number of selected months

 

back_month = (DATEDIFF(MIN('calendar'[Date]),MAX('calendar'[Date]),MONTH)+1)*-1

 

 

4. Measure [count_filter] - contains the total number of filtered rows in the txt-table for the selected months (point 2) and Products (point 1)

5.  The problem in the [count_back].

Measure [count_back] - must containts the total number filtered rows in the txt-table for [back_month] months ago.

 

 

 

count_back = 
VAR back_month = (DATEDIFF(MIN('calendar'[Date]),MAX('calendar'[Date]),MONTH)+1)*-1
VAR count_back=CALCULATE([count_filter],DATEADD('calendar'[Date],back_month,MONTH),ALL())
RETURN count_back

 

You can see the expected date-intervals for filtering [count_back] in Measures [date_backFr] & [date_backLs]

 

date_backFr = FIRSTDATE(DATEADD('calendar'[Date],[back_month],MONTH))
date_backLs = LASTDATE(DATEADD('calendar'[Date],[back_month],MONTH))

 

 

 

For example,
if we chose the Month interval: (2021-03) & (2021-04), then in the Measure [count_back] we must calculated the number of rows in txt for the previous month-intervals (-2 months) => (2021-01) & (2021-02) 

if we chose the Month interval: (2021-03), then in the Measure [count_back] we must calculated the number of rows in txt for the previous month-interval (-1 month) => (2021-02)

 

 


At now, the measure [count_back] work correctly only if we do not use the Prod filter.

If we turn Propuct filter is on, then everything breaks down ((
How to use additional filtering by Prod in this case?

 

PBIX file: http://ftp.kantartns.ru/download/637ycdxjeb216eym2vw72b13q0jf73/tt.zip

Regards,

 

 

1 ACCEPTED SOLUTION
StenX
Frequent Visitor

I was modify my formula (using PARALLELPERIOD ) and it worked!

 

Nprev = 
VAR monthBack = (DATEDIFF(MIN('rsp'[date]),MAX('rsp'[date]),MONTH)+1)*-1
VAR xStart = STARTOFMONTH(PARALLELPERIOD(rsp[date],monthBack,MONTH))
VAR xEnd = ENDOFMONTH(PARALLELPERIOD(rsp[date],monthBack,MONTH))
VAR cntx = CALCULATE(COUNTROWS(txt),FILTER(ALL(rsp[date],rsp[month]),rsp[date]>=xStart && rsp[date]<=xEnd))

RETURN cntx

View solution in original post

3 REPLIES 3
amitchandak
Super User
Super User

@StenX , Try like

 

same period based on date range
Last Period =
var _max =maxx(date,date[date])
var _min =maxx(date,date[date])
var datediff1 = datediff(_min,_max,day)
var _maxX = _max-datediff1
var _minX = _min -datediff1
return
CALCULATE(SUM(Sales[Sales Amount]),filter(all(date),date[date]<=_maxX &&date[date]>=_minX)))

 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Thank you for your quick response!
My apologies for the delay in reply. Our office was closed for the weekend ((

I applied your idea to calculate for a displacement of the full month(s). 

 

 

count_MonthBack = 
VAR xDiffMonthBack = (DATEDIFF(MIN('calendar'[Date]),MAX('calendar'[Date]),MONTH)+1)*-1
VAR xStartBack = STARTOFMONTH(DATEADD('calendar'[Date],xDiffMonthBack,MONTH))
VAR xEndBack = ENDOFMONTH(DATEADD('calendar'[Date],xDiffMonthBack,MONTH))
VAR xCountBack=CALCULATE([cnt],FILTER(all(rsp),rsp[date]>=xStartBack && rsp[date]<=xEndBack))
RETURN xCountBack

 

 


Unfortunately, I couldn't do it. If we use the slicer Prod, everything breaks.
Perhaps the problem is in the DATEADD(). How can this be fixed for month metrics?

 

For check:

if we select the month 2021-04, then the date of the week should be filtered by 01.03.2021-31.03.2021 (one month)

if we select the month 2021-04&2021-03, then the date of the week should be filtered by 01.01.2021-28.02.2021 (two months)

 

Regards,

StenX
Frequent Visitor

I was modify my formula (using PARALLELPERIOD ) and it worked!

 

Nprev = 
VAR monthBack = (DATEDIFF(MIN('rsp'[date]),MAX('rsp'[date]),MONTH)+1)*-1
VAR xStart = STARTOFMONTH(PARALLELPERIOD(rsp[date],monthBack,MONTH))
VAR xEnd = ENDOFMONTH(PARALLELPERIOD(rsp[date],monthBack,MONTH))
VAR cntx = CALCULATE(COUNTROWS(txt),FILTER(ALL(rsp[date],rsp[month]),rsp[date]>=xStart && rsp[date]<=xEnd))

RETURN cntx

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.