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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
KrisD
Helper II
Helper II

Use different calculations in line graph based on year in x-axis

Hi!

 

I have written a somewhat lengthy measure which uses one of two values available in a slicer to produce values for a line graph.

I do however not want to perform this particular calculation on years other than the current. What I want is to have a different calculation take place if the year is not the current. This is where I've gotten a bit stuck. 

The main calculation:

 

Accident frequency =
VAR MultiplicationFactor = 10^6
VAR AverageLastYear = CALCULATE ( DIVIDE ( [Number of hours worked], 12 ),
ALL ( Period ),
Period[Offset Year] = -1 )
VAR SlicerValue = SELECTEDVALUE ( 'Slicer'[Value] )
SWITCH (
TRUE(),
SlicerValue = "Include current month",
DIVIDE ( SUM ( 'Fact Accidents'[Number off accidents with absence] ), ( [Worked hours] ), 0 ),

SlicerValue = "Exclude current month",
DIVIDE (
SUM ( 'Fact Accidents'[Number off accidents with absence] ), ( [Worked hours] - AverageLastYear ), 0 ) ) )
 
VAR Result = Frequency * MultiplicationFactor

RETURN
 
Result

 

Img.PNG

 

Is it possible to simultaneously have this calculation take place ONLY for current year, and use another calulation for previous years?

1 ACCEPTED SOLUTION
v-jingzhang
Community Support
Community Support

Hi @KrisD 

Yes, it is possible. Assume you have two measures, MeasureA for current year and MeasureB for previous years. You can create a measure like below and use it in the line chart. You can use different calculation in MeasueA and MeasureB according to your needs.

MeasureSample =
VAR currentYear = YEAR ( TODAY () )
RETURN
    IF ( SELECTEDVALUE ( 'Table'[Year] ) = currentYear, [MeasureA], [MeasureB] )

 

Regards,
Community Support Team _ Jing
If this post helps, please Accept it as the solution to help other members find it.

View solution in original post

2 REPLIES 2
v-jingzhang
Community Support
Community Support

Hi @KrisD 

Yes, it is possible. Assume you have two measures, MeasureA for current year and MeasureB for previous years. You can create a measure like below and use it in the line chart. You can use different calculation in MeasueA and MeasureB according to your needs.

MeasureSample =
VAR currentYear = YEAR ( TODAY () )
RETURN
    IF ( SELECTEDVALUE ( 'Table'[Year] ) = currentYear, [MeasureA], [MeasureB] )

 

Regards,
Community Support Team _ Jing
If this post helps, please Accept it as the solution to help other members find it.

Great! I used CurrentYear to check what year it was in the visual, but failed to use SelectedValue which was the missing component to achieve my goal. Your input is much appreciated! 

 

The final result (if anyone in the future should ask a similar question in this forum):

 

VAR FrequencyExcl = IF (
SlicerValue = "Exclude current month" && SELECTEDVALUE ( Period[Year] ) = CurrentYear,
DIVIDE (
SUM ( 'Fact Accidents'[# of accidents with absence] ), ( [Worked Hours] ), 0 ),
DIVIDE (
SUM ( 'Fact Accidents'[# of accidents with absence] ), ( [Worked Hours] ) ) )

VAR FrequencyIncl = IF (
SlicerValue = "Include current month" && SELECTEDVALUE ( Period[Year] ) = CurrentYear,
DIVIDE (
SUM ( 'Fact Accidents'[# of accidents with absence] ), ( [Worked Hours] - AverageLastYear ), 0 ) )
 
VAR Result = IF (
FrequencyExcl, FrequencyExcl * MultiplicationFactor,
IF ( FrequencyIncl, FrequencyIncl * MultiplicationFactor ) )

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.