{"id":1954,"date":"2025-02-20T07:02:51","date_gmt":"2025-02-20T07:02:51","guid":{"rendered":"https:\/\/mailitics.com\/index.php\/2025\/02\/20\/advanced-time-intelligence-in-dax-with-performance-in-mind\/"},"modified":"2025-02-20T07:02:51","modified_gmt":"2025-02-20T07:02:51","slug":"advanced-time-intelligence-in-dax-with-performance-in-mind","status":"publish","type":"post","link":"https:\/\/mailitics.com\/index.php\/2025\/02\/20\/advanced-time-intelligence-in-dax-with-performance-in-mind\/","title":{"rendered":"Advanced Time Intelligence in DAX with Performance in Mind"},"content":{"rendered":"<p>    Advanced Time Intelligence in DAX with Performance in Mind<br \/>\n \t<BR><br \/>\n<BR><\/BR><br \/>\n    <!-- no image --><br \/>\n \t<BR><br \/>\n<BR><\/BR><\/p>\n<div>\n<p class=\"wp-block-paragraph\">We all know the usual Time Intelligence function based on years, quarters, months, and days. But\u00a0sometimes, we need to perform more exotic timer intelligence calculations. But we should not forget to consider performance while programming the measures.\u00a0<\/p>\n<h2 class=\"wp-block-heading\">Introduction\u00a0<\/h2>\n<p class=\"wp-block-paragraph\">There are many <a href=\"https:\/\/towardsdatascience.com\/tag\/dax\/\" title=\"Dax\">Dax<\/a> functions in Power BI for Time Intelligence Measures.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">The most common are:\u00a0<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\"><code>&lt;a href=\"https:\/\/dax.guide\/dateadd\/\"&gt;DATEADD()&lt;\/a&gt;\u00a0<\/code><\/li>\n<li class=\"wp-block-list-item\">\n<a href=\"https:\/\/dax.guide\/datesmtd\/\">DATESMTD()<\/a>, <a href=\"https:\/\/dax.guide\/datesqtd\/\">DATESQTD()<\/a> and <a href=\"https:\/\/dax.guide\/datesytd\/\">DATESYTD()<\/a> and the shortcut functions <a href=\"https:\/\/dax.guide\/totalmtd\/\">TOTALMTD()<\/a>, <a href=\"https:\/\/dax.guide\/totalqtd\/\">TOTALQTD()<\/a> and <a href=\"https:\/\/dax.guide\/totalytd\/\">TOTALYTD()<\/a>\u00a0<\/li>\n<li class=\"wp-block-list-item\">\n<a href=\"https:\/\/dax.guide\/parallelperiod\/\">PARALLELPERIOD()<\/a>\u00a0<\/li>\n<li class=\"wp-block-list-item\">\n<a href=\"https:\/\/dax.guide\/previousday\/\">PREVIOUSDAY()<\/a>, <a href=\"https:\/\/dax.guide\/previousmonth\/\">PREVIOUSMONTH()<\/a>, <a href=\"https:\/\/dax.guide\/previousquarter\/\">PREVIOUSQUARTER(<\/a>), and <a href=\"https:\/\/dax.guide\/previousyear\/\">PREVIOUSYEAR()<\/a>\n<\/li>\n<li class=\"wp-block-list-item\">\n<a href=\"https:\/\/dax.guide\/sameperiodlastyear\/\">SAMEPERIODLASTYEAR()<\/a>\u00a0<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\">You can find a comprehensive list of Time Intelligence functions here: <a href=\"https:\/\/dax.guide\/functions\/time-intelligence\/\">Time Intelligence \u2013 DAX Guide<\/a>. These functions cover the most common cases.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">However, some requirements cannot be easily covered with these functions. And here we are.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">I want to cover some of these cases I encountered in my projects, which include:\u00a0<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">Last n Periods and some variants\u00a0<\/li>\n<li class=\"wp-block-list-item\">How to cope with Leap years\u00a0<\/li>\n<li class=\"wp-block-list-item\">Week-to-Date calculations\u00a0<\/li>\n<li class=\"wp-block-list-item\">Calculating Weekly sums\u00a0<\/li>\n<li class=\"wp-block-list-item\">Fiscal Week YTD\u00a0<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\">I will show you how to use an extended date table to support these scenarios and improve efficiency and performance.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">Most Time-Intelligence functions work regardless of whether the Fiscal Year is aligned with the calendar year. One exception is Year-to-Date (YTD).\u00a0<\/p>\n<p class=\"wp-block-paragraph\">For such cases, look at the DATESYTD() function mentioned above. There, you will find the optional parameter to pass the last day of the Fiscal year.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">The last case will cover calculations based on weeks, while the Fiscal year doesn\u2019t align with the calendar year.\u00a0<\/p>\n<h2 class=\"wp-block-heading\">Scenario\u00a0<\/h2>\n<p class=\"wp-block-paragraph\">I will use the well-known ContosoRetailDW data model.<\/p>\n<p class=\"wp-block-paragraph\">The Base Measure is Sum Online Sales, which has the following code:\u00a0<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">Sum Online Sales = SUMX('Online Sales',\n\u00a0( 'Online Sales'[UnitPrice]\n\u00a0\u00a0          * 'Online Sales'[SalesQuantity] )\u00a0\n                         - 'Online Sales'[DiscountAmount] )<\/code><\/pre>\n<p class=\"wp-block-paragraph\">I will work almost exclusively in <a href=\"https:\/\/www.sqlbi.com\/tools\/dax-studio\/\">DAX-Studio<\/a>, which provides the Server Timing function to analyze the performance of the DAX code. In the References section below, you can find a link to an article about how to collect and interpret performance data in DAX Studio.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">This is the base query used in my examples to get some data from the data model:\u00a0<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">EVALUATE \n CALCULATETABLE( \n SUMMARIZECOLUMNS('Date'[Year] \n ,'Date'[Month Short Name] \n,'Date'[Week] \n,'Date'[Date] \n,\"Online Sales\", [Sum Online Sales] \n) \n ,'Product'[ProductCategoryName] = \"Computers\" ,'Product'[ProductSubcategoryName] = \"Laptops\" \n,'Customer'[Continent] = \"North America\" \n ,'Customer'[Country] = \"United States\"  ,'Customer'[State\/Province] = \"Texas\" )\n<\/code><\/pre>\n<p class=\"wp-block-paragraph\">In most examples, I will remove some filters to get more complete data (for each day).\u00a0<\/p>\n<h2 class=\"wp-block-heading\">Date table\u00a0<\/h2>\n<p class=\"wp-block-paragraph\">My date table includes a relatively large number of additional columns.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">In the references section below, you can find some articles written by SQLBI, on building weekly related calculations, including creating a date table to support these calculations.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">As described in my article about date tables referenced below, I have added the following columns:\u00a0<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">Index or Offset columns to count the days, weeks, months, quarters, semesters, and years from the current date.\u00a0<\/li>\n<li class=\"wp-block-list-item\">Flag columns to mark the current day, week, month, quarter, semester, and year based on the current date.\u00a0<\/li>\n<li class=\"wp-block-list-item\">This and the previous columns require a daily recalculation to ensure the correct date is used\u00a0as the reference date.\u00a0<\/li>\n<li class=\"wp-block-list-item\">Start- and End-Dates of each week and month (Add more if needed).\u00a0<\/li>\n<li class=\"wp-block-list-item\">Start- and End-Dates for the Fiscal Year.\u00a0<\/li>\n<li class=\"wp-block-list-item\">Previous year dates to include the start and end dates of the current period. This is especially interesting for weeks, as the start- and end dates of the weeks are not the same from year to year.\u00a0<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\">As you will see, I will use these columns extensively to simplify my calculations.<\/p>\n<p class=\"wp-block-paragraph\">In addition, we will use the Calendar Hierarchy to calculate the needed results at different levels of\u00a0the hierarchy.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">A complete Calendar hierarchy contains either:\u00a0<\/p>\n<ol class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">Year\u00a0<\/li>\n<li class=\"wp-block-list-item\">Semester\u00a0<\/li>\n<li class=\"wp-block-list-item\">Quarter\u00a0<\/li>\n<li class=\"wp-block-list-item\">Month\u00a0<\/li>\n<li class=\"wp-block-list-item\">Day\u00a0<\/li>\n<\/ol>\n<p class=\"wp-block-paragraph\">Or\u00a0<\/p>\n<ol class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">Year\u00a0<\/li>\n<li class=\"wp-block-list-item\">Week\u00a0<\/li>\n<li class=\"wp-block-list-item\">Day\u00a0<\/li>\n<\/ol>\n<p class=\"wp-block-paragraph\">If the Fiscal Year doesn\u2019t align with the Calendar year, I built the Hierarchy with the Fiscal Year instead of the Calendar Year.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">Then, I added a separate FiscalMonthName column and a FiscalMonthSort column to ensure that the first month of the fiscal year was shown first.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">OK, let\u2019s start with the first case.\u00a0<\/p>\n<h2 class=\"wp-block-heading\">Last n periods\u00a0<\/h2>\n<p class=\"wp-block-paragraph\">This scenario calculates the rolling sum of values over the past n periods.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">For example, for each day, we want to get the Sales for the last 10 days:\u00a0<\/p>\n<figure class=\"wp-block-image size-large is-resized\"><img data-recalc-dims=\"1\" data-dominant-color=\"e0e3e5\" data-has-transparency=\"true\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"634\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-11.40.18%25E2%2580%25AFAM-1024x634.png?resize=1024%2C634&#038;ssl=1\" alt=\"\" class=\"wp-image-598127 has-transparency\" style=\"--dominant-color: #e0e3e5; width:582px;height:auto\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-11.40.18\u202fAM-1024x634.png 1024w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-11.40.18\u202fAM-300x186.png 300w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-11.40.18\u202fAM-768x475.png 768w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-11.40.18\u202fAM.png 1050w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\"><figcaption class=\"wp-element-caption\"><em>Figure 1 \u2013 Example for the sum over the last 10 days (Figure by the Author)\u00a0<\/em><\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">Here is the Measure I came up with:\u00a0<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">Online Sales (Last 10 days) = \n CALCULATE (\n [Sum Online Sales] \n ,DATESINPERIOD ( \n 'Date'[Date], \nMAX ( 'Date'[Date] ), \n-10, \nDAY \n ) \n ) <\/code><\/pre>\n<p class=\"wp-block-paragraph\">When executing the query filtering for Computers and North America, I get this result: <\/p>\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" data-dominant-color=\"e3e3e5\" data-has-transparency=\"true\" style=\"--dominant-color: #e3e3e5;\" loading=\"lazy\" decoding=\"async\" width=\"540\" height=\"348\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-11.42.19%25E2%2580%25AFAM.png?resize=540%2C348&#038;ssl=1\" alt=\"\" class=\"wp-image-598128 has-transparency\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-11.42.19\u202fAM.png 540w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-11.42.19\u202fAM-300x193.png 300w\" sizes=\"auto, (max-width: 540px) 100vw, 540px\"><figcaption class=\"wp-element-caption\"><em>Figure 2 \u2013 Last 10 days \u2013 Result of Measure (Figure by the Author)<\/em><\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">If I look at the server timings, the result is not bad:\u00a0<\/p>\n<figure class=\"wp-block-image aligncenter is-resized\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXcibj9TX2MPwXZkLifW2GA9Pc_0sXdZoF-xKlcebbNaQoooERUFe16UI9PaybXgV_iqsgqUytvK1jQ-ski9o8RxpXNM02ZEEC285aXSQzccGL_cjji3S57QJsuaCIjwNVQKgu4PwQ?key=UZ99HMJ_TyEY5euXsrcZKGZ7\" alt=\"\" style=\"width:164px;height:auto\"><figcaption class=\"wp-element-caption\"><em>Figure 3 \u2013 Server timings for the last 10 days Measure (Figure by the Author)\u00a0<\/em><\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">As you can see, the Storage engine performs more than half of the work, which is a good sign. It\u2019s not\u00a0perfect, but as the execution time is less than 100 ms, it\u2019s still very good from the performance point of view.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">This approach has one crucial issue:<\/p>\n<p class=\"wp-block-paragraph\">When calculating the rolling sum over multiple months, you must know that this approach is date oriented.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">This means that when you look at a specific time, it goes back to the same day of the given month. For example:\u00a0<\/p>\n<p class=\"wp-block-paragraph\">We look at January 12. 2024, and we want to calculate the rolling sum over the last three months. The starting date for this calculation will be November 13. 2023.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">When do we want to get the rolling sum for the entire month?\u00a0<\/p>\n<p class=\"wp-block-paragraph\">In the case above, I want to have as the starting date November 1, 2023.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">For this case, we can use the MonthIndex column.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">Each column has a unique index based on the current date.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">Therefore, we can use it to go back three months and get the entire month.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">This is the DAX Code for this:\u00a0<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">Online Sales rolling full 3 months = \n VAR CurDate = \n MAX ( 'Date'[Date] ) \n VAR CurMonthIndex = \n MAX ( 'Date'[MonthIndex] ) \n VAR FirstDatePrevMonth = \n CALCULATE ( \n MIN ( 'Date'[Date] ), \n REMOVEFILTERS ( 'Date' ), \n 'Date'[MonthIndex] = CurMonthIndex - 2 \n ) \n RETURN \n CALCULATE ( \n [Sum Online Sales], \n DATESBETWEEN ( \n 'Date'[Date], \nFirstDatePrevMonth, \nCurDate \n ) \n )<\/code><\/pre>\n<p class=\"wp-block-paragraph\">The execution is still quick, but it\u2019s less efficient, as most of the calculations cannot be performed by the Storage engine:<\/p>\n<figure class=\"wp-block-image aligncenter is-resized\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXfBlmh8jKSd02mFprWGtUyZWhKDVf4LdALF_SUcghTwSrt9osiarB5xsrALXMZoUK3bNKH_Nez7fwpjAvJvzx1H7lZdwzeq7gdrGzVKRcI_KBy2TUAVnUD_9ziZ7JYQr-1N4thmSg?key=UZ99HMJ_TyEY5euXsrcZKGZ7\" alt=\"\" style=\"width:145px;height:auto\"><figcaption class=\"wp-element-caption\"><em>Figure 4 \u2013 Server timings for the rolling sum of the last three full months (Figure by the Author) <\/em>As you can see, it is not as fast as before.\u00a0<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">I tried other approaches (for example, <code>'Date'[MonthIndex] &gt;= CurMonthIndex \u2013 2 &amp;&amp;<\/code> <code>'Date'[MonthIndex] &lt;= CurMonthIndex)<\/code>, but these approaches were worse than this one.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">Here is the result for the same logic, but for the last two months (To avoid showing too many rows):<\/p>\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" data-dominant-color=\"e7e8e7\" data-has-transparency=\"true\" style=\"--dominant-color: #e7e8e7;\" loading=\"lazy\" decoding=\"async\" width=\"605\" height=\"672\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-11.45.25%25E2%2580%25AFAM.png?resize=605%2C672&#038;ssl=1\" alt=\"\" class=\"wp-image-598129 has-transparency\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-11.45.25\u202fAM.png 605w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-11.45.25\u202fAM-270x300.png 270w\" sizes=\"auto, (max-width: 605px) 100vw, 605px\"><figcaption class=\"wp-element-caption\"><em>Figure 5 \u2013 Results for the last two whole months (Figure by the Author)\u00a0<\/em><\/figcaption><\/figure>\n<h2 class=\"wp-block-heading\">Regarding Leap Years\u00a0<\/h2>\n<p class=\"wp-block-paragraph\">The leap year problem is odd, which is evident when calculating the previous year for each day. Let me explain:\u00a0<\/p>\n<p class=\"wp-block-paragraph\">When I execute the following Query to get the last days of February for the years 2020 and 2021:\u00a0<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">EVALUATE \nCALCULATETABLE ( \n SUMMARIZECOLUMNS ( \n 'Date'[Year], \n 'Date'[Month Short Name], \n 'Date'[MonthKey],\n 'Date'[Day Of Month], \n \"Online Sales\", [Sum Online Sales], \n \"Online Sales (PY)\", [Online Sales (PY)] \n ), \n 'Date'[Year] IN {2020, 2021}, \n 'Date'[Month] = 2, \n 'Date'[Day Of Month] IN {27, 28, 29}, \n 'Customer'[Continent] = \"North America\", \n 'Customer'[Country] = \"United States\" \n) \n ORDER BY 'Date'[MonthKey], \n 'Date'[Day Of Month]<\/code><\/pre>\n<p class=\"wp-block-paragraph\">I get the following result:\u00a0<\/p>\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" data-dominant-color=\"ece9e9\" data-has-transparency=\"true\" style=\"--dominant-color: #ece9e9;\" loading=\"lazy\" decoding=\"async\" width=\"547\" height=\"159\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-11.47.10%25E2%2580%25AFAM.png?resize=547%2C159&#038;ssl=1\" alt=\"\" class=\"wp-image-598130 has-transparency\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-11.47.10\u202fAM.png 547w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-11.47.10\u202fAM-300x87.png 300w\" sizes=\"auto, (max-width: 547px) 100vw, 547px\"><figcaption class=\"wp-element-caption\"><em>Figure 6 \u2013 Problem of daily PY for the year after a leap year (Figure by the Author)\u00a0<\/em><\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">As you can see above, the result for February 28. 2020 is shown twice, and one day is missing the February 2021 for Online Sales (PY).\u00a0<\/p>\n<p class=\"wp-block-paragraph\">When looking at the month, the sum is correct:\u00a0<\/p>\n<figure class=\"wp-block-image is-resized\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXeMRTAAF7ZFO5t0diNEWqq21NjG9F0RJx-d_61EJ51Ad6oXkDon0rftxZFJDmHU5oaozW8MKU9HrbmB81WOvNLEzcpIoFspY6R82ZJl97z40-rlCSTrvXgLCBmTyT1KlqFoFpEEDQ?key=UZ99HMJ_TyEY5euXsrcZKGZ7\" alt=\"\" style=\"width:524px;height:auto\"><figcaption class=\"wp-element-caption\"><em>Figure 7 \u2013 Correct monthly sum with leap years (Figure by the Author)\u00a0<\/em><\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">The problem is that there is no February 29 in 2021. Therefore, there is no way that the sales for\u00a0February 29, 2020 will be displayed when listing the Sales Amount per day.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">While the result is correct, it will be wrong when the data is exported to Excel, and the values are\u00a0summed. Then, the sum of the daily results will differ from those shown for the entire month.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">This can undermine the users\u2019 perceived reliability of the data.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">My solution was to add a <code>LeapYearDate<\/code> table. This table is a copy of the Date table but without a\u00a0Date column. I added one row each year on February 29, even for non-leap years.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">Then, I added a calculated column for each month and day (<code>MonthDay<\/code>):\u00a0<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">MonthDay = ('LeapYearDate'[Month] * 100 ) + 'LeapYearDate'[Day Of Month]<\/code><\/pre>\n<p class=\"wp-block-paragraph\">The Measure to calculate the previous year manually and using the new table is the following:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">Online Sales (PY Leap Year) = \n VAR ActYear = \n SELECTEDVALUE ( 'LeapYearDate'[Year] ) \n VAR ActDays = \n VALUES ( 'LeapYearDate'[MonthDay] ) \n RETURN \n CALCULATE ( \n [Sum Online Sales], \n REMOVEFILTERS ( LeapYearDate ), \n 'LeapYearDate'[Year] = ActYear - 1, \n ActDays \n )<\/code><\/pre>\n<p class=\"wp-block-paragraph\">As you can see, I got the current year, and by using the <a href=\"https:\/\/dax.guide\/values\/\">VALUES() function<\/a>, I got the list of all dates in the current filter context.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">Using this method, my Measure works for single Days, Months, Quarters, and Years. The result of this Measure is the following:\u00a0<\/p>\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" data-dominant-color=\"ede9e9\" data-has-transparency=\"true\" style=\"--dominant-color: #ede9e9;\" loading=\"lazy\" decoding=\"async\" width=\"598\" height=\"183\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-11.56.10%25E2%2580%25AFAM.png?resize=598%2C183&#038;ssl=1\" alt=\"\" class=\"wp-image-598131 has-transparency\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-11.56.10\u202fAM.png 598w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-11.56.10\u202fAM-300x92.png 300w\" sizes=\"auto, (max-width: 598px) 100vw, 598px\"><figcaption class=\"wp-element-caption\"><em>Figure 8 \u2013 Result for the custom PY Measure, which always displays leap days (Figure by the Author) <\/em><\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">As you can see here, the Measure is very efficient, as most of the work is done by the Storage engine: <\/p>\n<figure class=\"wp-block-image aligncenter size-full\"><img data-recalc-dims=\"1\" data-dominant-color=\"e2e3e3\" data-has-transparency=\"true\" style=\"--dominant-color: #e2e3e3;\" loading=\"lazy\" decoding=\"async\" width=\"125\" height=\"182\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-11.57.07%25E2%2580%25AFAM.png?resize=125%2C182&#038;ssl=1\" alt=\"\" class=\"wp-image-598132 has-transparency\"><figcaption class=\"wp-element-caption\"><em>Figure 9 \u2013 Server Timings for the custom PY Measure for Leap years (Figure by the Author)\u00a0<\/em><\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">But, to be honest, I don\u2019t like this approach, even though it works very well.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">The reason is that the LeapYearDate table does not have a date column. Therefore, it cannot be used as a Date table for the existing Time Intelligence functions.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">We must also use the calendar columns from this table in the visualizations. We cannot use the ordinary date table.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">Consequently, we must reinvent all Time Intelligence functions to use this table.<\/p>\n<p class=\"wp-block-paragraph\">I strongly recommend using this approach only when necessary.\u00a0<\/p>\n<h2 class=\"wp-block-heading\">Week to Date and PY\u00a0<\/h2>\n<p class=\"wp-block-paragraph\">Some Business areas concentrate on Weekly analysis.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">Unfortunately, the standard Time Intelligence functions do not support weekly analysis out of the box. Therefore, we must build our Weekly Measures by ourselves.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">The first Measure is WTD.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">The first approach is the following:\u00a0<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-\">Online Sales WTD v1 = \n VAR MaxDate = MAX('Date'[Date]) \n  \n VAR CurWeekday = WEEKDAY(MaxDate, 2) \n  \n RETURN \n CALCULATE([Sum Online Sales] \n ,DATESBETWEEN('Date'[Date] \n ,MaxDate - CurWeekDay + 1  ,MaxDate) \n )<\/code><\/pre>\n<p class=\"wp-block-paragraph\">As you can see, I use the <a href=\"https:\/\/dax.guide\/weekday\/\"><code>WEEKDAY()<\/code> function<\/a> to calculate the start date of the week. Then, I use the <a href=\"https:\/\/dax.guide\/datesbetween\/\"><code>DATESBETWEEN()<\/code> function<\/a> to calculate the WTD.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">When you adapt this pattern to your situation, you must ensure that the second parameter in\u00a0<code>WEEKDAY()<\/code> is set to the correct value. Please read the documentation to learn more about it.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">The result is the following:<\/p>\n<figure class=\"wp-block-image aligncenter size-full is-resized\"><img data-recalc-dims=\"1\" data-dominant-color=\"e8e8e8\" data-has-transparency=\"true\" loading=\"lazy\" decoding=\"async\" width=\"462\" height=\"450\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-11.59.03%25E2%2580%25AFAM.png?resize=462%2C450&#038;ssl=1\" alt=\"\" class=\"wp-image-598133 has-transparency\" style=\"--dominant-color: #e8e8e8; width:462px;height:auto\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-11.59.03\u202fAM.png 462w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-11.59.03\u202fAM-300x292.png 300w\" sizes=\"auto, (max-width: 462px) 100vw, 462px\"><figcaption class=\"wp-element-caption\"><em>Figure 10 \u2013 Result for WTD in DAX Studio (Figure by the Author)\u00a0<\/em><\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">Another approach is to store the first date of each week in the Date table and use this information in the Measure:\u00a0<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">Online Sales WTD PY v2 = \n VAR DayOfWeek = MAX('Date'[Day Of Week]) \n  \n VAR FirstDayOfWeek = MIN('Date'[FirstDayOfWeekDatePY])   \n RETURN \n CALCULATE([Sum Online Sales] \n ,DATESBETWEEN('Date'[Date] \n ,FirstDayOfWeek \n,FirstDayOfWeek + DayOfWeek - 1) \n )\n<\/code><\/pre>\n<p class=\"wp-block-paragraph\">The result is precisely the same.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">When analyzing the performance in DAX Studio, I see that both Measures are comparable to each other:<\/p>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXc4fqdyEN-XX760lAz4f7IG7A_vzJ0goGTg3UJ1ROmDi8eSlwwN8Yo7stKebfKlrd-O6ByNHBIx0c5-PVlbrtou3I8gOHRd498eekPJCzRC6X_zDzhYuDuYqFG17sGFUrpqG4SwPg?key=UZ99HMJ_TyEY5euXsrcZKGZ7\" alt=\"\"><figcaption class=\"wp-element-caption\"><em>Figure 11 \u2013 On the left, you can see the execution statistics for the first version, and on the right, you see them for the second\u00a0 version. As you can see, both are very comparable (Figure by the Author)<\/em><\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\"><em>\u00a0<\/em><\/p>\n<p class=\"wp-block-paragraph\">I tend to use the second one, as it has better potential when combined with other Measures. But in the end, it depends on the current scenario.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">Another challenge is to calculate the previous year.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">Look at the following dates for the same week in different weeks:\u00a0<\/p>\n<figure class=\"wp-block-image aligncenter is-resized\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXdf6SsT728scuGCqhXnVacrNc8VivcjR5iA3XW5c23KibM16qiN3ptNwGM8PVEMBvVyj_bJ6GeIsgBg84TEMdGwmVMygPtSiygH6MI7f1VXSMHDi1MmUFvGGgzGDxMmHaGG6aIIIw?key=UZ99HMJ_TyEY5euXsrcZKGZ7\" alt=\"\" style=\"width:329px;height:auto\"><figcaption class=\"wp-element-caption\"><em>Figure 12 \u2013 Comparing the dates of the same week in different years. (Figure by the Author)\u00a0<\/em><\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">As you can see, the dates are shifted. And as the standard time intelligence functions are based on\u00a0shifting dates, they will not work.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">I tried different approaches, but in the end, I stored the first date of the same week for the previous year in the date table and used it like in the second version of WTD shown above:\u00a0<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">Online Sales WTD PY = \n VAR DayOfWeek = MAX('Date'[Day Of Week]) \n  \n VAR FirstDayOfWeek = MIN('Date'[FirstDayOfWeekDatePY])   \n RETURN \n CALCULATE([Sum Online Sales] \n ,DATESBETWEEN('Date'[Date]\n ,FirstDayOfWeek \n,FirstDayOfWeek + DayOfWeek - 1) \n )\n<\/code><\/pre>\n<p class=\"wp-block-paragraph\">This is the result:\u00a0<\/p>\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" data-dominant-color=\"eeecec\" data-has-transparency=\"true\" style=\"--dominant-color: #eeecec;\" loading=\"lazy\" decoding=\"async\" width=\"594\" height=\"577\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-12.01.29%25E2%2580%25AFPM.png?resize=594%2C577&#038;ssl=1\" alt=\"\" class=\"wp-image-598134 has-transparency\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-12.01.29\u202fPM.png 594w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-12.01.29\u202fPM-300x291.png 300w\" sizes=\"auto, (max-width: 594px) 100vw, 594px\"><figcaption class=\"wp-element-caption\"><em>Figure 13 \u2013 Result for WTD PY Measure (Figure by the Author)\u00a0<\/em><\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">As the logic is the same as in the WTD v2, the performance is also the same. Therefore, this Measure is very efficient.\u00a0<\/p>\n<h2 class=\"wp-block-heading\">Weekly Sums for PY\u00a0<\/h2>\n<p class=\"wp-block-paragraph\">Sometimes, the weekly view is enough, and we don\u2019t need to calculate the WTD at the Daily level.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">We don\u2019t need a WTD Measure for this scenario for the current year. The base Measure sliced by Week can cover this. The result is correct out of the box.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">But, again, it\u2019s another story for PY.<\/p>\n<p class=\"wp-block-paragraph\">This is the first version I came up with:\u00a0<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">Online Sales (PY Weekly) v1] = \n VAR ActYear = MAX('Date'[Year]) \n  \n RETURN \n CALCULATE([Sum Online Sales] \n ,ALLEXCEPT('Date' \n ,'Date'[Week] \n) \n ,'Date'[Year] = ActYear - 1 \n )\n<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Here, I subtract one from the current year while retaining the filter for the current week. This is the result:<\/p>\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXdHdrAi-ZEW9_C4LDBMvzbGF5TxilbNf-vZk--bLPwl8ge9weDeNYVMxp-f--3d41zrRdwrDtTDjeWanG-US0XUDk6KUr9RlmFRLirJfgo9XcNhjMBtVxuYAnGnf6C-Rly6gFvr?key=UZ99HMJ_TyEY5euXsrcZKGZ7\" alt=\"\"><figcaption class=\"wp-element-caption\"><em>Figure 14 \u2013 The result for WTD PY for the whole week. See that the WTD result for the last day of each week corresponds to the PY value (Figure by the Author)\u00a0<\/em><\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">The performance is good, but I can do better.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">What if I could store a unique Week Identifier in the Date column?\u00a0<\/p>\n<p class=\"wp-block-paragraph\">For example, the Current Week is 9 of 2025..\u00a0<\/p>\n<p class=\"wp-block-paragraph\">The Identifier would be 202509.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">When I detract 100 from it, I get 202409, the identifier for the same week in the previous year. After adding this column to the date table, I can change the Measure to this:\u00a0<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">MEASURE 'All Measures'[Online Sales (PY Weekly) v2] = \nVAR WeeksPY = VALUES('Date'[WeekKeyPY]) \nRETURN \nCALCULATE([Sum Online Sales]\n,REMOVEFILTERS('Date') \n,'Date'[WeekKey] IN WeeksPY \n)\n<\/code><\/pre>\n<p class=\"wp-block-paragraph\">This version is much simpler than before, and the result is still the same.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">When we compare the execution statistics of the two versions, we see this:\u00a0<\/p>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXeQqUUspkTxfAcQTjDXqLVIQnbLrrgrPLwcvPQnWrvqvDW0r-ut3gx3JDhg4yJOezvbUr-QFlLvQL717hGXE6m-L85QrY0_T9eY6jQMdmWlPu_xre5S2LezJem8b1qfOuMYfoopnQ?key=UZ99HMJ_TyEY5euXsrcZKGZ7\" alt=\"\"><figcaption class=\"wp-element-caption\"><em>Figure 15 \u2013 Comparing the execution statistics of the two versions for WTD PY for the whole week. On the left is V1, and on\u00a0the right is V2. (Figure by the Author)\u00a0<\/em><\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">As you can see, the second version, with the precalculated column in the Date table, is slightly more efficient. I have only four SE queries, a good sign for increased efficiency.\u00a0<\/p>\n<h2 class=\"wp-block-heading\">Fiscal Weeks YTD\u00a0<\/h2>\n<p class=\"wp-block-paragraph\">This last one is tricky.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">The requirement is that the user wants to see a YTD starting from the first day of the first week of the Fiscal year.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">For example, the Fiscal year starts on July 1.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">In 2022, the week containing July the 1<sup>st <\/sup>starts on Monday, June 27.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">This means that the YTD calculation must start on this date.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">The same applies to the YTD PY calculation starting Monday, June 28, 2021.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">This approach has some consequences when visualizing the data.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">Again, knowing if the result must be shown at the day or week level is essential. When showing the data at the day level, the result can be confusing when selecting a Fiscal Year:<\/p>\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" data-dominant-color=\"eeeeee\" data-has-transparency=\"true\" style=\"--dominant-color: #eeeeee;\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"348\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-12.04.25%25E2%2580%25AFPM-1024x348.png?resize=1024%2C348&#038;ssl=1\" alt=\"\" class=\"wp-image-598135 has-transparency\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-12.04.25\u202fPM-1024x348.png 1024w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-12.04.25\u202fPM-300x102.png 300w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-12.04.25\u202fPM-768x261.png 768w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-12.04.25\u202fPM.png 1214w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\"><figcaption class=\"wp-element-caption\"><em>Figure 16 \u2013 Result of the weekly based YTD for the Fiscal year 22\/23 (Figure by the Author)\u00a0<\/em><\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">As you can see, Friday is the first day of the Fiscal year. And the YTD result doesn\u2019t start on July 1<sup>st <\/sup>but on Monday of that week.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">The consequence is that the YTD doesn\u2019t seem to start correctly. The users must know what they are looking at.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">The same is valid for the YTD PY results.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">To facilitate the calculations, I added more columns to the Date table:\u00a0<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">FiscalYearWeekYear\u2014This field contains the numerical representation of the Fiscal year (for 23\/24, I get 2324), starting with the first week of the Fiscal year.\u00a0<\/li>\n<li class=\"wp-block-list-item\">FiscalYearWeekYearPY \u2013 The same as before, but for the previous year (FiscalYearWeekYear \u2013 101).\u00a0<\/li>\n<li class=\"wp-block-list-item\">FiscalWeekSort\u2014This sorting column starts the week with the first day of the fiscal year. A more elaborate way to use this column could be to follow the ISO-Week definition, which I\u00a0didn\u2019t do to keep it more uncomplicated.\u00a0<\/li>\n<li class=\"wp-block-list-item\">FiscalYearWeekSort \u2013 The same as before but with the FiscalYearWeekYear in front (e. g. 232402).\u00a0<\/li>\n<li class=\"wp-block-list-item\">FirstDayOfWeekDate \u2013 The date of the Monday of the week in which the current date is in. <\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\">Here is the Measure for the Daily YTD:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">Online Sales (Fiscal Week YTD) =\nVAR FiscalYearWeekYear = MAX('Date'[FiscalYearWeekYear])\nVAR StartFiscalYear = CALCULATE(MIN('Date'[Date])\n,REMOVEFILTERS('Date')\n,'Date'[FiscalYearWeekSort] =\n\nFiscalYearWeekYear * 100 + 1\n\n)\n\nVAR FiscalYearStartWeekDate = CALCULATE(MIN('Date'[FirstDayOfWeekDate])\n,ALLEXCEPT('Date'\n,'Date'[FiscalYearWeekYear]\n)\n,'Date'[Date] = StartFiscalYear\n\n)\nVAR MaxDate = MAX('Date'[Date])\nRETURN\nCALCULATE([Sum Online Sales]\n,REMOVEFILTERS('Date')\n\n,DATESBETWEEN('Date'[Date]\n,FiscalYearStartWeekDate\n\n,MaxDate\n)<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Here is the DAX Code for the Daily YTD PY:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">Online Sales (Fiscal Week YTD) (PY)] =\nVAR FiscalYearWeekYear = MAX('Date'[FiscalYearWeekYear])\n-- Get the Week\/Weekday at the start of the current Fiscal Year\nVAR FiscalYearStart = CALCULATE(MIN('Date'[Date])\n,REMOVEFILTERS('Date')\n,'Date'[FiscalYearWeekSort] =\n\nFiscalYearWeekYear * 100 + 1\n)\nVAR MaxDate = MAX('Date'[Date])\n-- Get the number of Days since the start of the FiscalYear\nVAR DaysFromFiscalYearStart =\nDATEDIFF( FiscalYearStart, MaxDate, DAY )\n-- Get the PY Date of the Fiscal Year Week Start date\nVAR DateWeekStartPY = CALCULATE(MIN('Date'[Date])\n,REMOVEFILTERS('Date')\n,'Date'[FiscalYearWeekSort] =\n\n(FiscalYearWeekYear - 101) * 100 + 1\n)\nRETURN\nCALCULATE(\n[Sum Online Sales],\nDATESBETWEEN(\n'Date'[Date],\nDateWeekStartPY,\nDateWeekStartPY + DaysFromFiscalYearStart\n\n)\n)<\/code><\/pre>\n<p class=\"wp-block-paragraph\">As you can see, both Measures follow the same pattern:\u00a0<\/p>\n<ol class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">Get the current Fiscal Year.\u00a0<\/li>\n<li class=\"wp-block-list-item\">Get the Starting Date of the current Fiscal Year.\u00a0<\/li>\n<li class=\"wp-block-list-item\">Get the Starting date of the week starting the Fiscal Year.\u00a0<\/li>\n<li class=\"wp-block-list-item\">Calculate the Result based on the Difference between these two dates\u00a0<\/li>\n<\/ol>\n<p class=\"wp-block-paragraph\">For the PY Measure, one additional step is required:\u00a0<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">Calculate the days between the starting and current dates to calculate the correct YTD. This is necessary because of the date shift between the years.\u00a0<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\">And here is the DAX code for the weekly base YTD:\u00a0<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">Online Sales (Fiscal Week YTD) =\nVAR FiscalWeekSort = MAX( 'Date'[FiscalWeekSort] )\n-- Get the Week\/Weekday at the start of the current Fiscal Year\nVAR FiscalYearNumber = MAX( 'Date'[FiscalYearWeekYear] )\n\nRETURN\nCALCULATE(\n[Sum Online Sales],\nREMOVEFILTERS('Date'),\n'Date'[FiscalYearWeekSort] &gt;= (FiscalYearNumber * 100 ) + 1\n&amp;&amp; 'Date'[FiscalYearWeekSort] &lt;= (FiscalYearNumber * 100 ) +\nFiscalWeekSort\n)<\/code><\/pre>\n<p class=\"wp-block-paragraph\">For the weekly YTD PY, the DAX code is the following:\u00a0<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">Online Sales (Fiscal Week YTD) (PY) =\nVAR FiscalWeekSort = MAX( 'Date'[FiscalWeekSort] )\n-- Get the Week\/Weekday at the start of the current Fiscal Year\nVAR FiscalYearNumberPY = MAX( 'Date'[FiscalYearWeekYearPY] )\nRETURN\nCALCULATE(\n[Sum Online Sales],\nREMOVEFILTERS('Date'),\n'Date'[FiscalYearWeekSort] &gt;= (FiscalYearNumberPY * 100) + 1\n&amp;&amp; 'Date'[FiscalYearWeekSort] &lt;= (FiscalYearNumberPY * 100) +\nFiscalWeekSort\n)<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Again, both Measures follow the same pattern:\u00a0<\/p>\n<ol class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">Get the current (Sort-) number of the week in the Fiscal year.<\/li>\n<li class=\"wp-block-list-item\">Get the start date for the fiscal year\u2019s first week.<\/li>\n<li class=\"wp-block-list-item\">Calculate the result based on these values.<\/li>\n<\/ol>\n<p class=\"wp-block-paragraph\">The result for the weekly based Measure is the following (At the weekly level, as the value is the\u00a0same for each day of the same week):\u00a0<\/p>\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" data-dominant-color=\"f0eeee\" data-has-transparency=\"true\" style=\"--dominant-color: #f0eeee;\" loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"361\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-12.11.56%25E2%2580%25AFPM-1024x361.png?resize=1024%2C361&#038;ssl=1\" alt=\"\" class=\"wp-image-598136 has-transparency\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-12.11.56\u202fPM-1024x361.png 1024w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-12.11.56\u202fPM-300x106.png 300w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-12.11.56\u202fPM-768x270.png 768w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-12.11.56\u202fPM.png 1210w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\"><figcaption class=\"wp-element-caption\"><em>Figure 17 \u2013 Result for the first three weeks per Fiscal Year with the weekly based YTD and PY Measure (Figure by the Author)\u00a0<\/em><\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">When comparing the two Approaches, the Measure for the weekly calculation is more efficient than the one for the daily calculation:<\/p>\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" data-dominant-color=\"e2e2e2\" data-has-transparency=\"true\" style=\"--dominant-color: #e2e2e2;\" loading=\"lazy\" decoding=\"async\" width=\"524\" height=\"368\" src=\"https:\/\/i0.wp.com\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-12.12.32%25E2%2580%25AFPM.png?resize=524%2C368&#038;ssl=1\" alt=\"\" class=\"wp-image-598137 has-transparency\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-12.12.32\u202fPM.png 524w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/Screenshot-2025-02-19-at-12.12.32\u202fPM-300x211.png 300w\" sizes=\"auto, (max-width: 524px) 100vw, 524px\"><figcaption class=\"wp-element-caption\"><em>Figure 18 \u2013 Comparing the execution statistics for the two Measures. On the left is the daily, and on the right is the weekly calculation. They are the same for the calculation for the current and the previous year (Figure by the Author)\u00a0<\/em><\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\">As you can see, the Measure for the weekly result is faster, has a more significant portion executed in the Storage Engine (SE), and has fewer SE queries.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">Therefore, it can be a good idea to ask the users if they need a WTD result at the day level or if it\u2019s enough to see the results at the week level.\u00a0<\/p>\n<h2 class=\"wp-block-heading\">Conclusion\u00a0<\/h2>\n<p class=\"wp-block-paragraph\">When you start writing Time Intelligence expressions, consider whether additional calculated columns in your date table can be helpful.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">A carefully crafted and extended date table can be helpful for two reasons:\u00a0<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">Make Measures easier to write\u00a0<\/li>\n<li class=\"wp-block-list-item\">Improve the performance of the Measures\u00a0<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\">They will be easier to write as I do not need to perform the calculations to get the intermediary results to calculate the required results.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">The consequence of shorter and simpler Measures is better efficiency and performance.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">I will add more and more columns to the template of my date table as I encounter more situations in which they can be helpful.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">One question remains: How to build it?\u00a0<\/p>\n<p class=\"wp-block-paragraph\">In my case, I used an Azure SQL database to create the table used in my examples.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">But it\u2019s possible to create a date table as a DAX table or use Python or JavaScript in Fabric or whatever data platform you use.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">Another option is to use the Bravo tool from SQLBI, which allows you to create a DAX table containing additional columns to support exotic Time Intelligence scenarios.\u00a0<\/p>\n<h2 class=\"wp-block-heading\">References\u00a0<\/h2>\n<p class=\"wp-block-paragraph\">You can find more information about my date-table <a href=\"https:\/\/towardsdatascience.com\/3-ways-to-improve-your-reporting-with-an-expanded-date-table-2d983d76cced\/\">here<\/a>.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">Read <a href=\"https:\/\/medium.com\/towards-data-science\/how-to-get-performance-data-from-power-bi-with-dax%20studio-b7f11b9dd9f9\">this piece<\/a> to learn how to extract performance data in DAX-Studio and how to interpret it.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">An <a href=\"https:\/\/www.sqlbi.com\/articles\/using-weekly-calendars-in-power-bi\/\">SQLBI article<\/a> about building a date table to support weekly calculations: Using weekly calendars in <a href=\"https:\/\/towardsdatascience.com\/tag\/power-bi\/\" title=\"Power Bi\">Power Bi<\/a> \u2013 SQLBI\u00a0<\/p>\n<p class=\"wp-block-paragraph\">SQLBI Pattern to perform further weekly calculations:\u00a0<\/p>\n<p class=\"wp-block-paragraph\">Week-related calculations \u2013 <a href=\"https:\/\/www.daxpatterns.com\/week-related-calculations\/\">DAX Patterns<\/a>\u00a0<\/p>\n<p class=\"wp-block-paragraph\">Like in my previous articles, I use the Contoso sample dataset. You can download the\u00a0ContosoRetailDW Dataset for free from Microsoft <a href=\"https:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=18279\">here<\/a>.\u00a0<\/p>\n<p class=\"wp-block-paragraph\">The Contoso Data can be freely used under the MIT License, as described <a href=\"https:\/\/github.com\/microsoft\/Power-BI-Embedded-Contoso-Sales-Demo\">here<\/a>. <\/p>\n<p class=\"wp-block-paragraph\">I changed the dataset to shift the data to contemporary dates.\u00a0<\/p>\n<p>The post <a href=\"https:\/\/towardsdatascience.com\/advanced-time-intelligence-in-dax-with-performance-in-mind\/\">Advanced Time Intelligence in DAX with Performance in Mind<\/a> appeared first on <a href=\"https:\/\/towardsdatascience.com\/\">Towards Data Science<\/a>.<\/p>\n<\/div>\n<p> \t<BR><br \/>\n <BR><\/BR><br \/>\n    Salvatore Cagliari<br \/>\n \t<BR><br \/>\n<BR><\/BR><br \/>\n<a href=\"https:\/\/towardsdatascience.com\/advanced-time-intelligence-in-dax-with-performance-in-mind\/\">Go to original source<\/a><br \/>\n \t<BR><br \/>\n <BR><\/BR><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Advanced Time Intelligence in DAX with Performance in Mind We all know the usual Time Intelligence function based on years, quarters, months, and days. But\u00a0sometimes, we need to perform more exotic timer intelligence calculations. But we should not forget to consider performance while programming the measures.\u00a0 Introduction\u00a0 There are many Dax functions in Power BI [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62,83,1805,1511,1360,160,1512],"tags":[1514,1779,15],"class_list":["post-1954","post","type-post","status-publish","format-standard","hentry","category-aimldsaimlds","category-data-science","category-databases","category-dax","category-power-bi","category-programming","category-time-intelligence","tag-dax","tag-intelligence","tag-time"],"_links":{"self":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts\/1954"}],"collection":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/comments?post=1954"}],"version-history":[{"count":0,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts\/1954\/revisions"}],"wp:attachment":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/media?parent=1954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/categories?post=1954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/tags?post=1954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}