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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Tapojoy
New Member

How to create a measure to calculate percentage change based on date range slicer ?

Hi everyone ! @PowerBIDev 
I have a time-series line plot showing values for last 90 days and have a date range slicer, based on which the chart gets displayed.

I need to create a new measure that is able to calculate the percentage change based on the date range selected through the slicer.

 

The time-series line plot is as shown below :

Tapojoy_1-1604730785022.png


For example, if the date range in the slicer is selected as :  19/08/2020 - 17/10/2020
the percentage change measure should be calculated as :-
[ ( Value on 17/10/2020 - Value on 19/08/2020 ) / Value on 19/08/2020 ] X 100 %.

 

I tried a DAX code, but getting the following error :

Tapojoy_0-1604730713171.png

 

Please suggest any solutions or redirect to any other link, if already answered.

Thanks in advance ! 😄

1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi @Tapojoy 

The reason for the error is that the Filter arguments provided to CALCULATE must either be tables or appropriate boolean expressions that refer to a single column.

 

Since MIN and MAX functions are scalar functions, they don't return tables, so startdate and enddate aren't valid filter arguments.

 

You can fix this issue by by using FIRSTDATE and LASTDATE, which return tables containing a single row/column corresponding to the min/max date. I think the order of arguments in DIVIDE also needs to be switched, and there was an unnecessary CALCULATE:

 

perc_change =
VAR startdate =
    FIRSTDATE ( daily_count[date] )
VAR enddate =
    LASTDATE ( daily_count[date] )
RETURN
    DIVIDE (
        CALCULATE ( [CumCount], enddate ),
        CALCULATE ( [CumCount], startdate )
    ) - 1

 

 

Also, I would advise creating a separate Date dimension table to use for all date filtering (various articles on this online).

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

4 REPLIES 4
Tapojoy
New Member

Thanks a lot ! It works now.

OwenAuger
Super User
Super User

Hi @Tapojoy 

The reason for the error is that the Filter arguments provided to CALCULATE must either be tables or appropriate boolean expressions that refer to a single column.

 

Since MIN and MAX functions are scalar functions, they don't return tables, so startdate and enddate aren't valid filter arguments.

 

You can fix this issue by by using FIRSTDATE and LASTDATE, which return tables containing a single row/column corresponding to the min/max date. I think the order of arguments in DIVIDE also needs to be switched, and there was an unnecessary CALCULATE:

 

perc_change =
VAR startdate =
    FIRSTDATE ( daily_count[date] )
VAR enddate =
    LASTDATE ( daily_count[date] )
RETURN
    DIVIDE (
        CALCULATE ( [CumCount], enddate ),
        CALCULATE ( [CumCount], startdate )
    ) - 1

 

 

Also, I would advise creating a separate Date dimension table to use for all date filtering (various articles on this online).

 

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

For this, how would it work if I wanted the Start Date to be the SELECTEDVALUE and the end date being the previous month based on the Start Date?

ie: Start Date = Jun 23 so End Date = May 23

Anonymous
Not applicable

Greetings ,
I have a slightly different problem.

I have one card displaying the percentage change based on date selection and
2 date slicers (Between and Relative Date) on the same page with the card and
I have two different algorithms for % change for dates selected by Between Date Slicer and Relative Date Slicer separately.

Is there a way to detect what type of date slicer is selected, so this way I can switch to the right algorithm based on the date slicer selection?
If not then,
How to calculate % Change based on dates from any type of date slicer?

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Fabric Monthly Update - May 2024

Check out the May 2024 Fabric update to learn about new features.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.