How do control the start date in DATESYTD and DATESQTD (for example: whether it is calendar year, ISO year and fiscal year)? - dax

Here are my DAX formula's for YTD and QTD calculations.
Sales YTD := CALCULATE( [Total Sales], DATESYTD( 'Date'[Date] ) )
Sales QTD := CALCULATE( [Total Sales], DATESQTD( 'Date'[Date] ) )
The date table has dates from 01-01-2020 to 31-12-2040
What is the logic used by the DATESYTD and DATESQTD to derive the 1st date of the year (DATESYTD) and first date of the quarter (DATESQTD)? For example - How do I make it use the start date based on either of the following: calendar year, ISO year and fiscal year ?

For the DATESYTD, you can control the start of the year, by setting the year end date. The default with out this option will be the normal calendar year.
DATESYTD('Date'[Date]', "03-31")
This will start the YTD calculation from the "01-04" (Normal UK tax year)
For variations between Calendar, ISO and Fiscal years, you'll have to create individual measures, a switch based on a selection, or use a calculation group to drive your YTD measures.
DATESQTD doesn't have the same option. You'll need a well-formed calendar table for the other 'To Date' options.
Update: for week related start/end year, best to check out the dax patterns approach

Related

DAX ALL being overridden by cross highlight

I have a Dates table, which includes a Season column. Seasons run from August 1 to July 31. As part of a more complex measure, I need to know the last selected date in the date slicer. The measure is run per season, so to get this date I am using
MaxSelectedDate =
CALCULATE ( MAX ( Dates[Date] ), ALL( Dates[Season] ) )
This works fine, if the slicer date range is set to end at 31 May 22, the measure returns this date for all seasons. However, the visual featuring the measure can be cross-highlighted by another season-based visual. If you choose say, the 2018/19 season, the measure calcs as 31 July 19, the max date for the chosen season. It seems to ignore the ALL( Dates[Season] ) filter removal. How can I stop it doing this?

DAX - Calculate average between last day of past month and last day of current month

I'm working on Excel and Power Pivot and I'm trying to get the average sales between the last day of the past month and the last day of the current month, but I'm unable to get the right expression for it.
I have the "Credits" table and the "Calendar" table, both linked by the "Date" field.
So, this is what I have:
=CALCULATE ( AVERAGE(Credits[Sales] );
FILTER ( Calendar ;
Calendar[DateNum] >= VALUE(FORMAT(STARTOFMONTH(Calendar[Date])-1;"YYYYMMDD"))
&&
Calendar[DateNum] <= VALUE(FORMAT(ENDOFMONTH(Calendar[Date]);"YYYYMMDD"))))
I use that measure in a dynamic table along with the "Month" dimension, but it only shows the average for the full month, not taking into account the filters I'm trying to apply so that it also takes the last day from the previous month.
I think what's happening is that the month is still in your filter context.
Try FILTER( ALL(Calendar) ; instead.
I think you could probably also simplify your measure a bit. Maybe try something along these lines:
CALCULATE(
AVERAGE( Credits[Sales] );
DATESBETWEEN(
Calendar[Date];
STARTOFMONTH( Calendar[Date] ) - 1;
ENDOFMONTH( Calendar[Date] )
)
)

Yesterday Sales Amount

We can assume that Yesterday will be the last non blank date in the Sales table
How do I add the above logic into the following measure?
Yesterday Sales =
CALCULATE(
SUM(Sales[SalesAmount])
, LASTDATE('Calendar'[Date])
)
Currently this measure returns the following:
This makes sense as the last date in the calendar table is not in the Sales table - so I think I need to change LASTDATE('Calendar'[Date]) so it only returns the last date that is in the Sales table - how do I do this?
note
There is a relationship between the Calendar and Sales table based on a Datekey but there is no Date column in the Sales table.
You can calculate it based on your date table and the today() function.
Yesterday Sales =
CALCULATE(
SUM(Sales[SalesAmount])
, 'Calendar'[Date] = today()-1
)
If you want to be based on your 'date' in the salestable, you could use a max of that field and compare with this field.

Date Logic - Tableau

Could someone help me achieve the date logic as below:
I work for a fiscal year starting from Oct to Sep, once I finish the fiscal year and step into a new one I want the last month of last fiscal year's sales for reference until the end of new fiscal year. For example, now in Sep'17 the fiscal year ended but I want Sep'17 sales number to be shown in graph as reference until next Sep'18 later that I want Sep'18 number to be shown until Sep'19 and so on so forth.
The logic I have arrived is not a permanent solution as it requires editing once I step into Year 2018, it is as below:
IF YEAR([Invoice Date]) = YEAR(TODAY()) AND
DATEPART('month', [Invoice Date]) = 9 THEN [Sales] END
once I step into Year 2018, I need to make change to the above logic like:
IF YEAR([Invoice Date]) = YEAR(TODAY())**-1** AND
DATEPART('month', [Invoice Date]) = 9 THEN [Sales] END
Is there a way to achieve permanent solution without editing the logic?
Try this:
Create 2 calcualted fields for start date and end date, Where start date is september last year and end date is september current year.
Start Date:
DATEADD('month',-1,MAKEDATE(YEAR(TODAY())-1,MONTH(TODAY()),01 ) )
End Date:
DATEADD('month',-1,MAKEDATE(YEAR(TODAY()),MONTH(TODAY()),01 ) )
Now one more calcualted field which will create the filter and add the formula to filter to get the require data.
[Order Date]>=[Start Date] AND
[Order Date] <=[End Date]
Add to filter and then select True
Note: Here today function means start of the fiscal year that you need to manage.

How to Calculate age by birthday, including month, day & Year?

What is the code for calculating the age by birthday?
How to Calculate age by birthday, including month, day & Year?
SELECT (DATE()-bdate)/365 FROM table1
where bdate is Date or DateTime field containing date of birth.
One of these articles or a combination of both should give you what you need:
http://www.tomorrowssolutionsllc.com/Articles/Calculate%20Age%20in%20Years%20and%20Months.pdf
http://www.tomorrowssolutionsllc.com/Articles/Computing%20Exact%20Age.PDF

Resources