DAX compare two years YTD - date

I need to compare data from this year with the data from last year. So the customer wants to switch between full year and year-to-date comparison. The full year i already have, but with year-to-date they want to see how the are going now compared to the data from today last year e.g.:
full data from Jan 2016 - 24. Aug (today) compared to Jan 2015 - 24. Aug 2015.
How to implement this in DAX? I'm using this in Power BI.

Supposing you have a measure called [Sales], calculated from a fact table, and a Dates table, you can write 2 new measures to compare data from this year and the previous:
Sales YTD = CALCULATE ( [Sales]; DATESYTD ( 'Dates'[Date] ) )
Sales PY YTD = CALCULATE ( [Sales]; DATEADD ( DATESYTD ( 'Dates'[Date] ); -1; YEAR ) )
Putting it in a matrix visualization, it would give you the following:
Please remember to have your 'Dates' relationship to the Sales table correctly configured in order to have your time intelligence calculations done right. You can find more info here.

Related

Tableau Fixed Max date registered by month

I want to create a graph line in tableau with a count distinct id per month.
I use an excel spreadsheet that is updated a few times per month but never on the same date, so I have different dates registered as months go by.
I want to use the last date registered per month so I can use that date to show month's trend through the years. Each id represent a different construction building, so it is expected that the same id can be found on different months.
This is what I tried: Tableau Fixed (LOD) formula the get last date registered per each month:
{ FIXED [id_constructionbuilding], MONTH([date_registered]): MAX([date_registered])}
Then I use rows and columns in tableau but I get more than one max date (my excel has 2020 and 2021 dates, so it's picking dates from October 2020 and October 2021 for example). Dates are order according to Spanish so you will see month and days shifted:
I would suggest updating your calculated field to:
{ FIXED [id_constructionbuilding],DATETRUNC('month', [date_registered]), DATETRUNC('year', [date_registered]):MAX([date_registered])}
This will keep the level of detail (LOD) to the id_constru..., month and year.

mdx query to calculate difference between measure at nearest quarter date and measure today

I am trying to calculate a measure which is the difference between a measure (say, profit) today and the nearest preceding quarter.
E.g.: if the quarter months are March, June, September and December, then:
If the current month is May, then the calculated measure = Profit(May)-Profit(March)
If the current month is November, then the calculated measure = Profit(November)-Profit(September)
If the current month is December, then the calculated measure = Profit(December)-Profit(September)
I'm new to MDX and would really like some help on this.
I'm aware of .CurrentMember.PrevMember to get the previous member, but that doesn't help if you don't know how many previous members calls you should use.
Welcome to MDX. To solve the problem you need to have basic understanding of "User Hierarchy".Plus you should know how the functions "currentmember", "parent", "lag()" and "lastchild".
Based on this follow the example below. I am trying to re-produce your example on AdventureWorks for year 2013.
Lets first see all the internet sales amount for 2013
select
{[Measures].[Internet Sales Amount]}
on columns,
{
[Date].[Month of Year].[Month of Year]
}
on rows from
[adventure works]
where
[Date].[Calendar Year].&[2013]
Result
Now lets make a measure that will result value for the last month of previous quater
with
member [Measures].[LastMonthOfPreviousQuater] as
([Date].[Calendar].currentmember.parent.lag(1).lastchild,
[Measures].[Internet Sales Amount])
select
{[Measures].[Internet Sales Amount],[Measures].[LastMonthOfPreviousQuater]}
on columns,
{
[Date].[Calendar].[Month].&[2013]&[09]
}
on rows from
[adventure works]
Result

Tableau create a filter with several values in one option

I made a year over year percentage difference of some value in a pivot table. It works ugly: If Year=2018 is selected, the percentage is treaded as 100%. If I choose Year IN (2018, 2017), I can see the proper percentage diff for Year 2018 compared with 2017 treaded as 100%.
One idea I have is to create a radio filter that works like this:
If a user selects Year=2018, I want to show data for Year IN (2018, 2017). So actually two values are selected. The table pivot part with 2017 year is hidden though.
This should work for several Year options.
Is this possible?
What you are looking for are all the years whose difference to the argument is at max one year. So if you choose 2018 as argument, you want to choose 2018 and 2017 from the data, if you choose 1997 you want 1997 and 1996.
So, create a parameter [Date Argument] for your target year. Then create the condition:
DATEDIFF('year',[Date],[Date Argument]) = 0 (OR 1) and add it to the view.
However, I believe what you are trying to do could be more easily achieved using a table calculation.

Comparing quarterly data to same time period a year ago

I have quarterly data for 2018 (Q1-Q3) which I would like to compare with the same time periods from a year ago so 2017 Q1-Q3.
Where it gets a little tricky is that I would like for my workbook to be smart enough in Q1 2019 to know that it only compares for one quarter in 2018 and when Q2 2019 data rolls in it uses 2 quarters and so on and so on.
Is this possible in Tableau?
For those that are interested I have generated some random data in the format of my workbook in Excel for import into Tableau. Here is the link to the data.
Try the following:
IF DATEPART('year',[datefield]) - 1 = DATEPART('year',TODAY())
AND DATEPART('quarter',[datefield]) < DATEPART('quarter',TODAY())
THEN [valueField]
END
This will compare with previous year's data.

Need to take window average of last 3 months for my sales

Im trying to create a wiondow average calculation of 3 months using "window_avg" function.
So far every month(viewing from the right) I get the correct window average.
However If i filter down using the order date to 1st November to End of Date, then Im not able to get the correct average for December 2015.
What Average Im supposed to Get for Dec 2015 when Order date in filter is 1st Nov to End of Date: (31045 + 75973 + 74920)/3 = 60766
What Average Im getting for Dec 2015 when Order Date in Filter is 1st Nov to End of Date : 75446(Here instead of window average for 3 months it takes wondow average of 2 months)
So the question is :
How can i make sure that even If I filter the values I get the correct window average of 3 months and not based on the filter criteria?
Workbook Link Here
I know that table calculations are based on what is in the view, but still is there a workaround for the same ?
Set your filter to include all the data you want included in your table calc — that is, filter to include any prior months needed in data you want to display. So if December 2017 is the first month you want to display, and if your moving average requires 2 prior months, filter to start in Oct 2017.
Then right click on the headers and ‘hide’ the months you don’t want to display.
Table calcs are computed in Tableau based on the result set returned by the data source in response to Tableau’s query. Filters — other than table calc filters - control what data is included in the query result. Hiding marks just prevents them from being displayed.
The example below does effectively the same thing as described above, but uses parameters and a table calc filter to avoid having to manually hide marks. The calculated field [Within Date Range] is defined as [Order Date] >= DATEADD('month', -2, [Start Date]) and
[Order Date] <= [End Date]
and [Start Date] and [End Date] are parameters.
Be sure to notice the filter based on the index () function. Table calc filters hide values, rather than exclude them from the results.
Here's another variation