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
Nicpet0
New Member

Comparing values with the same date range

Hi Community

I would like to get some help with a DAX measure. I want to have a bar chart like shown in the snippet, where i compare values from previous years, but my struggle is to have the same date range as my current year (in this case 2024/25) so in a month slicer i have selected september. this means that i have data from september 1st til september 17th. But my previous years shows the full month of september. How can i make sure it always follows the same range? how would a DAX measure look like to achieve this?

Nicpet0_0-1726655790171.png

 

1 ACCEPTED SOLUTION
v-xianjtan-msft
Community Support
Community Support

Hi @Nicpet0 

 

Here's my solution, hope it helps!

1. Create a calculated column in the calendar table that marks whether each date falls within the same date range for the current year.

 

IsInCurrentYearRange = 
VAR CurrentYear = YEAR(MAX('Table'[Date]))
VAR CurrentMonth = MONTH(MAX('Table'[Date]))
VAR MaxCurrentDate = DAY(MAX('Table'[Date]))
RETURN 
IF(
    'Calendar'[Year] < CurrentYear &&
    'Calendar'[Month] == CurrentMonth &&
    'Calendar'[Day] > MaxCurrentDate,
    0,
    1
)

 

2. Create a measure to calculate revenue in the same date range.

 

SameDateRangeRevenue = 
CALCULATE(
    SUM('Table'[Value]),
    FILTER(
        'Calendar',
        'Calendar'[IsInCurrentYearRange] = 1
    )
)

 

3. Here is final result.

vxianjtanmsft_0-1726824899747.png

vxianjtanmsft_1-1726824955749.png

 

Best Regards,
Jarvis Tang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

2 REPLIES 2
v-xianjtan-msft
Community Support
Community Support

Hi @Nicpet0 

 

Here's my solution, hope it helps!

1. Create a calculated column in the calendar table that marks whether each date falls within the same date range for the current year.

 

IsInCurrentYearRange = 
VAR CurrentYear = YEAR(MAX('Table'[Date]))
VAR CurrentMonth = MONTH(MAX('Table'[Date]))
VAR MaxCurrentDate = DAY(MAX('Table'[Date]))
RETURN 
IF(
    'Calendar'[Year] < CurrentYear &&
    'Calendar'[Month] == CurrentMonth &&
    'Calendar'[Day] > MaxCurrentDate,
    0,
    1
)

 

2. Create a measure to calculate revenue in the same date range.

 

SameDateRangeRevenue = 
CALCULATE(
    SUM('Table'[Value]),
    FILTER(
        'Calendar',
        'Calendar'[IsInCurrentYearRange] = 1
    )
)

 

3. Here is final result.

vxianjtanmsft_0-1726824899747.png

vxianjtanmsft_1-1726824955749.png

 

Best Regards,
Jarvis Tang
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

123abc
Community Champion
Community Champion

Step 1: Create a measure that captures the latest date selected in the slicer for the current year:

MaxCurrentYearDate =
CALCULATE(
MAX('Calendar'[Date]),
'Calendar'[Year] = MAX('Calendar'[Year])
)

 

Step 2: Calculate Previous Years up to the Same Date
Now create another measure to calculate the revenue for previous years but only up to the maximum date of the current year:

 

RevenueSamePeriod =
CALCULATE(
SUM('Sales'[Revenue]),
FILTER(
ALL('Calendar'),
'Calendar'[Year] < MAX('Calendar'[Year]) &&
'Calendar'[MonthDay] <= DAY([MaxCurrentYearDate])
)
)

 

  1. In this measure:

    • ALL('Calendar') removes the date filter context so that it applies the logic across all years.
    • 'Calendar'[Year] < MAX('Calendar'[Year]) ensures it only applies to previous years.
    • 'Calendar'[MonthDay] <= DAY([MaxCurrentYearDate]) limits the comparison to the same number of days as in the current year.
  2. Step 3: Apply this Measure in Your Bar Chart
    Use the new RevenueSamePeriod measure for the previous years, so the bar chart will only display values up to the same date range as in the current year.

By using this approach, each year's data will be restricted to the same range of days as in the current year, ensuring a fair comparison across different years based on the slicer selection.

 

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!

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.