Create calculated column to add + work days to a date - date

I am trying to create a calculated column to add + X work days to a date based on a work day steering table.
In the work day steering table, the bank days are flagged as 0.
What DAX formula should I use to create the calculated column and shift the date + work days further?
Expected Result:
Work day steering table:

You can use either a Measure or Calculated Column code as given below-
Measure Code
add_day_dinamically =
MAXX(
TOPN(
MIN(your_table_name[transport_lead_time]),
FILTER(
all(work_day_steering_table),
work_day_steering_table[flag] = 1
&& work_day_steering_table[date].[Date] > MIN(your_table_name[date_column_name])
),
work_day_steering_table[date].[Date],
ASC
),
work_day_steering_table[date].[Date]
)
Calculated Column Code
add_day_dinamically_column =
MAXX(
TOPN(
your_table_name[transport_lead_time],
FILTER(
all(work_day_steering_table),
work_day_steering_table[flag] = 1
&& work_day_steering_table[date].[Date] > your_table_name[date_column_name].[Date]
),
work_day_steering_table[date].[Date],
ASC
),
work_day_steering_table[date].[Date]
)
Here is the output-

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])
)

Rolling average a certain period using a DAX measure in Power BI

I tried generating a DAX measure as a rolling average. I don't quite know how to insert the measure I need rolling averaged as "CPI_annualized". It is not giving an option to bring CPI_annualized into the Rolling Average calc.
This is the error I am given when trying to construct the RollingAverage VAR in the measure P_MA
The syntax for 'CALCULATE' is incorrect. DAX(VAR __LAST_DATE = LASTDATE('public econometrics'[date]) ......
This is the DAX measure that I am trying to complete :
P_MA =
VAR __NUM_PERIODS = 3
VAR __LAST_DATE = LASTDATE('public econometrics'[date])
VAR RollingAverage =
AVERAGEX(
DATESBETWEEN(
'public econometrics'[date],
DATEADD(__LAST_DATE, -__NUM_PERIODS, MONTH),
__LAST_DATE)
CALCULATE([CPILFESL]))
)
RETURN RollingAverage
This is the DAX measure that I am trying to use in the rolling calculation with the data in my dB given monthly. This works as below.
CPI_annualized = (CALCULATE(SUM('public econometrics'[value]),'public
econometrics'[econometric_name]=="CPILFESL")/CALCULATE(SUM('public
econometrics'[value]),'public
econometrics'[econometric_name]=="CPILFESL",SAMEPERIODLASTYEAR('public econometrics'[date])))-1
Inserting this measure in a line chart gives my this table.
date
CPILFESL
1 January, 1978
6.41%
1 February, 1978
6.20%
I think you are getting error message due to this line:
CALCULATE([CPILFESL])
In Dax Calculate measure, you cannot directly calculate a value, instead you have to include a number calculation function such as sum, max, min, here is the example:
CALCULATE(sum([CPILFESL]))
In addition, you can get rid of Calculate measure when there is no filter expression, such as sum([CPILFESL]) is enough.
A proper way of using calculate is to filter the value, as shown in online documentation
CALCULATE(
SUM(Sales[Sales Amount]),
'Product'[Color] = "Blue"
)
You can find many usefull templated here:
https://www.daxpatterns.com/month-related-calculations/
Moving average 3 months
VAR MonthsInRange = 3
VAR LastMonthRange =
MAX ( 'Date'[Year Month Number] )
VAR FirstMonthRange =
LastMonthRange - MonthsInRange + 1
VAR Period3M =
FILTER (
ALL ( 'Date'[Year Month Number] ),
'Date'[Year Month Number] >= FirstMonthRange
&& 'Date'[Year Month Number] <= LastMonthRange
)
VAR Result =
IF (
COUNTROWS ( Period3M ) >= MonthsInRange,
CALCULATE (
AVERAGEX ( Period3M, [Sales Amount] ),
REMOVEFILTERS ( 'Date' )
)
)
RETURN
Result

Is there a way to count days INSIDE a range of dates?

I'm quite a beginner on VB/SQL, I just began my learning few months ago, but I can understand the logic of algorithms as I used to do some Excel VBA .
I'm actually designing a database where I can (wish to) follow up every colleague's activity during the year.
The objective is to have a (Monthly) ratio of =>
Billable days / (Billable + Non Billable - Absent)
The context :
A single person can be : Working internally (Non billable), OR Working Externally (Billable) , OR on Holidays (Absent).
I have a [Planning] Table where it stores the following data : [Consultant_ID] (linked to another table [Consultant], [Activity] (A list with the three choices described above), [Beginning_Date], [End_Date].
Example :
Consultant 1 : Working externally from 01/01/2019 to 01/06/2019,
Working internally from 02/06/2019 to 31/12/2019,
Holidays from 02/03/2019 to 15/03/2019
Is there a way to have the Billable ratio of March for example ?
I created 4 queries (Maybe too much ?)
3 queries : [Consultant_ID] [Activity] [Beginning_Date] [End_Date] [Ratio : Datediff("d";[Beginning_Date];[End_Date]).
For each query : The [Activity criteria] : one Working Internally, one Working Externally, one Absent.
And for the [Beginning_Date] and [End_Date] criterias : <=[Enter beginning date], >=[Enter End date]
And the 4th query [Consultant ID] [Billable] [Non billable] [Absent] (and planning to add the [RATIO]).
Problem is : the Datediff counts the dates of the whole activity of what it finds, and not only the dates between 01/03/2019 and 31/03/2019 as I wish to.
I Expect the output of the ratio to be : Billable days / (Billable + Non Billable - Absent) of the desired period.
The actual output shows the billable, non billable, and absent days of the whole period between the dates which are inputted
So instead of 31 Billable, 0 Non billable, 15 Absent
It shows 180 Billable, 0 Non Billable, 32 Absent
Sorry for the long post, it is actually my first, and thank you very much !
I've been struggling with this for a whole week
We first need to figure out the maxBegin and the minEnd dates for each row
SELECT
*,
(IIF (Beginning_Date > #3/1/2019#, Beginning_Date, #3/1/2019#) ) as maxBegin,
(IIF (End_Date < #3/31/2019#, End_Date, #3/31/2019#) ) as minEnd,
Datediff("d", maxBegin, minEnd) + 1 as theDiff
FROM Planning
Where Beginning_Date <= #3/31/2019# AND End_Date >= #3/1/2019#
Then use that to compute the durations. Note: DateDiff does not count both ends, so we need to add +1.
SELECT
Consultant_ID,
SUM(IIF (Activity = "Working Internally", Datediff("d", maxBegin, minEnd) +1, 0) ) as NonBillable,
SUM(IIF (Activity = "Working Externally", Datediff("d", maxBegin, minEnd) +1, 0) ) as Billable,
SUM(IIF (Activity = "Holidays", Datediff("d", maxBegin, minEnd) +1, 0) ) as Absent
FROM
(
SELECT
*,
(IIF (Beginning_Date > #3/1/2019#, Beginning_Date, #3/1/2019#) ) as maxBegin,
(IIF (End_Date < #3/31/2019#, End_Date, #3/31/2019#) ) as minEnd
FROM Planning
Where Beginning_Date <= #3/31/2019# AND End_Date >= #3/1/2019#
) as z
GROUP BY Planning.Consultant_ID;
Finally, you need to substitute the actual Begin/End dates via params into the sql to run your query. Also note that the Holidays are only 14, not 15.
Also, you can add the Ratio calculation right into this sql, and have only one query.

Power BI Rolling Average DAX to plot correctly on Column Chart

I have a problem with the measure of the 3mth rolling average to visualise it correctly on the graph.
The data model is here:
https://docs.google.com/spreadsheets/d/1naChcuZtjSbk0pVEi1xKuTZhSY7Rpabc0OCbmxowQME/edit?usp=sharing
I am using the formula below to calculate 3mth average through a measure:
Product3Mth = CALCULATE(SUM('Table'[Product A uncum]);DATESINPERIOD('Table'[Date];LASTDATE('Table'[Date]);-3;MONTH))/3
When I am plotting it as a table it is showing right values for each month.
But When I am plotting it in the column chart together with Product A Accumulated I am getting wrong value which is the value for Product unaccum /3 insted of sum of 3 consecutive values for Product unaccum /3.
What should I change in the DAX to have it visualised correctly? Please HELP
It seems you've stumble accross quite a few Power BI "gotchas" here when it comes to both the format of the date in your source data and the way you've chosen to display the Date column in your visulaization. But I think I've figured it out. This is my result:
And just to verify some numbers:
(4043 + 20 + 158) / 3 = 1469
(189+ 200 + 207) / 3 = 199
And here are the details:
I used this dataset where I've changed the names slightly to make it easier to write DAX expressions and imported it using Get Data
Date unAcc ACc
01-10-2017 00:00 4043 4043
01-11-2017 00:00 205 4248
01-12-2017 00:00 158 4406
01-01-2018 00:00 142 4548
01-02-2018 00:00 312 4860
01-03-2018 00:00 258 5118
01-04-2018 00:00 176 5294
01-05-2018 00:00 210 5504
01-06-2018 00:00 189 5693
01-07-2018 00:00 200 5893
01-08-2018 00:00 207 6100
And for reasons still uknown to me, I had the same issues as you had with the Date column. But following some tips from the Power BI community, I created a Date2 like this :
Date2 =
DATE('Table1'[Date].[Year];'Table1'[Date].[MonthNo];1)
Then I calculated the three month average using a
Moving_Average_3_Months =
CALCULATE (
AVERAGEX ( 'Table1'; 'Table1'[unAcc] );
DATESINPERIOD (
'Table1'[Date2];
LASTDATE ( 'Table1'[Date2]);
-3;
MONTH
)
)
Now, if you insert a column chart and assign Date2 to the Axis and Moving_Average_3_months together with unAcc to Values, you'll get this:
And that's not what we want. So go to the Visualization settings and change Date2 from Date Hierarchy to simply Date2 like this:
And that's it:
And here's the whole thing as a table so you can see that the numbers are correct:
In your case, maybe the only thing you have to do is that very last part.
Please don't hesitate to let me know how it works out for you!
I ask in the last comment the issue about the calculated value (as column or measure) because I have different results based on this, as you can see in the example below:
The Product3Mth is based on the calculated column and the Product3Mth 2 is based on the measure (result you want!).
Hope this example can help you. If not tell me please!
The DATESINPERIOD and LASTDATE functions will not work correctly, because 'Table'[Date] is not a continous range of dates. These functions need to be based on a datetable.
Try something like this
Product3MthAVG =
VAR rowDate = 'Table'[Date]
RETURN
CALCULATE (
SUM ( 'Table'[Product A unaccum] );
FILTER (
'Table';
'Table'[Date]
< DATE ( YEAR ( rowDate ); MONTH ( rowDate ) + 1; DAY ( rowDate ) )
&& 'Table'[Date]
>= DATE ( YEAR ( rowDate ); MONTH ( rowDate ) - 2; DAY ( rowDate ) )
)
)
/ 3

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)