Calculate performance vs LY with shifting month dates - date

I've tried so many times to solve this, but failed.
Question:
I help on a solution on how to calculate Current Year divided by last year in our Fiscal Year, month by month, where last years date differs from this years date
Scenario
I want to see how we performed on revenue January 2017 vs. January 2016
I select these date filters; YEAR = 2017 and Month = January. This will generate the dates 1st January to 31st January 2017
But, the dates for 2016, is not 1st January to 31st January
My company's logic is; Which DAY NAME was the 1st January of 2017 and find this first DAY NAME in January 2016 and then see which DATE this has.
Scenario Example:
2017 = 1 - 31 January and the DAY NAME for 1st January is "Sunday"
2016 = The date for the first Sunday in 2016 is 3rd January, therefore, the dates for January 2016 = 3 January - 2. February
My problem is how to make a formula that takes this year and divide that on last year's date range logic
What have I done?
First I created a formula; CALCULATE(SUM(Append1[SalesAmount]);SAMEPERIODLASTYEAR(Append1[Date]);ALL(Append1);FILTERS(Append1[Channel]))
This worked perfectly, but this just calculates 1-31 January 2017 / 1-31 January 2016 (calendar year, not fiscal year).
Googled through many, many Power BI post regarding Rolling months, DAX, DATE DAX and such, but i still haven't been able to come up with a solution.
Hope some of you can help me in the right direction!
Regards Erik

You can create a calculated column that gives the corresponding date from the previous year as follows:
DateLastYear =
VAR CurrDate = Append1[Date]
VAR LastYear = DATE(YEAR(CurrDate)-1,MONTH(CurrDate),DAY(CurrDate))
VAR Offset = MOD(WEEKDAY(CurrDate) - WEEKDAY(LastYear),7)
RETURN LastYear + Offset
Given this, you can create a calculated column that looks up the sales amount on those dates:
LastYearSalesAmount =
LOOKUPVALUE(Append1[SalesAmount],Append1[Date],Append1[DateLastYear])
At this point, you should be able to create your ratio as follows:
DIVIDE(SUM(Append1[SalesAmount]), SUM(Append1[LastYearSalesAmount]))

Related

Week Number restarting at 1 every month as per ISO format in Tableau

I am trying to get week numbers ( resetting at 1 for each month) as per ISO format for each month in 2019.For example I am interested in getting
All dates in July 2019: week 1 to 4,
All dates in Aug 2019 : week 1 to 4 and so on.
I first created the calculated field (Week_Number_ISO) to get the overall week number in year 2019.I used the following formula;
DATEPART('iso-week',[ Date]) which works as intended.
To get the monthly week number I used the following formula
INT((DATEPART('day',[Created Date])-DATEPART('iso-weekday',[Created Date])+7)/7)+1.
(Idea was to calculate the date of the first day of each week & then divide by 7 and take the integer part)
As per the ISO format, shouldn't July 29 to 31st be a part of week 4 for July?But the formula is showing it as week 5 for July 2019.I feel I am missing something in the formula or am missing something about ISO week number resetting at 1 for each month.
Can someone help me?
Here is an example of the dates in July 2019 and the associated week numbers.
Why would July 28th-July 31st 2019 be considered week 4?

How to display previous year totals into current year in Tableau??without creating parameter

I need to create a field of previous year total sales in the current year column.
Here is an example based on it being 2018 today: in 2018, it should be 2017 total sales(full year), in 2019, it should be 2018 Sales(jan-apr would be only months in may), and 2020 should be blank, as there are no data in 2019 yet, etc.
Use the calculation to make it as "Date-1".

PowerBI Datesbetween - Line Chart Visualisation

I am trying to plot on a line chart running data for 3 fiscal years. My fiscal year being 30 June and not 31 December
Data for Year 1 [1 July 2015 to 30 June 2016]
Data for Year 2 [1 July 2016 to 30 June 2017]
Data for Year 3 [1 July 2016 to 30 June 2018] This is the current year
The following is what I am hoping to achieve.
3 Year Chart
I am not certain how to achieve this.
I was thinking the following might be of some relevance
Revenue from Start = CALCULATE(Report[Revenue], DATESBETWEEN('Dates'[Date], BLANK(), LASTDATE('Dates'[Date])))
I have had it suggested to set up a new measure "Date2". But I really am not sure how to do this. Also given that I am after a third series would I then need to set up a "Date3"
For Date2 [and Date3 if applicable], how would this/these measures be coded, ie is this logical
Date2 = DATE(2015,07,01)
Date3 = DATE(2016,07,01)
Thanks for any help that can be offered. You will see from my question, I know what I want as an output, but have no idea how to really implement.
I would use the TOTALYTD function, e.g.
Revenue from Start = TOTALYTD ( [Revenue] , 'Dates'[Date] , "30 June 2018" )
This blog post is probably the best description of this and related functions:
https://www.sqlbi.com/articles/time-intelligence-in-power-bi-desktop/

Converting month to date format in access

I have an access column which has values like May, May-June, November, January-February etc.
Requirement is that
1) If only one month is there, I should create a transformation with a date column pointing to the last date of that month and the year would be the current year.
Eg May would be 5/30/2015, January would be 1/31/2015
2) If it is having two months, then second month must be taken and the same logic as in Point 1 should be implemented.
Eg May-June would be 6/31/2015, January-February would be 2/28/2015.
My first preference would be without using VBA code.
Please help.
You can use a one-liner in a function:
Public Function GetDateFromMonth(ByVal Months As String) As Date
Dim Ultimo As Date
Ultimo = DateAdd("d", -1, DateAdd("m", 1 + UBound(Split(Months, "-")), CDate(Split(Months, "-")(0) & "/1")))
GetDateFromMonth = Ultimo
End Function
Split the string on the "-", use the second month, add one to that month to get the next month, then take the first day of that month and subtract one from it. For example Jan-Feb : build 3/1/2015 and then subtract 1 day to get the last day of Feb.
Make a table that holds a list of Months:
MonthName MonthNumber
January 1
February 2
March 3
April 4
May 5
June 6
July 7
August 8
September 9
October 10
November 11
December 12
Create this query:
SELECT InputData.Months, DateSerial(Year(Date()),[MonthNumber]+1,0) AS ReqdDate
FROM InputData, Months
WHERE (((IIf(InStr([Months],'-')=0,[Months],Mid([Months],InStr([Months],'-')+1)))=[MonthName]));
That's it. Output looks like this:
Months ReqdDate
May 31/05/2015
May-June 30/06/2015
November 30/11/2015
January-February 28/02/2015
March 31/03/2015
April-September 30/09/2015
Sorry about formatting - noob. I'm in Eu so my dates look odd to you, but you get the idea; you can make them US dates in a moment I'm sure. You can use the bits of this you need in your code in two secs.
If you don't like the lookup table for month number, I'm sure you can use some format function to turn the month name into a month number.

How to subtract two dates in Oracle and extract the year part from the difference?

My typical requirement is that I want to subtract two Dates cast as timestamp. The Minuend (First parameter) is the current date and the Subtrahend(second parameter) is stored separately as DD, MM and YYYY in three columns. The final output should be a discrete year as number. I am playing with something like :
SELECT (TO_DATE('05-DEC-2013') -
CASE LENGTH(CAND_DOB_DD)
WHEN 1 THEN
CAST(TO_DATE('0'||CAND_DOB_DD||'-'||CAND_DOB_MM||'- '||CAND_DOB_YYYY,'DD-MM-YY HH24:MI:SS')
AS TIMESTAMP)
ELSE
CAST(TO_DATE(CAND_DOB_DD||'-'||CAND_DOB_MM||'-'||CAND_DOB_YYYY,'DD-MM-YY HH24:MI:SS')
AS TIMESTAMP) / 365 END YEAR
FROM CANDIDATE
The Year part as Integer will be used for a very sensitive calculation. Please suggest if the above piece of SCRIPT will yield the desired result. Thanks in advance.
For most purposes you could just use the Months_Between() function to determine the number of months between two dates, and then divide by 12 etc.. Note that the number of months is an integer when comparing two dates that have the same day of the month or are both the last day of the month.
This is tricky when it comes to leap years.
Do you count 28th Feb 2015 to 28th Feb 2016 as exactly one year, when 28th Feb 2015 to 29th Feb 2016 is one day longer but plainly is a year?
What about 29th Feb 2016 to 28th Feb 2017, or 28th Feb 2016 to 28th Feb 2017?
Think carefully about these boundary cases, but Months_Between() is likely to be your best choice.