Qlikview: Past Year data calculations - qlikview

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

Related

I am trying to get a month and year to date column in 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.

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.

Previous year Year To Date with partial current year Year To Date Calculation SQL Server

I have a revenue table with data for last year and current year. I need to calculate the YTD last year and YTD current year, BUT I need to only consider data from min(date) from last year PER branch for current year YTD calculation.
eg: Branch KTM has data from 2018-02-25 not from Jan 1st.
Now I want to get YTD for the current year from the same date on 2019 till today.
I am able to get whole YTD for last year and this year, and also the minimum date/weeknumber for each branch for last year, but unable to calculated partial YTD for the current year.
Here is one drive link to mydata and sql : https://1drv.ms/u/s!Ave_-9o8DQVEgRS7FaJmm48UNsWz?e=lRfOJF
A snippet from my code
I need help with the SQL query to do this.
This returns the number of days between the same day-of-year of a last year's date and today's date:
select current_date - (date'2018-02-25' + interval '1' year); -- PostgreSQL
select datediff(current_date, (date'2018-02-25' + interval '1' year)); -- MySQL
Alternative version:
select extract(doy from current_date) - extract(doy from date'2018-02-25'); -- PostgreSQL
doy stands for day of year. At the time of the answer (2019-09-24) all queries return 211.
To sum values in that date range, use BETWEEN:
SELECT sum(revenue)
FROM your_table
WHERE date BETWEEN date'2018-02-25' + interval '1' year AND current_date

NetSuite Wookbooks Running Total Sales for this year vs. previous year

I am trying to make a chart in NetSuite using Analytics Workbooks which shows sales for certain reps this year vs. previous year.
I've tried this in a number of different ways but it does not seem like this is possible without using formulas. I am not very savvy in writing code as I am just getting into this.
Type is Sales Order AND Sales Rep is any of ...,...,...,...
The goal is to have a shaded line chart for this year to date vs. last years sales for a group of reps.
Edit:
The closest I can get to a chart showing cumulative data is a column chart with separate measures for each month. This gives a year next to year instead of a year over year.
Original:
This formula will give you the criteria field for your date range:
CASE WHEN TO_CHAR({trandate},'DDD') <= TO_CHAR(CURRENT_DATE,'DDD') AND TO_CHAR({trandate},'YYYY') >= ( TO_CHAR(CURRENT_DATE,'YYYY') - 1) THEN 'T' ELSE 'F' END
Set this to "Yes" on the Criteria tab.
For the Series, use: TO_CHAR({trandate},'YYYY')
For the X-Axis, use:
Month: TO_CHAR({trandate},'MM-MON')
Week: TO_CHAR({trandate},'WW')
Date: TO_CHAR({trandate},'MM/DD')
Here is a list of TO_CHAR date options
Here is my chart layout

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))