How to dynamically calculate the YTD for current year without using a table calculation in Tableau? - tableau-api

How do I dynamically calculate the Year To Date (YTD) for the current year without using a table calculation in Tableau?
I have used the below formulas to calculate YoY for the current year:
if datediff('year',[Date],TODAY())=0 then [Sales] END
For the previous year:
if datediff('year',[Date],TODAY())=1 then [Sales] END
YoY:
sum(current year)/sum(previous year)-1

create a calculated field:
[date] >= MAKEDATE(Year(today()),1,1) and
[date]<= today()
Drag this to filter and select True

It depends on what you're trying to achieve. If you want to filter dates to show only values in the current year without a table calculation, then you could create a calculated field like below and filter on the result:
if Year([Date]) = YEAR(TODAY()) then "YTD" else "Not" END

Related

DAX Calculate Billing Days Between Two Variable Dates

I have a dimdate table that is represented below. I have each day flagged as BusinessDay Y/N. I also have a DimSalesRep table that has a daily goal for each rep. I want to be able to allow users to input a StartDt and EndDt with filters on the report and have a calculated column look at the business days between those dates. I can calculate daysbetween with defined dates but I am unsure how I would use DAX with variable dates that are applied through Report filters.
I should also note I am not sure how best to handle a startdt and enddt filter based of the column, TheDate
Cheers!
Reference your dimdate table twice
StartDate = 'dimdate'
EndDate = 'dimdate'
and use this measure:
Num BusinessDays =
CALCULATE(
COUNTROWS('dimdate'),
'dimdate'[BusinessDay] = "Y",
'dimdate'[Date] >= SELECTEDVALUE(StartDate[Date]),
'dimdate'[Date] <= SELECTEDVALUE(EndDate[Date])
)

Tableau - Divide values from 2 dates

I need help making a division with a relative date range.
For example:
Date Range: 01-01-2015 & 20-01-2015
Divison: 1.209,812 / 1.207,810 = 1,0016575
You can do this with a combination of LOD calcs and a Context Filter.
Select Add to Context on your relative range date filter.
Create Level of Detail calcs to isolate the min and max dates based on your filter
{max([Order Date])}
and
{min([Order Date])}
Create min and max values based on your measures. I'm using the Superstore data set in this example. The calculation states if the date equals the max date, return the Sales value. Repeat for min values.
if [Order Date] = [max date] then [Sales] end
You should have something like this:
Now just create a division between the max and min. You'll need to remove the date from the view for the calculation to render.
sum([max value]) / sum([min value])
See attached sample workbook if needed. https://www.dropbox.com/s/1ed15pwhihmjkdv/181227%20stack%20question.twbx?dl=0

How to Create Calculated field for last 7 days sales

I am trying to create one calculated field for last 7 days and want to display same info for like total order value,Total orders, Total Ordering retailers
My Formula for Calculated Field
IF ATTR([CreatedDate])>=TOTAL(MAX([CreatedDate]))-6 THEN
SUM([OrderAmount])
END
Create a new calculated field called [Day Index] that indexes your date field by day:
DATEDIFF('day', [Date], today())
Then a new field per measure to get the value for the last 7 days:
IF [Day Index] <= 6 THEN [Total Orders] END
The 6 assures you data source includes the current day also, if it doesn't then you may want to adjust this to 7.
create calculation :
[order Date] >= (TODAY()-7)
drag this calculation into filter and select TRUE

Create a measure to return the maximum date about the order in a fact table. (SSAS Multidimensional)

I want to create a measure which return the maximum date about Orders but before the actual day
I will write an example :
My tables here
(In my table Calendar i have the year 2016,2017,2019, and in my Order table, i have an order for 2016 and 2019,
I want the last date order but before the actual day (18/05/2017), so i want the Date 01/01/2016).
I have 2 table, a dimension Calendar and a fact table Order.
I was thinking about the function filter, so i search how to use filter in
google, and all the solutions i found use 'With' and 'Select'.
(I can't use 'With' and 'Select' when i create a measure in SSAS multidimensional).
Hope i will see your advice.
Just like this similar case in adv cube?
[max order date] return the maximum date about [Internet Sales Amount]
with member [max order date] AS
tail(NONEMPTY([Date].[Date].[Date],[Measures].[Internet Sales Amount])).item(0).item(0).PROPERTIES( "name" )
select {[max order date] } on 0 from [Adventure Works]
if yes, then you can create a measure in your cube like this:
Create Member CurrentCube.[Measures].[max order date]
As tail(NONEMPTY([Date].[Date].[Date],[Measures].[Internet Sales
Amount])).item(0).item(0).PROPERTIES( "name" );
if only till current day, then(following is refer to adv cube, you need do some code changes per your cube):
Create Member CurrentCube.[max order date] AS
Tail
(
NonEmpty
(
{
Head([Date].[Date].[Date]).Item(0).Item(0)--the first day in your Date dim
:
StrToMember("[Date].[Date].&[" + Format(Now(),"yyyyMMdd") + "]")-- as of current day
}
,[Measures].[Internet Sales Amount]
)
).Item(0).Item(0).Properties("name")
IDE to Write, Analyze, Tuning, Debug MDX efficiently (www.mdx-helper.com)

Using a single Calculated field , can I get the number of weekdays calculated from the current month in Tableau?

I have a requirement to do % of (Workdays till date) / (Total Workdays of the month). How can I create a "Calculated field" for this logic. I don't need to consider holidays or any sort. Any help is highly appreciated.
A quick google search turned up this:
http://kb.tableau.com/articles/knowledgebase/calculating-the-number-of-business-days
In the Calculated Field dialog box, do the following and then click
OK: Name the calculated field. In the example workbook, the calculated
field is named Number of Weekdays. In the formula field, create a
calculated field similar to the following:
DATEDIFF("weekday", [Start Date], [End Date])
- 2 * (DATEPART('week', [End Date]) -DATEPART('week', [Start Date]))
+ (IF DATENAME('weekday',[End Date]) = 'Saturday' OR DATENAME('weekday',[Start Date]) = 'Sunday'
THEN 0 ELSE 1 END)
In your example you take the difference between the first and the last of a month and calculate the working days by subtracting 2 * [number of weeks] for the weekends. Once you have that value you can easily create the ratio you wanted.