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

Data between dates (slider). selection minus 1 year not working

Hi, 

I am trying to make a comparison of Revenue between the selected period (slider) and the same period last year. I have tried this in a couple difrent ways now, but i seem to keep getting errors. I made a difrend table so that there can be no filters interfering with the data. This is the code i am trying at the moment:

OmzetVorigJaar = 
var minDate = CALCULATE(MIN(revArticleRevenue[Factuurdatum]), ALLSELECTED(revArticleRevenue[Factuurdatum]))
var maxDate = CALCULATE(MAX(revArticleRevenue[Factuurdatum]), ALLSELECTED(revArticleRevenue[Factuurdatum]))

var prefYear = YEAR(minDate)-1
var prefMinDate = DATE(prefYear , MONTH(minDate), DAY(minDate))
var prefMaxDate = DATE(prefYear , MONTH(maxDate), DAY(maxDate))

var datesBetweenPref = DATESBETWEEN(cvDailyRevenue[Factuurdatum], prefMinDate, prefMaxDate)

var revenue = CALCULATE(
   SUM('cvDailyRevenue'[DagOmzet]),
   FILTER('cvDailyRevenue', 'cvDailyRevenue'[Factuurjaar] = prefYear),
   FILTER('cvDailyRevenue', 'cvDailyRevenue'[Factuurdatum] in datesBetweenPref)
)

RETURN revenue

 at first i did not use prefYear but Year(minDate) - 1 this seemed to not always work.

If i use 2 static dates in the between like : 

var datesBetweenPref = DATESBETWEEN(cvDailyRevenue[Factuurdatum], 01-01-2018, 31-12-2018)
it works fine. but this does ofc not let me compare periods set by the slider.

I Also tried it this way:
var revenue = CALCULATE(
   SUM('cvDailyRevenue'[DagOmzet]),
   FILTER('cvDailyRevenue', 'cvDailyRevenue'[Factuurjaar] = prefYear),
   FILTER('cvDailyRevenue', 'cvDailyRevenue'[Factuurdatum] >= prefMinDate),
   FILTER('cvDailyRevenue', 'cvDailyRevenue'[Factuurdatum] <= prefMaxDate)
)
this again returns an error, but with the static dates it works.

The error i am getting with this is:

"MdxScript(Model) (610, 19) Calculation error in measure 'revArticleRevenue'[OmzetVorigJaar]: An argument of function 'DATE' has the wrong data type or the result is too large or too small."

1 ACCEPTED SOLUTION

Accepted Solutions
JorPowerViking Frequent Visitor
Frequent Visitor

Re: Data between dates (slider). selection minus 1 year not working

i found that Power Bi has a harder job working with Dates etc. therfore i made it so that powerapps can work with ints. In the view i returned the day of the year and the year number as an int. Then the following changes to the code where made: 

OmzetVorigJaar = 
var minDate = CALCULATE(MIN(revArticleRevenue[Factuurdatum]), ALLSELECTED(revArticleRevenue[Factuurdatum]))
var maxDate = CALCULATE(MAX(revArticleRevenue[Factuurdatum]), ALLSELECTED(revArticleRevenue[Factuurdatum]))

var dayOfYearMin = DATEDIFF(DATE(YEAR(minDate), 1, 1), minDate, DAY ) + 1
var dayOfYearMax = DATEDIFF(DATE(YEAR(minDate), 1, 1), maxDate, DAY ) + 1


var revenue = CALCULATE(
   SUM('cvDailyRevenue'[DagOmzet]),
   FILTER('cvDailyRevenue', 'cvDailyRevenue'[Factuurjaar] = YEAR(minDate)-1),
   FILTER('cvDailyRevenue', 'cvDailyRevenue'[Factuurdag] >= dayOfYearMin),
   FILTER('cvDailyRevenue', 'cvDailyRevenue'[Factuurdag] <= dayOfYearMax)
)

RETURN revenue

View solution in original post

1 REPLY 1
JorPowerViking Frequent Visitor
Frequent Visitor

Re: Data between dates (slider). selection minus 1 year not working

i found that Power Bi has a harder job working with Dates etc. therfore i made it so that powerapps can work with ints. In the view i returned the day of the year and the year number as an int. Then the following changes to the code where made: 

OmzetVorigJaar = 
var minDate = CALCULATE(MIN(revArticleRevenue[Factuurdatum]), ALLSELECTED(revArticleRevenue[Factuurdatum]))
var maxDate = CALCULATE(MAX(revArticleRevenue[Factuurdatum]), ALLSELECTED(revArticleRevenue[Factuurdatum]))

var dayOfYearMin = DATEDIFF(DATE(YEAR(minDate), 1, 1), minDate, DAY ) + 1
var dayOfYearMax = DATEDIFF(DATE(YEAR(minDate), 1, 1), maxDate, DAY ) + 1


var revenue = CALCULATE(
   SUM('cvDailyRevenue'[DagOmzet]),
   FILTER('cvDailyRevenue', 'cvDailyRevenue'[Factuurjaar] = YEAR(minDate)-1),
   FILTER('cvDailyRevenue', 'cvDailyRevenue'[Factuurdag] >= dayOfYearMin),
   FILTER('cvDailyRevenue', 'cvDailyRevenue'[Factuurdag] <= dayOfYearMax)
)

RETURN revenue

View solution in original post

Helpful resources

Announcements
Can You Solve These Challenge

Challenge: Can You Solve These?

Find out how to participate in the first Power BI 'Can You Solve These?' challenge.

Community News & Announcements

Community News & Announcements

Get your latest community news and announcements.

Virtual Launch Event

Microsoft Business Applications October Virtual Launch Event

Join us for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

Community Kudopalooza

Win Power BI Swag with Community Kudopalooza!

Each week, complete activities and be qualified in the drawing for cool Power BI Swag.

Top Kudoed Authors
Users Online
Currently online: 60 members 1,343 guests
Please welcome our newest community members: