Calculating YTD MTD in Tableau with fixed LOD (level of detail) - tableau-api

Trying to make a report in Tableau that would show the following sales stats:
sales yesterday
sales week-to-date
sales month-to-date
sales run-rate (30 days) - based on month-to-date calculation
The problem is that the data source is Shopware DB and it stores all historical states of an order: when it was created, when it was paid but not shipped, when it was shipped, when it was closed. I.e. duplicate orders.
The report is connected directly to Shopware DB without data prep. Thus, to remove the duplicate entries for each order state I am using a FIXED level of detail calculation to calclate the correct SUM:
{Fixed [Order Number],[identifier]:MIN([total_price])}
However, when I am trying to use either of solutions for MTD, YTD calculations that I found online based on this fixed calculated field "Correct SUM", I am getting totally strange numbers, which i am not even able to understand (they are much more then what I have, for example, in Power BI for the same data source; 17k USD instead of 238 USD, 65k USD instead of 55k USD, etc.).
I tried using the following calculated fields:
SUM IF:
SUM( IF MONTH([Order Date])=MONTH(TODAY()) THEN [Correct SUM] END
True/False calculated field used as a filter (as described here):
[Order Date] <= TODAY() AND DATETRUNC( "month", [Order Date]) = DATETRUNC("month", TODAY())
Can anyone advise what to do next?

You should create the field
MTD_ Sales:
IIF (year ([order date] = year (today()) and month ([order date] = year (today()) and [order date] < = today(), 1,0);
first, set the field MTD_sales becomes a dimension, and then put this field into the filter of tableau. Select "1". Then you can select the dimension and value you want, MTD_ Sales refers to the date when the MTD is met. Similarly, YTD does the same

Related

First date with sales greater than 100 in TABLEAU

I have a very Basic flat file with Sales by date and product names. I need to create a field for First sales day where sales are greater than 100 units.
I tried {FIXED [Style Code]: MIN([Prod Cal Activity Date])} but that just gives me the first day in the data the Style code Exists
I also tried IF ([Net Sales Units]>200) THEN {FIXED [Style Code]: MIN([Prod Cal Activity Date])}END but that also gives me the first day in the data the Style code Exists
DATA EXISTS PRIOR TO SALES DATE
You can use the following calculation:
MIN(IF([Net Sales Units]>100) THEN [Prod Cal Activity Date] ELSE #2100-01-01# END)
The IF([Net Sales Units]>100) THEN [Prod Cal Activity Date] ELSE #2100-01-01# END part of the calculation converts the date into a very high value (year 2100 in the example) for all the cases where the sales was more than 100 units. Once this is done, you can simply take a minimum of the calculated date to get the desired result. If you need this by style code, then you can add a fixed function in the beginning.
A few ways to simplify further if you like. They don't change the meaning.
You don't need parenthesis around boolean expressions as you would in C.
You can eliminate the ELSE clause altogether. The if expression will default to null in cases where the condition was false. Aggregation functions like MIN(), MAX(), SUM() etc silently ignore nulls, so you don't have to come up with some default future date.
So MIN(IF [Net Sales Units] > 100 THEN [Prod Cal Activity Date] END is exactly equivalent, just a few less characters to read.
The next possible twist has a bit of analytic value beyond just saving keystrokes.
You don't need to hard code the choice of aggregation function into the calculation. You could instead name your calculated field something like High Sales Activity Date defined as just
if [Net Sales Units] > 100 then [Prod Cal Activity Date] end
This field just holds the date for records with high sales, and is null for records with low sales. But by leaving the aggregation function out of the calculation, you have more flexibility to use it in different ways. For example, you could
Calculate the earliest (i.e. Min) high sales date as requested originally
Calculate the latest high sales date using Max
Filter to only dates with high sales by filtering special non-null values
Calculate the number of high sales dates using COUNTD
Simple little filtering calculations like this can be very useful - so called because of the embedded if statement effectively filters out values that don't match the condition. There are still null values for the other records, but since aggregation functions ignore nulls, you can think of them as effectively filtered out by the calculation.

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

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

Calculated field that will get the % total from a specific date to a year back

DataTypes
AffectedDate: Date & Time
RecDate: Date & Time
I have 2 Data Sources i am pulling my data from.
Above is the Data that is being pulled into this report. The AffectedDate comes from the Data source Elliott_QtyOnHand2 and it controls how many years/months are displayed.
I have this calculated field and the amount that shows up is controlled by the RecDate, which is in the Elliott_IssueQty data source.
Right now the Prev Amount Calculated field changes when i put the RecDate in the Filter area because the RecDate dictates how much data is summed in the Sum(Amount) part of the Prev Amount calculated field.
Right now i can put the RecDate in the filter area to show me the sum(Amount) in the Prev Amount calculated field of everything between a range of dates, but i need this to be done on the fly, as each field will be different.
For example if the Affected Date is Jan, 2017 we want the Sum(Amount) in the Prev Amount Calculated field to only sum from Jan, 2016 to Jan, 2017, which again is calculated off of the RecDate
Feb, 2017 would sum(amount) between Feb, 2016 - Feb, 2017 and so on...
Create a calculated field and write below code:
[Order Date] >= MAKEDATE(YEAR(TODAY())-1,MONTH(TODAY()),DAY(TODAY())) AND
[Order Date] < MAKEDATE(YEAR(TODAY()),MONTH(TODAY()),DAY(TODAY()))
Replace order date with date field in your database.
Place the calcualted field in filter shelf and then select true
Edit--------------------------------------------------------------------
Using filter doesn't help dynamic functionality of your requirement, instead you need to use parameter to achieve this:
Create a parameter select date and then select datatype as date, Here you can manually type values or select any database field using list radio button and display format should be month year from drop down.
Now crate a calculated field and write below code.
[Order Date] >= MAKEDATE(YEAR([Parameter 1])-1,MONTH([Parameter 1]),1)
AND
[Order Date] < MAKEDATE(YEAR([Parameter 1]),MONTH([Parameter 1])+1,1)
//Change order date to your **Rec Date**, if **Rec Date** is `date time` then convert t0 date format, else just replace the order date with **REc date**
Place the created calculated field on filter area and select true
Note: if you are using the datbase filed to populate the parameter values then if any new data additions to database you need to update the parameter manually or just follow method in link
if you need I can share workbook, drop a comment if you have any queries

Get month from date parameter in MDX

I'm currently building reports using Reporting Services (and MDX language).
I get a date thanks to a parameter:
MEMBER [Measures].[retail sales amount] AS (
STRTOMEMBER(#TimecalendarTimecalendarmonthhierarchy)
, [Measures].[Retail sales amount invoiced including tax])
STRTOMEMBER(#TimecalendarTimecalendarmonthhierarchy) could be something like that [Time calendar].[Time calendar month hierarchy].[Time calendar date].&[2011-03-18T00:00:00]
I would like to only get the month of this parameter. When I use:
MTD([Time calendar].[Time calendar month hierarchy].[Time calendar date].&[2011-03-18T00:00:00]
, [Measures].[Retail sales amount invoiced including tax])
it works fine but that's not the case for
MTD(STRTOMEMBER(#TimecalendarTimecalendarmonthhierarchy)
, [Measures].[Retail sales amount invoiced including tax])
Try :
MTD( STRTOMEMBER(#TimecalendarTimecalendarmonthhierarchy) ) * {[Measures].[Retail sales amount invoiced including tax]}
If it does not work, what is the result of :
STRTOMEMBER(#TimecalendarTimecalendarmonthhierarchy).name
MTD will give you the set of dates from the first date of the month to the date specified. If you want to get the month, you can either use ANCESTOR or PARENT. Parent will work if Month is directly above Date in your hierarchy. The difference between SUM(MTD(date), measure) and (date.parent, measure) is obvious when you think about previous months - if you go to a month which is in the past MTD will give you partial results as opposed to the month member itself (for dates other than the last date of the month).
StrToMember essentially translates a string to a member expression in MDX. There is no difference (functionally) between writing:
([Time].[Month].&[201001], [Measures].[SomeMeasure])
and
(StrToMember("[Time].[Month].&[201001]"), [Measures].[SomeMeasure])
It would be interesting to hear what exactly the error is when you execute your non-working expression in order to debug.
If you just want to get the month amount for the measure, you should write:
(StrToMember(#param).Parent, [Measures].[Retail sales amount...])
or (if you have more levels in between):
(Ancestor(StrToMember(#param),
[Time calendar].[<hierarchy>].[<month level>]),
[Measures].[Retail sales amount...])
Also, when using MTD you want to write:
SUM(MTD(<date member>), [Measure].[Some Measure])
instead of
MTD(<date member>, [Measure].[Some Measure])
have a look at BOL's definition of MTD