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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

% change with random dates

I have some workout data that includes a number of fitness tests and I want to be able to calculate progress in these tests. The tests don't take place at regular intervals and there could be any number of them. 

 

The sample data and some code is below. I want the % change, in this case it would be (125-107)/107 = 16.8% but I'm getting -62.5% with the calculation below. From what I can work out the current FTP and previousDatewithFTP are calculating correctly, but the previous FTP isn't.

 

Any ideas what is going wrong?

 

Sample data:

DateTitleAvg. Watts
02/05/202020 min FTP Test Ride107
17/10/202020 min FTP Test Ride125

 

Code:

%FTP change =
VAR currentFTP =
CALCULATE (
SUM ( workouts[Avg. Watts] ),
FILTER (
ALL ( workouts), SEARCH ( "FTP Test Ride", workouts[Title],, BLANK () ) > 0 && workouts[Date] = MAX ( workouts[Date] )
)
)
VAR previousDatewithFTP =
CALCULATE (
MAX ( workouts[Date] ),
FILTER (
ALL ( workouts ), SEARCH ( "FTP Test Ride", workouts[Title],, BLANK () ) > 0 && workouts[Date] < MAX ( workouts[Date] )
)
)
VAR previousFTP =
CALCULATE (
SUM ( workouts[Avg. Watts] ),
FILTER (
ALL ( workouts ),
workouts[Date] = previousDatewithFTP
)
)
RETURN
DIVIDE ( currentFTP - previousFTP, previousFTP, 0 )

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Thank you - I tried the above but still got the same issue. Managed to fix it in the end by making the filter for the previousFTP calculation the same as the others (adding the bit in red):

 

VAR previousFTP =
CALCULATE (
SUM ( workouts[Avg. Watts] ),
FILTER (
ALL ( workouts ), SEARCH ( "FTP Test Ride", workouts[Title],, BLANK () ) > 0 &&
workouts[Date] = previousDatewithFTP
)
)

 

Thank you for the advice!

View solution in original post

4 REPLIES 4
lbendlin
Super User
Super User

try this version:

 

%FTP change =
VAR currentFTP =
CALCULATE (
SUM ( workouts[Avg. Watts] ),
FILTER (
ALL ( workouts), SEARCH ( "FTP Test Ride", workouts[Title],, BLANK () ) > 0 && workouts[Date] = MAX ( workouts[Date] )
)
)

VAR CD = MAX(workouts[Date])
VAR previousDatewithFTP =
CALCULATE (
MAX ( workouts[Date] ),
FILTER (
ALL ( workouts ), SEARCH ( "FTP Test Ride", workouts[Title],, BLANK () ) > 0 && workouts[Date] < CD
)
)
VAR previousFTP =
CALCULATE (
SUM ( workouts[Avg. Watts] ),
FILTER (
ALL ( workouts ),
workouts[Date] = previousDatewithFTP
)
)
RETURN
DIVIDE ( currentFTP - previousFTP, previousFTP, 0 )

Anonymous
Not applicable

Thank you - I tried the above but still got the same issue. Managed to fix it in the end by making the filter for the previousFTP calculation the same as the others (adding the bit in red):

 

VAR previousFTP =
CALCULATE (
SUM ( workouts[Avg. Watts] ),
FILTER (
ALL ( workouts ), SEARCH ( "FTP Test Ride", workouts[Title],, BLANK () ) > 0 &&
workouts[Date] = previousDatewithFTP
)
)

 

Thank you for the advice!

lbendlin
Super User
Super User

What's the reason for using SEARCH ?  Do you have scenarios where you want to compare activities where the title is not exactly the same?  Please provide more context.

Anonymous
Not applicable

The wider dataset has many other activities with different titles, but it's only the ones with "FTP Test ride" in the title that I want to compare. 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.