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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Angads
Advocate I
Advocate I

Need to sync Year Slicer with YTD,MTD,QTD.

I have this DAX as below.


YTD_MTD_QTD 1 =

VAR today = TODAY()

VAR start_year = CALCULATE(STARTOFYEAR('Calendar'[Date]), YEAR('Calendar'[Date]) = YEAR(TODAY()))

VAR start_quarter = CALCULATE(STARTOFQUARTER('Calendar'[Date]), YEAR('Calendar'[Date]) = YEAR(TODAY()), QUARTER('Calendar'[Date]) = QUARTER(TODAY()))

VAR start_month = CALCULATE(STARTOFMONTH('Calendar'[Date]), YEAR('Calendar'[Date]) = YEAR(TODAY()), MONTH('Calendar'[Date]) = MONTH(TODAY()))

 

VAR result =

    UNION(

        ADDCOLUMNS(

            FILTER('Calendar', 'Calendar'[Date] >= start_year && 'Calendar'[Date] <= today),

            "selection", "YTD"

        ),

        ADDCOLUMNS(

            FILTER('Calendar', 'Calendar'[Date] >= start_quarter && 'Calendar'[Date] <= today),

            "selection", "QTD"

        ),

        ADDCOLUMNS(

            FILTER('Calendar', 'Calendar'[Date] >= start_month && 'Calendar'[Date] <= today),

            "selection", "MTD"

        )

    )

 

RETURN result


Also I have a Year slicer.


The above dax is only working for current year. If I select other years this is not working. Need help.
Help with dax!

1 REPLY 1
technolog
Super User
Super User

To make it work with the Year slicer, you need to adjust the DAX to take into account the selected year from the slicer instead of always using the current year.

First, let's get the selected year from the slicer. You can do this by using the MAX or MIN function (assuming single select on the slicer) on the 'Calendar'[Date] column. Let's call this selectedYear.

VAR selectedYear = YEAR(MAX('Calendar'[Date]))

Now, replace all instances of YEAR(TODAY()) in your DAX with selectedYear.

So, the start_year, start_quarter, and start_month variables should look like:

VAR start_year = CALCULATE(STARTOFYEAR('Calendar'[Date]), YEAR('Calendar'[Date]) = selectedYear)

VAR start_quarter = CALCULATE(STARTOFQUARTER('Calendar'[Date]), YEAR('Calendar'[Date]) = selectedYear, QUARTER('Calendar'[Date]) = QUARTER(TODAY()))

VAR start_month = CALCULATE(STARTOFMONTH('Calendar'[Date]), YEAR('Calendar'[Date]) = selectedYear, MONTH('Calendar'[Date]) = MONTH(TODAY()))

With these changes, the DAX will now consider the year selected in the slicer when calculating YTD, QTD, and MTD. So, if you select a different year in the slicer, the DAX should adjust accordingly and give you the desired results for that year.

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

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! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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

Top Solution Authors