I am trying to get a month and year to date column in SQL - sql

I am trying to create a month and year to date column but the FY starts from October and I need it to roll into the next year so this can automatically update in Power Bi I have the Date ID for our year now maybe need flags to show the current month and current year then I can create a measure in Power Bi, also need it to sum up the months for year to date etc if I click january it shows me sales from October to January.

Related

How to calculate sum of amount for current month including the amount of previous months in Power Bi?

I would like to create DAX expression. This is the condition and I have my month and year table in separate column.
If we choose Month as a March then it should show the sum of amount of Jan, Feb and as well as March. Similarly, If we choose Month as Dec then it should have the sum from Jan to Dec.
How can we write DAX expression for this condition in Power Bi?
You probably need a measure as below. You need to have a separate Date/Calendar table for that which is connected to your Data table using Date column.
total_amount_ytd =
CALCULATE(
SUM(your_table_name[your_amount_column_name]),
DATESYTD(your_date_table_name[your_date_column_name])
)
With this above Measure, if you add Year/Month in a table with the new Measure, you will find the value per year will increase from January to December for a single year and starts re calculating from January again in the next year.

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.

Fiscal year and month parameters

I'm working on a report that reports on a month that a user can select from a drop-down and also reports on the cumulative data for the fiscal year up until the month selected.
How would I go about setting up the parameters so that they can select this fiscal year first from the drop-down and then the second drop-down list with automatically populate which months in that fiscal year has completed or is currently in?
For example, I choose '2019' for my fiscal year, the month date should only show the following choices: October, November, December.
You may create table with such fields: Year, Month. Run such code
select distinct Year from table order by Year desc
to select all fiscal years.
Add change event listener to drop-down (fiscal years), on fire run such code:
select Month from table where Year = #year
to select months (#year - chosen fiscal year)

Default prompt for date using month names from month numbers in OBIEE 11G analysis

Our database has a field for months as periods 1,2,3, . . . and a field for years. I would like the prompt to have month names instead of digits and I would like to have a prompt for my analysis that defaults the prior month from the time the report was run. I have year and month working in digit format(works for every month except if run in January) but I can't seem to get a solution that works with month names defaulting to the prior month.
The months need to be Year to Date, so <= the prior month.
I am in accounting so it is to get the ledger for the last day of the prior month. Each period month has just the activity for that month, so Prior year is 0, Current year activity is <>0 and the balance as of the month is 0 through the prior month.
I want users to be able to change the selections if they need but have defaults selected that they will use 99% of the time.
Right now, with the period numbers I was using
SELECT "Date"."Fiscal Period Nbr"
FROM "General Ledger"
WHERE "Date"."Fiscal Period Nbr" = (VALUEOF(Current_Month) - 1)
And for the years
SELECT "Date"."Fiscal Yr Nbr" FROM "General Ledger"
WHERE "Date"."Fiscal Yr Nbr" = (VALUEOF(Current_Year))
I created a group for each month selecting the periods, so January is 0,1 and February is 0,1,2 and so on. In the prompt I select those 12 groups and was trying to do some sort of SQL for the Default selection.

Qlikview: Past Year data calculations

I have a scenario.
In the sample table below, I need to show the sales by year…
And for each year, I need to show the last yr and last 2nd year sales for that year.
For example in 2014,
Current Year = 2014 Sales
Last Year = 2013 Sales
Current Year = 2013 Sales
Last Year = 2012 Sales
|----------2013------------|---------2014-------------|
| Last Year | Current Year | Last Year | Current Year |
Ive tried but when i nest them under a year dimension.. the calculations are not working.. is there a way around this, to come up with this kind of report format? our user is very particular in having such format..
many thanks for the help.
I'd simply hardcode all rows, and skip the year dimension:
Current Year
Sum({< Date = {">=$(=YearStart(min(Date),0"}*{">=$(=Addyears(max(Date),0)"} >} SalesAmount)
Last Year:
Sum({< Date = {">=$(=YearStart(min(Date),-1"}*{">=$(=Addyears(max(Date),-1)"} >} SalesAmount)
-2 Year:
Sum({< Date = {">=$(=YearStart(min(Date),-2"}*{">=$(=Addyears(max(Date),-2)"} >} SalesAmount)
I think this could be achieved using a pivot table. Here's an example.
You can solve this problem at the script side while loading data. So that you can compare year to date data with previous year with until corresponding month.
Transaction_Table:
LOAD date,productID,amount
FROM data.qvd;
concatenate
Load AddYears(date,1) as date,productID,amount_1
from data.qvd where date<=AddYears($(=max(date)),-1);
Data_Table:
load distinct
date,
month(date) as Month,
year(date) as Year
resident Transaction_Table;
There will be two coloumns "amount" is current date's data and "amount_1" is previous year's same day data.
Create pivot chart put year to top and product to left and create two expressions. One for calculation of amount_1: previous term and one for amount: current term
You can name expressions:
previous year label: =year-1
current year label: =year