I'm trying to create a calculated field which shows what percentage of the quarter has passed so far. This is what I have come up with:
DATEDIFF('day',DATETRUNC('quarter',[Max Date in quarter]),[Max Date in quarter]) //days past in the quarter so far
/
//total days in quarter
IF (DATEPART('quarter', TODAY()) = 1) THEN
DATEDIFF('day', #2019-01-01#, #2019-03-31#)
ELSEIF (DATEPART('quarter', TODAY()) = 2) THEN
DATEDIFF('day', #2019-04-01#, #2019-06-30#)
ELSEIF (DATEPART('quarter', TODAY()) = 3) THEN
DATEDIFF('day', #2019-07-01#, #2019-09-30#)
ELSEIF (DATEPART('quarter', TODAY()) = 4) THEN
DATEDIFF('day', #2019-10-01#, #2019-12-31#)
END
[Max Date in Quarter] looks like:
{ FIXED YEAR([Yyyy Mm Dd]), DATETRUNC('quarter',[Yyyy Mm Dd]): MAX([Yyyy Mm Dd])}
However, it doesn't seem to work as expected. For Q1 2019 I get 97.8% and for Q2 2019 I get 98.9%. I would expect both to be 100%.
Additionally, is there a way I could dynamically update the year so when it's 2020, I won't get caught up and need to manually change the dates here?
I achieved the correct percentage by using the below:
//days past in the quarter so far
IF (DATEPART('quarter', [Max Date in quarter]) = 1) THEN
DATEDIFF('day', #2019-01-01#, today())
ELSEIF (DATEPART('quarter', [Max Date in quarter]) = 2) THEN
DATEDIFF('day', #2019-04-01#, today())
ELSEIF (DATEPART('quarter', [Max Date in quarter]) = 3) THEN
DATEDIFF('day', #2019-07-01#, today())
ELSEIF (DATEPART('quarter', [Max Date in quarter]) = 4) THEN
DATEDIFF('day', #2019-10-01#, today())
END
/
//total days in quarter
IF (DATEPART('quarter', [Max Date in quarter]) = 1) THEN
DATEDIFF('day', #2019-01-01#, [Max Date in quarter])
ELSEIF (DATEPART('quarter', [Max Date in quarter]) = 2) THEN
DATEDIFF('day', #2019-04-01#, [Max Date in quarter])
ELSEIF (DATEPART('quarter', [Max Date in quarter]) = 3) THEN
DATEDIFF('day', #2019-07-01#, [Max Date in quarter])
ELSEIF (DATEPART('quarter', [Max Date in quarter]) = 4) THEN
DATEDIFF('day', #2019-10-01#, [Max Date in quarter])
END
Related
in my dataset there are a few customers who churned besides that their subscription plan information. Customers can change their subscription, so here I get the max plan:
{FIXED [Customer Id]:MAX(
(IF {FIXED [Customer Id]: MAX(
IF NOT ISNULL([Subscription Plan]) THEN [Date] END)
}=[Date] THEN [Subscription Plan] END)
)}
To find customer churn:
{ FIXED DATETRUNC('month', [Date]), [Max Plan]:
COUNTD(
IF NOT ISNULL([Churn Date]) AND
DATETRUNC('month', [Date]) = DATETRUNC('month', [Churn Date])
THEN [Customer Id] END
)
I want to calculate the revenue loss by churn, i.e. for each customer churn with advanced plan costs 9, for premium 19 dollars.
IF [Max Plan] = 'advanced' THEN [Churn]*9
ELSEIF [Max Plan] = 'premium' THEN [Churn]*19
END
However, it doesn't give me the correct result.
Here is the expected result:
Here is the workbook attached: https://community.tableau.com/s/contentdocument/0694T000004aUnLQAU
I have a report that will be updated Monday through Friday and want to display a single metric [Productivity %] across several different date [In_Date] "bins" i.e. a generated date dimension that would bin my data according to the following definitions:
Yesterday
Week to date (e.g. on Thursday, Sunday through Wednesday
data would be displayed etc.)
Last 7 days
Month to date
Last full month
Last 3 months
Last 6 months
Last 12 months
I'm not looking to create a parameter that the user would use to toggle; rather, I want a new dimension that would be dropped in the columns section that would follow the bins outlined above.
Below is an illustration of what I want to achieve:
Currently, I have the below result using this code:
IF [In Date] = DATEADD('day', -1, TODAY()) THEN 'Yesterday'
ELSEIF [In Date] < TODAY() AND [In Date] >= DATEADD('day', -ISOWEEKDAY(TODAY()), TODAY()) then 'Week to Date'
ELSEIF [In Date] < TODAY() AND [In Date] >= DATEADD('day', -7, TODAY()) THEN 'Last 7 Days'
ELSEIF [In Date] < TODAY() AND [In Date] >= DATEADD('day', -day(TODAY())+1, TODAY()) then 'Month to Date'
ELSEIF [In Date] < DATETRUNC('month', TODAY()) AND [In Date] >= DATEADD('month', -1, DATETRUNC('month', TODAY())) then 'Last Month'
ELSEIF [In Date] < DATETRUNC('month', TODAY()) AND [In Date] >= DATEADD('month', -3, DATETRUNC('month', TODAY())) then 'Last 3 Months'
ELSEIF [In Date] < DATETRUNC('month', TODAY()) AND [In Date] >= DATEADD('month', -6, DATETRUNC('month', TODAY())) then 'Last 6 Months'
ELSEIF [In Date] < DATETRUNC('month', TODAY()) AND [In Date] >= DATEADD('month', -12, DATETRUNC('month', TODAY())) then 'Last 12 Months'
ELSE 'Older' END
The overlapping date bins are not capturing all of the data: "last 12 months" omits metrics previously captured in the "last 6 months," etc. Moreover, the first 4 bins (yesterday, WTD, last 7 days, and MTD) are missing entirely.
This is a proper case of use of PARAMETERS in Tableau. I enjoyed while solving this.
Since you have not provided any data, I created a dummy data in excel by generating dates from 1-7-2020 onwards till date (DATE_GIVEN) and filling some random numbers against these as MEASURE_1. Something like this.
Now proceed like this.
Step-1 Create a parameters with 8 inputs (as listed in your question). I created for first 6 and leaving remaining for you. The parameter should be like
Step-2 Create a calculated field date bins with the following calculation
CASE [Parameter 1]
WHEN 1 THEN (
IF [Date_Given] = DATEADD('day', -1, TODAY()) THEN [Date_Given] end)
WHEN 2 THEN
( IF [Date_Given] < TODAY() AND [Date_Given] >= DATEADD('day', -ISOWEEKDAY(TODAY()), TODAY()) then [Date_Given] END )
WHEN 3 then
( IF [Date_Given] < TODAY() AND [Date_Given] >= DATEADD('day', -7, TODAY()) THEN [Date_Given] END )
WHEN 4 THEN
( IF [Date_Given] < TODAY() AND [Date_Given] >= DATEADD('day', -day(TODAY())+1, TODAY()) then [Date_Given] END )
WHEN 5 THEN
( IF [Date_Given] < DATETRUNC('month', TODAY()) AND [Date_Given] >= DATEADD('month', -1, DATETRUNC('month', TODAY())) then [Date_Given] END)
WHEN 6 THEN
( IF [Date_Given] < DATETRUNC('month', TODAY()) AND [Date_Given] >= DATEADD('month', -3, DATETRUNC('month', TODAY())) then [Date_Given] END)
END
Needless to say you have to incorporate appropriate calculation for remaining two values
Step-3 Place date bins on rows shelf and additionally on FILTERS CARD. (Filter out only null values from this .. special values tab). Add measure_1 to desired type. Show parameter 1 and your view is ready. Check the screenshots (Today's system date - 16-12-2020)
OR
OR
OR
As regards your edited question, I propose a workaround (because creation of a calculated field where one value input can result in multiple value outputs seems illogical and impossible to me given the basic rules of mathematics) as follows.
Create 8 different calculated fields as
Last 3 months M
IF [Date_Given] < DATETRUNC('month', TODAY()) AND [Date_Given] >= DATEADD('month', -3, DATETRUNC('month', TODAY())) then [Measure_1] END
similarly for other desired bins create a separate calculated field. You can thereafter build a view/viz as included in your question.
I have the below query which gives me the headcount per month when I select a month from the date table which is fine and works correctly.
I would like the average headcount for the past 12 months from the selected date e.g if i select Aug 2020, the average should be a sum of headcount from July 2019 - Aug 2020
Active Employees =
var currentdate = 'Date'[Max Date]
RETURN
CALCULATE(
DISTINCTCOUNT('Joined Query'[EMP_NO]),
'Joined Query'[DATE_OF_EMPLOYMENT] <= currentdate,
or(
ISBLANK('Joined Query'[DATE_OF_LEAVING]),
'Joined Query'[DATE_OF_LEAVING] > currentdate
)
)
You can reuse your headcount measure per month like this:
average 12 months =
VAR currentdate = SELECTEDVALUE('Date'[Max Date])
RETURN AVERAGEX(
SUMMARIZE(
FILTER(ALL('Date'),
'Date'[Max Date] <= currentdate && 'Date'[Max Date] >= EDATE(currentDate,-11)),
'Date'[Max Date],
"headcount", [Active Employees]
), [headcount])
Please note, -11 gives you the date 12 months ago in regards to the current selected date. In your question you say if you select August you want to see from July last year, and that would actualy be 14 months, not 12. If that's your use case you need to change it to -13.
Create 2 following measures for 12 month start and end date-
12 Month End Date =
VAR D1 =
DATEVALUE(
SELECTEDVALUE('Date'[Max Date].[MonthNo])
& "/1/"
& SELECTEDVALUE('Date'[Max Date].[Year])
)
RETURN D1-1
12 Month Start Date =
VAR D1 =
DATEVALUE(
SELECTEDVALUE('Date'[Max Date].[MonthNo])
& "/1/"
& SELECTEDVALUE('Date'[Max Date].[Year])
)
RETURN EDATE(D1,-12)
Now create this following measure for average calculation-
12_month_average =
(
CALCULATE(
DISTINCTCOUNT('Joined Query'[EMP_NO]),
DATESBETWEEN(
'Date'[Max Date],
[12 Month Start Date],
[12 Month End Date]
)
) + 0
) / 12
I want to have a date table created with DAX for a power BI report where it will have week index numbers 0,-1,-2,etc to go back in time and make comparisons of weeks. I found the below out on the web (I dont remember where)
DATE =
GENERATE (
CALENDAR( DATE( YEAR( TODAY() ) - 2, MONTH( TODAY() ), DAY( TODAY()) ), TODAY()),
VAR startOfWeek = 1 // Where 1 is Sunday and 7 is Saturday, thus a 3 would be Tuesday
VAR currentDay = [Date]
VAR days = DAY( currentDay )
VAR months = MONTH ( currentDay )
VAR years = YEAR ( currentDay )
VAR nowYear = YEAR( TODAY() )
VAR nowMonth = MONTH( TODAY() )
VAR dayIndex = DATEDIFF( currentDay, TODAY(), DAY) * -1
VAR todayNum = WEEKDAY( TODAY() )
VAR weekIndex = INT( ROUNDDOWN( ( dayIndex + -1 * IF( todayNum + startOfWeek <= 6, todayNum + startOfWeek, todayNum + startOfWeek - 7 )) / 7, 0 ) )
RETURN ROW (
"day", days,
"month", months,
"year", years,
"day index", dayIndex,
"week index", weekIndex,
"month index", INT( (years - nowYear ) * 12 + months - nowMonth ),
"year index", INT( years - nowYear )
)
)
This report should be updated daily but I noticed that today VAR Week index is not calculating correctly. Today is 4/1/2020 so I would expect 3/29/2020 - 4/1/2020 to have a week index of 0 however only 4/1/2020 and 3/31/2020 have week index 0.
I have played around with this formula a bit and cannot seem to get something that would consistently work for a report that is updated daily. I am probably going to just begin using the ISO week and ISO year values to get at my comparisons but I will lose some functionality that the week index number gives me. Can anyone please help with the a working formula?
Thank you!
The weekIndex can be simpler calculated, you can change it to:
VAR weekIndex = ROUNDDOWN((dayIndex + todayNum - startOfWeek - 6)/7;0)
I'm trying to write an expression in SSRS:
If today's DAY is <= 15th day, return pay period previous month 16th to last day of month
If today's DAY is >= 16th day, return current month day 1st to 15th
This is the expression I have now but its not working:
Start Date:
=IIF(DAY(TODAY) <= 15, DateAdd(DateInterval.Day, 1-Day(Today), Today), DateAdd(DateInterval.Month, -1, (DateAdd(DateInterval.Day, 16-Day(Today), Today)))),
IIF(DAY(TODAY) >= 16, dateadd("m",0,dateserial(YEAR(Today),MONTH(Today),1)
End Date:
=IIF(DAY(TODAY) <= 15, DateAdd(DateInterval.Day, -1, DateSerial(Year(Date.Now), Month(Date.Now), 1)),
IIF(DAY(TODAY) >= 16, dateadd(dd,datediff(dd,datepart(dd,getdate()),15),getdate())
I just had to do something similar in the past week...
--StartDate, if current day is more than 16, set to 1, else 16
=iif(DatePart("d",Today) >= 16, DateSerial(Year(Today), Month(Today), "1").AddMonths(-1), DateSerial(Year(Today), Month(Today),"16").AddMonths(-1))
--EndDate, if current day is more than 16 set to 15, else month end
=iif(DatePart("d",Today) >= 16, DateSerial(Year(Today), Month(Today), "15").AddMonths(-1), DateSerial(Year(Today), Month(Today),"1").AddDays(-1))
Use the following expressions
StartDate:
=
Iif ( Day(Today())>15
, DateAdd( "d", -Day(Today())+1, Today())
, DateAdd("d", -Day(Today())+16, DATEADD("m", -1,Today()))
)
EndDate:
=
Iif ( Day(Parameters!DateParam.Value)>15
, DateAdd("d", -Day(Today())+15 , Today())
, DateAdd("d", -Day( Today()) , Today())
)