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

Rolling SUM with Datesbetween where Initial Date is blank - Error

Hello,

I have one measure which is the following:

Margin = DIVIDE(CALCULATE([Margin],KEEPFILTERS('Program'[Model]="Y")),[Margin PM],BLANK())


And I made two rolling sum measures, one using a SUMX and the other SUM.

RollingMargin(SUM) = var between = 
DATESBETWEEN( 
        'Date'[Date],
DATEADD(FIRSTDATE('Date'[Date]),-6,MONTH),
        LASTDATE (PREVIOUSMONTH('Date'[Date])))

return
CALCULATE([Margin],between)
RollingMargin(SUMX) = 
var between = 
DATESBETWEEN( 
        'Date'[Date],
DATEADD(FIRSTDATE('Date'[Date]),-6,MONTH),
        LASTDATE (PREVIOUSMONTH('Date'[Date])))
return CALCULATE(SUMX(VALUES('Date'[MonthID]),[Margin]),between)


As displayed in the image below, given there is no data in January, using the SUM with datesbetween yields only null values, I assume because the range includes a non-existing Date as 1st parameter, or maybe cause of the base measure is using DIVIDE (which I changed to alternate result as 0 and got the same result as below, but instead of blank, zeros).

The 2nd Table is using a 1 Month interval.
If I would use a 12 Month interval then for SUM everything would be blank
Is there a way to avoid this? I would like to use SUM as it yields results faster.

forum.PNG

Thank you!




 

5 REPLIES 5
amitchandak Super Contributor
Super Contributor

Re: Rolling SUM with Datesbetween where Initial Date is blank - Error

In case of non-continuous date prefer calculation like the one given below 

 

Var _last_year= (max('Date'[Date Filer]))-365
Var   _This_year=year(max('Date'[Date Filer]))
Var _min_last_year= (maxx('Date',STARTOFYEAR(DATEADD('Date'[Date Filer],-12,MONTH))))
Var   _min_This_year=year(max(STARTOFYEAR('Date'[Date Filer])))

Var _last_year_val= CALCULATE(sum(Sales[Sales Amount]),(Sales[Sales Date])<=_last_year && (Sales[Sales Date]) >=_min_last_year)
Var   _This_year_val =CALCULATE(sum(Sales[Sales Amount]),(Sales[Sales Date])<=_This_year && (Sales[Sales Date]) >=_min_This_year)
return
(_This_year_val-_last_year_val)/_last_year_val*100

 

lcbdax Frequent Visitor
Frequent Visitor

Re: Rolling SUM with Datesbetween where Initial Date is blank - Error

My Margin formula its a SUM of a column, did I mistake up top.

Margin = DIVIDE(CALCULATE(SUM(Table[Values]),KEEPFILTERS('Program'[Model]="Y")),[Margin PM],BLANK())

v-lid-msft Super Contributor
Super Contributor

Re: Rolling SUM with Datesbetween where Initial Date is blank - Error

Hi @lcbdax ,


To use Time intelligence function, Continuous time is necessary for most time. you can try to generate a calendar table and using in your DAX.

 

Date = CALENDAR(MIN('Table'[Date]),MAX('Table'[Date]))

19.PNG

 

The two formulas seems have same result on my side, Could you please provide more details about it?

 

18.PNG

 

 

 

BTW, pbix as attached.

 

Best regards,

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

v-lid-msft Super Contributor
Super Contributor

Re: Rolling SUM with Datesbetween where Initial Date is blank - Error

Hi @lcbdax ,

 

How about the result after you follow the suggestions mentioned in my original post?Could you please provide more details about it If it doesn't meet your requirement?

 

Best regards,

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

luiscb Member
Member

Re: Rolling SUM with Datesbetween where Initial Date is blank - Error

Hi again,

Sorry for the late response. You used a static value for Margin PM.
Use this formula instead and you will get the same issue I am talking about:

Margin PM = CALCULATE(SUM('Table'[Values]),PREVIOUSMONTH('Date'[Date]))
I think this is only possible via Sumx. 
Thanks!

Helpful resources

Announcements
October 2019 Community Highlights

October 2019 Community Highlights

October was a busy month in the community. Read the recap article to learn about some of the events and content.

New Solution Badges

New Solution Badges

Two waves of brand new solution badges are coming! Read the article for more information on our new community badges.

Ask Amir Anything

Exclusive LIVE Community Event No. 2 – Ask Amir Anything

Next in our Triple A series: Ask Amir Netz questions about the latest updates, features and future.

Analytics in Azure virtual event

Analytics in Azure virtual event

Experience a limitless analytics service built to ingest, prep, manage, and serve data for immediate use in Power BI.

Users Online
Currently online: 436 members 3,724 guests
Please welcome our newest community members: