How to build a Dax to view data of all the months till data w.r.t all the years? - ssas

How can I build a DAX function which calculates all the data until a certain date and compare that with the previous year which have the same months as the "until" date?
For example, today's date is 5 April 2018, so if I select 2017 year inside the slicer, I should be able to see a graph which shows me the comparison between the start of year i.e 1 Jan 2018 to 5 April 2018, and 1 Jan 2017 to 5 April 2017 with the previous year.
Currently I am using YtD, but I think it's calculating all the 12 months of data of all the years except the year 2018, where it shows me data from Jan 1 to April 5. Can anyone shed some light here?
Currently I am using this YTDQty = TOTALYTD(sum(Bookscan[QtySold]),DATESYTD(Bookscan[Week Date]))
Which is showing me correct data of 2018, till date, I should be able to compare the 4 months of data to my previous years 2017, 2016, 2015, these years are showing me total data for all the years i.e 12months of data, However I only need to see data start from 2018 Jan till todays date or let say March 1, so all the years should show me this current data how to do this?

Very similar to this question.
Do you have a Date Dimension in your model?
TotalQuantity =
SUM(Bookscan[QtySold])
TotalQuantity YTD =
TOTALYTD([TotalQuantity],'Date'[Date])
TotalQuantity YTD LY =
CALCULATE(
[TotalQuantity YTD],
SAMEPERIODLASTYEAR('Date'[Date])
)

Related

How to calculate Australian Financial Year from a date column in SQL?

I have a table called Sales with a date column called SaleDate. How can I write a SQL query that shows the Australian financial year in addition to the other columns?
More details:
I use Microsoft SQL Server
The Australian financial year starts on 1 July and ends the next year on 30 June.
Example 1: 10 June 2019 is FY 2019
Example 2: 5 July 2019 is FY 2020
For Microsoft SQL Server, the following query will show a new column called FY, which represents the Australian financial year.
SELECT
year(dateadd(MONTH, 6, SaleDate)) AS FY,
SaleDate,
Item
FROM Sales

MONTHS_BETWEEN function does not give the expected result [duplicate]

This question already has answers here:
Months between not returning correct value
(2 answers)
Closed 4 years ago.
I am using MONTHS_BETWEEN function to get the difference between 2 dates , but the result is not 100% correct i think this function mines 1 day .
i tried this SQL
SELECT MONTHS_BETWEEN(TO_DATE('20170630','YYYYMMDD'),
TO_DATE('20170501','YYYYMMDD') ) "Months"
FROM DUAL;
here in this case it should return 2 but the result is 1.93548387096774
So, Any help ?? or is there any way to add 1 more day to date??
Try
SELECT ROUND( MONTHS_BETWEEN(
TO_DATE('20170630','YYYYMMDD'),
TO_DATE('20170501','YYYYMMDD')))
"Months" FROM DUAL;
MONTHS_BETWEEN returns decimal result if days are different and they are not both specify the last day of the month. For more info see http://www.sqlines.com/oracle/functions/months_between
Months between will give you a decimal value with respect to a whole month. So from 1st Jan to 1st Feb, you will get 1. But from 1st Jan to 31st Jan you will not get exact 1. It will be 0.9.... Similarly from 31st Jan to 28th Feb (non leap year) you will get 1.
So you will get 2 from 1st May to 1st July OR from 30th April to 30th June,
From 1st May to 30th June, you will never get exact 2.
If you want exact 2, then either add 1 day to greater date or subtract 1 day from start_date.
DBFiddle Demo

On-going Rolling 12 month sum for KPI

I have been asked to produce 2 KPI charts which give a Rolling 12 month percentage of:
1.Percentage of hours lost due to long term sickness against hours available
2.Percentage of hours lost due to short term sickness against hours available
As you can see in the image below I have the totals as per the last day of the month up until the day the KPI is viewed. (see as at date) This is saved as a view.
KPI View
Can anybody now help me with displaying the data so that it totals up the hours as per the last 12 months? once I have these the percentages I can do, its the totalling the hours over the rolling 12 months I am struggling with.
e.g
if ran today (23/03/2016)
march 2016 would be the sum of all the results from april 2015 to 23 march 2016
Feb 2016 would be the sum of all the results from march 2015 to 29th feb 2016
and so on...
Thank you in advance.
I would consider window function. For example, generating running total of AvailableHours:
sum(AvailableHours)over(order by calendarshortyearmonth asc) as run_total_AvailableHours

Powerpivot: year to date vs year to date previous year

I am using excel 2013 powerpivot which I have linked to a sql query. In this query I have all sales data from previous years and it updates itself with the current sales. I want to make a pivottable showing year to date sales from this year compared to the same period last year. So for example from Jan.1st 2015 until July 10th of 2015 compared to Jan.1st 2014 until July 10th 2014.
I linked my sales data table with a calendar table. But whatever I try, parallelperiod, sameperiodlastyear, totalytd, it always shows me the correct data for this year, but the full year sales of last year. Can anyone recommend me what to try?
thanks,
Frank
The TOTALYTD (and other time intelligence functions) will look for the highest date in whichever context you give it, so when you try to tell it to go back 12 months from now it thinks "OK, I'm in 2014" I'll get ALL the data from 2014 and calculate a TOTALYTD. So you have to ignore the built-in functions and build your own:
=CALCULATE(sum(Table1[sales]),DATESBETWEEN(DateDim[Date], FIRSTDATE(DATEADD(DateDim[Date],-12,MONTH)), LASTDATE(DATEADD(Table1[Date],-12,MONTH))))
So long as you have a slicer or a field on your table that picks up the year, then that should work.
Edit: Having tested with a data sample, again DAX is trying to be too clever and because I have told it to go back 12 MONTHS, it assumes I want ALL data from the month context which is including everything from July last year.
Going back 365 days fixes this (as long as it's not a problem for leap years).
=CALCULATE(sum(Table1[sales]),DATESBETWEEN(DateDim[Date], FIRSTDATE(DATEADD(DateDim[Date],-12,MONTH)), LASTDATE(DATEADD(Table1[Date],-365,DAY))))
I have tried your way, but it does not yet give me the correct answer. Let me show you what I have right now.
For current year sale I have the following Dax formula
=TOTALYTD(sum(Omzetgegevens[NettoOmzet]); Kalender[Calender date])
For the previous year sale I have:
=CALCULATE((SUM(Omzetgegevens[NettoOmzet])); SAMEPERIODLASTYEAR(DATESYTD(Kalender[Calender date])))
To test your solution I have called "test ytd":
=CALCULATE(SUM(Omzetgegevens[NettoOmzet]); DATESBETWEEN(Kalender[Calender date]; FIRSTDATE(DATEADD(Kalender[Calender date];-12; MONTH)); LASTDATE(DATEADD(Kalender[Calender date]; -365; DAY))))
If I run the pivottable now, the result I get is:
2015
current year ytd last year ytd test ytd
januari 28.912 34.487 34.487
februari 50.301 66.003 31.516
maart 73.362 92.647 26.644
april 99.561 117.853 25.205
mei 128.021 149.108 31.255
juni 149.706 174.076 24.968
juli 158.297 205.438 31.362
augustus 158.297 231.693 26.255
september 158.297 254.534 22.841
oktober 158.297 282.484 27.951
november 158.297 303.808 21.324
december 158.297 313.208 9.400
Total 158.297 313.208 313.208
What I would like to see is the following. As this report was run on the 14th of july 2015. I want to see all sales for 2015 until this date and all the sales for 2014 until july 14th 2014.
If it is not possible to see the months, I am also fine with only a total number for current ytd and last year ytd
2015
current year ytd last year ytd
januari 28.912 34.487
februari 50.301 66.003
maart 73.362 92.647
april 99.561 117.853
mei 128.021 149.108
juni 149.706 174.076
juli 158.297 175.312(so not full month of july in 2014)
Total 158.297 175.312
Recently had a similar issue. Current context confuse a lot and did not return the result we think should be returned. Play with DATEADD to go back and forth in combination with any of the date/time function. You will get the result and the context will also be clear.

Year Week column comparison

I have this query that spits out every Week for the past 2 years a company owes us money. I have to narrow the results down based on their starting year and week, which are 2 separate columns. This is my current query. How do I change this so it only shows data where the year is greater or equal to start year and the week is greater or equal to the start week without losing data? So for example if I have a company 023 and its start date is week 5 2012, I'll say year>=2012 and week>=5, but then it will exclude all the weeks in 2013 and 2014 where week is less than 5. I'm not sure how logically to get around that.
SELECT MW.MW_Weeks.Year,
MW.MW_Weeks.Week,
MW.MW_CompanyCodes.cmp_code,
MW.MW_CompanyCodes.stWeek,
MW.MW_CompanyCodes.stYear
FROM MW.MW_Weeks CROSS JOIN
MW.MW_CompanyCodes
WHERE (MW.MW_Weeks.WEDate <= GETDATE())
AND (MW.MW_Weeks.Year > YEAR(GETDATE()) - 2);
Use the following:
year >= 2012 and not (year = 2012 and week < 5)
This will get all data whose year is greater than or equal to 2012 and is not before week five in 2012.