MTD, Previous MTD, Last MTD, YTD and Previous YTD (LY YTD) calculation in Tableau
MTD:
- Select Analysis > Create Calculated Field
- Name the field MTD Sales, enter the following calculation, then click OK:
[Order Date] <= TODAY() AND
DATETRUNC( "month", [Order Date]) = DATETRUNC("month", TODAY() ) - Drag MTD onto the filter shelf and select "True"
[Trip Date] <= TODAY() AND
DATETRUNC( "month", [Trip Date]) = DATETRUNC("month", TODAY() )
OR
if [Run Date] <= TODAY() AND DATETRUNC( "month", [Date Field]) = DATETRUNC("month", TODAY() )then ([Total Sales]) else 0 end
Last MTD:
IF DATEDIFF('month',[Run Date], TODAY()) = 1 AND DAY([Date Field]) <= DAY(TODAY())THEN ([Total Sales])END
On the 1st of month MTD is usually shown blank, to handle this:
Create "This MTD or Last MTD" filter as shown below then create this logic:
(
DATEPART( 'day', TODAY() ) = 1
AND DATEADD( 'month', -1, DATETRUNC( 'month', TODAY() ) ) = DATETRUNC( 'month', [Trip Date] )
)
OR
(
DATEPART( 'day', TODAY() ) != 1
AND
DATETRUNC('month', TODAY() ) = DATETRUNC( 'month', [Trip Date] )
)
Now drag the "This MTD or Last MTD" into filters pane and select TRUE
This will select last month MTD on 1st of every month. So you will not see blank values for MTD on first on Month when the latest date in the table is always today-1.
[YTD]:
- Select Analysis > Create Calculated Field
- Name the field YTD Sales, enter the following calculation, then click OK:
[Order Date] <= TODAY() AND
DATETRUNC( "year", [Order Date]) = DATETRUNC("year", TODAY() ) - Drag YTD onto the filter shelf and select "True
OR
IF [Date] <=TODAY() AND DATEDIFF('year',[Date Field],Today())= 0 THEN [Total Sales] END
______________________________________________________________________
[YTD ( Last Year)]:
IF dateadd('year',1,[Run Date]) <=TODAY() AND DATEDIFF('year',dateadd('year',1,[Run Date]),Today())= 0 THEN [Total Runs] END
______________________________________________________________________
LY MTD:
Date>=DATEADD('year',-1,Date(Datetrunc('month',TODAY())) and [Date
Field]<=DATEADD('year',-1, TODAY())
______________________________________________________________
Dynamic MTD/YTD
- Create a parameter and name it "
YTD/MTD
" - String - Create a parameter and name it "Anchor Date" - Date
- Create a parameter and name it "Offset" - Int
- Select Analysis > Create Calculated Field
- Name the field Dynamic Sales, enter the following calculation, then click OK:
[Order Date] <= DATEADD( [Date Part],[Offset], [Anchor Date] ) AND
DATETRUNC( [Date Part], [Order Date]) = DATETRUNC([Date Part], DATEADD([Date Part], [Offset], [Anchor Date] )) - Drag MTD onto the filter shelf and select "True"
Comments
Post a Comment