Calculating Fiscal Quarters - vba

I need help with this code. Right now this is looking at calendar year quarters, but I would like to modify it to look at fiscal year quarters.
Function test(time)
Quarter = Format(time, "YYYYq")
For vRow = 2 To 1000
report_time = Format(Sheets("data").Range("A" & vRow).Value, "YYYYq")
Assessment = Sheets("data").Range("V" & vRow).Value
If (report_time = Quarter) Then
denominator = denominator + 1
If (Assessment= 1) Then
numerator = numerator + 1
End If
End If
Next
If denominator > 0 Then
test = numerator / denominator
Else
test = 0
End If
End Function
I would appreciate your time and suggestions regarding the same.

Since I have more data to type and there's no enough space in the comments.
In terms of financial reporting you will have fiscal, calendar & actual. So please verify,
if you are working with any data vendor to get fiscal year data.
if you are planning to do any calendarization to the data? e.g. Turn fiscal year data into a calendar year data...etc
if you are going to compare fiscal year data of one company to another
Or you are trying to simply code a fiscal quarters from any fiscal year entered by a user without any calendarizing?
So if you want your code to be generic to all fiscal year/semi-annuals/quarters that a user chooses, then you need a pretty robus logic. ;) Because Apple US fiscal year that ends in September is not as same as BHP AU in June..
It's inevitable for fiscal year to have a life span from one calendar year to another if fiscal year Q4 doesn't tally with Calendar year Q4 which is December. Most painful level is when your fiscal quarters do not fall into a Calendar quarter. Where Fiscal year ends in odd months... E.g. WallMart US with January 31. Usually some European or British stocks seems to have this nature. So you have to validate each month in that case.
I did a calendarized tool sometime back. So if you can clarify your requirements clearly, happy to help.

Related

How to pull next 2 quarter data with a data model that doesn't have fiscal time database and company defined fiscal year

I would like to pull all orders from the current, next and next to next quarter of the time stamp. I am able to pull up current quarter data but I am not able to pull next and next+1 quarter data.
I am currently using MS SQL Server 2013.
The time stamp also appears in a weird format. for eg.20191031_FY20_Q1_Wk1 whereas [Order Close Fiscal Year Quarter Display Code] appears as 2020-Q1. So to pull current quarter data I have used below condition:
(LEFT(Time_Stamp,2)+'20'+'-'+substring(Time_Stamp,15,2)) = [Order Close Fiscal Year Quarter Display Code]
What I logically want to do is this:
(LEFT(Time_Stamp,2)+'20'+'-'+substring(Time_Stamp,15,2)) = [Order Close Fiscal Year Quarter Display Code] + 1
(LEFT(Time_Stamp,2)+'20'+'-'+substring(Time_Stamp,15,2)) = [Order Close Fiscal Year Quarter Display Code] + 2
Obviously, I came across data type conversion error. I even tried using Dateadd() function:
[Opportunity Close Fiscal Year Quarter Display Code] = DATEADD(quarter,1, cast(left(time_stamp,8) as date) )
but still I keep coming across same error.
The MOST IMPORTANT THING I would like to HIGHLIGHT is my org's Fiscal Year is not same as generic Fiscal Year. In my org, Fiscal Year begins from Oct and ends in Sep. So I am not sure how even DateAdd() function will help. I believe having a Fiscal Time table customized as per org's Fiscal Year could be of great help for me but my manager thinks the BI team won't entertain such a request.
Any help in building this query would be really great!!
You would seem to have time_stamp values whose format is not as you describe.
You should find the column values that don't convert to a date:
select time_stamp
from t
where try_cast(left(time_stamp, 8) as date) is null and time_stamp is not null
With this information, you can debug your code or the data.

Power BI - Difference between amount per day and total in a table

I have a table with the sales from last 2 years, and I want to compare the sales from this year with the same natural day last year. For example, Sunday 1st of April 2018 will be compared with Sunday 2nd April 2017.
In order to do that I have created the measure
sales_last_year = CALCULATE(Sales[Revenue]); SAMEPERIODLASTYEAR(DATEADD('Calendar'[Date];+1;DAY)))
And I have created another measure where I have the value from the same day last year:
Prueba_sales_last_year = CALCULATE(Sales[Revenue]); SAMEPERIODLASTYEAR('Calendar'[Date]))
The result is the following:
Sales last year
As you can see the sales per day shows 5.316€ and 3.546€, which is correct, but the total is 111.796 €, which is not correct. However, the measure with the formula without the natural day the sum of the two rows is correct. How could I solve this?
Thank you very much in advance
I just changed the order to calculate the date and it was solved.
sales_last_year = CALCULATE(Sales[Revenue]);DATEADD( SAMEPERIODLASTYEAR('Calendar'[Date]);+1;DAY))

SQL - check if an order date occurs after the second Saturday in July

I am querying against a table of 4 yrs of order transactions (pk = order number) and I'm looking to tag each record with particular date flags based on the order date - e.g., calendar year, calendar month, fiscal year, etc. There are date attributes that are specific to our business (e.g., not easily solved by a datepart function) that I'm having trouble with.
I was able to add "School Year" (for us that runs Aug 1 - July 31) using a case statement:
case
when datepart(month, oline.order_date_ready) between 8 and 12 then datepart(year, oline.order_date_ready)
else (datepart(year, oline.order_date_ready)-1)
end as school_yr
So for 1/19/2017, the above would return "2016", because to us the 2016 school year runs from Aug 1 2016 to July 31 2017.
But now I'm having trouble repeating the same kind of case statement for something called "Rollover Year". All of our order history tables are reset/"rolled over" on the 2nd Saturday in July every calendar year, so for example the most recent rollover date was Saturday July 9th 2016. Click to view - rollover year date ranges
My above case statement doesn't apply anymore because I can't just add "datepart(month, oline.order_date_ready) = 7" - I don't need the whole month of July, I just need all the orders occurring after the 2nd Saturday in that July. So in this example, I need everything occurring from Sat July 9 2016 to today to be flagged as rollover_date = 2016.
Is there a flexible way to do this without hard coding previous/future rollover dates into another table? That's the only way I can think to solve it currently, but I'm sure there must be a better way.
Thanks!
If you ask for the day-of-the-week of July 1st, then from there it's simple arithmetic, right? This query gives results matching your image:
SELECT y,
CONCAT(y, '-07-01')::timestamp +
CONCAT(6 - EXTRACT(DOW FROM CONCAT(y, '-07-01')::timestamp) + 7, ' days')::interval
FROM generate_series(2013, 2020) s(y)
ORDER BY y DESC
;
So given any date d from year y, if it comes before the 2nd Saturday of July, give it fiscal year y - 1. Otherwise give it fiscal year (school year?) y.

Oracle Week Number from a Date

I am brand new to Oracle. I have figured out most of what I need but one field is driving me absolutely crazy. Seems like it should be simple but I think my brain is fried and I just can't get my head around it. I am trying to produce a Sales report. I am doing all kinds of crazy things based on the Invoice Date. The last thing I need to do is to be able to create a Week Number so I can report on weekly sales year vs year. For purposes of this report my fiscal year starts exactly on December 1 (regardless of day of week it falls on) every year. For example, Dec 1-7 will be week 1, etc. I can get the week number using various functions but all of them are based on either calendar year or ISO weeks. How can I easily generate a field that will give me the number of the week since December 1? Thanks so much for your help.
Forget about the default week number formats as that won't work for this specific requirement. I'd probably subtract the previous 1 December from invoice date and divide that by 7. Round down, add 1 and you should be fine.
select floor(
(
trunc(invoiceDate) -
case
-- if December is current month, than use 1st of this month
when to_char(invoiceDate, 'MM') = '12' then trunc(invoiceDate, 'MM')
-- else, use 1st December of previous year
else add_months(trunc(invoiceDate, 'YYYY'), -1)
end
) / 7
) + 1
from dual;

Algorithm to convert fiscal periods to calendar periods

What I am trying to do
I am somewhat desperately trying to build an algorithm which converts financial figures from different companies from fiscal periods to calendar periods.
The problem
Fiscal periods often do not correspond to calendar periods, e.g. a company might report fiscal year 2011 revenues of 100 USD but its fiscal year does not end at the end of December 2011 but instead on September 2011. For instance, Apple's fiscal year ends end of September. Dell's fiscal year ends end of January and Intel's fiscal year ends end of December. For Apple and Dell, all fiscal quarter and fiscal half year ends are shifted as well.
In order to compare revenues or other financial metrics among these companies, I need to be able to convert each fiscal period into equivalent calendar periods. For instance, someone might ask, how much revenue each company generated in calendar year 2011.
In the case of Apple Corp., we would need to remove the revenues which have been generated in calendar period 2010, which would equal Apple's fourth quarter of FY2010 and add the first quarter of fiscal year 2012 (which ended December 2011).
What I have (data model)
My data model has the following attributes for each entity calendarPeriod and fiscalPeriod:
endYear (year in which the period ends)
endMonth (month number
1..12 at which last day the period ends)
length (number of months 1..12 of the period)
What I need (desperately)
What would be the most efficient and short algorithm I could accomplish this?
It would be great if the algorithm could handle "special situations" like Dell where it would need to take 1/3 of its fiscal first quarter, which ends awfully on January, of the following year and adding it to the last quarter of the preceding year. In addition the algorithm should be flexible enough to handle all period lengths and endMonths and try to combine periods if necessary (for instance for the first half of calendar year 2012, it should try to find a six month period which ends June 2012 or consecutively add two periods (one ending March 2012 and one ending June 2012 or taking a fiscal year which ends June 2012 and subtracting the quarters or half year which fall in calendar year 2011).
Thank you so much.
This is more of a financial question then technical question.
If you have end of year results only, there is no practical way you could compare unless they are for same financial period.
Further, if you somehow manage to get the monthly results and do some juggling to prepare comparable results, they will not be comparable as there are many accounting adjustments and provisions generally done in the end of year financial result and not in monthly results which you will miss here.
I would suggest that you should try to compare those results which yields more meaningful results.