Starting December 3, join live sessions with database experts and the Microsoft product team to learn just how easy it is to get started
Learn moreGet 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
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!
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.
Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.
User | Count |
---|---|
24 | |
21 | |
18 | |
14 | |
11 |
User | Count |
---|---|
44 | |
35 | |
25 | |
22 | |
22 |