DAX Cycle Time at column level which can then be AVERAGE'd up (Parameter is not the correct type) - powerbi

I'm trying to calculate cycle times at row level, based on two slicers. Which I can then use in a measure to get Averages e.g. per client.
I have two pre-populated tables
CycleStartDateOptions
Id StartDateName
1 [Incident Date]
2 [Examination Date]
CycleEndDateOptions
Id StartDateName
3 [Resolution Date]
4 [Closed Date]
Then a measure on CycleStartDateOptions
SelectedCycleStartDate = SELECTEDVALUE(CycleStartDateOptions[Id])
.. and CycleEndDateOptions
SelectedCycleEndDate = SELECTEDVALUE(CycleEndDateOptions[Id])
Example MainTable Data:
ClientID [IncidentId] [Incident Date] [Examination Date] [Resolution Date] [Closed Date]
C0001 I00001 2020-01-01 2020-02-01 2020-03-01 2020-04-01
C0001 I00002 2020-01-01 2020-03-01 2020-04-01 2020-05-01
C0002 I00003 2021-01-02 2021-02-02 2021-03-02 2020-04-02
C0002 I00004 2021-01-02 2021-03-02 2021-04-02 2020-05-02
I have two measures on MainTable:
CycleStartDateSwitch = SWITCH(CycleStartDateOptions[SelectedCycleStartDate],
1,MIN('MainTable'[Incident Date]),
2,MIN('MainTable'[Examination Date]))
CycleEndDateSwitch = SWITCH(CycleEndDateOptions[SelectedCycleEndDate],
3,MIN('MainTable'[Resolution Date]),
4,MIN('MainTable'[Closed Date]))
Finally to generate my dynamic cycle time, I have this measure:
CycleTimeDynamic = DATEDIFF('MainTable'[CycleStartDateSwitch], 'MainTable'[CycleEndDateSwitch], DAY)
Phew! This works when shown in a table and for each incident I see a value for Cycle Time between the user selected start and end dates e.g. Incident Date to Resolution Date.
The problem now is, the above is done as measures. But I need to be able to filter on date ranges e.g. Incident Date > 2020-01-01 and 2022-01-01 and get Average cycle times per client. When I try to create a new measure it underlines my CycleTimeDynamic measure and says "Parameter is not the correct type".
I tried doing the CycleStartDateSwitch and CycleEndDateSwitch as Columns using e.g.
CycleStartDateSwitch = SWITCH(CycleStartDateOptions[SelectedCycleStartDate],
1,'MainTable'[Incident Date],
2,'MainTable'[Examination Date])
.. but it won't surface any data.
Any ideas what I'm doing wrong? This would be easy in SQL and I'm sure it's doable in DAX but I'm struggling with what should be Columns and Measures.
What I want to be able to see:
ClientID [Incident Count] [Average Cycle Time]
C0001 5 123
C0002 8 345
Any help much appreciated!

Related

DAX max date in dimension ignore filters on date

In my Power BI model I have a fact table (factSales) that links to a date dimension (dimDate) through a surrogate key DateId. Now I want to add a measure to obtain the max invoice date for each client. But it should be the maximum date ignoring the context (for the date filters). (So if I filter all sales in Q1 2020, then I still want the max invoice date in e.g. 2021).
This is how I got it working:
Add new column in factSales:
Invoice Date = RELATED(DimDate[Date])
Add new measure in factSales:
Last Contract =
CALCULATE( MAX(FactSales[Invoice Date]),
ALL( DimDate )
)
This works, but is there a better way to do this ? Without the extra calculated column. (And without using both-directional filtering).
You can use CROSSFILTER inside CALCULATE:
Last Contract =
CALCULATE (
MAX ( Dates[Date] ),
REMOVEFILTERS ( DimDate ),
CROSSFILTER ( Sales[Date], Dates[Date], BOTH )
)
You can create a measure to return max date using the following dax formula:
Measure = MAX(Sheet1[Date])
To always display the latest date without filter by slicer, you need to click on the slicer then goto Format >Edit Interaction >click none on the specific visual. In the following case, the max date is still 8 Nov 21 even though the slicer latest date is Sep 21
Try this:
Last Contract =
CALCULATE ( MAX ( DimDate[Date] ), ALL ( DimDate ), FactSales )
This removes filtering from a slicer on DimDate[Date] by still applies FactSales as a filter table.

Average with Multiple Criteria in PowerBI

I have 4 columns in table called year, product, status, value. I want the MEASURE average of value only for specific status and for specific year so how i can write the average DAX with multiple criteria.
In excel i can easily write as averageifs(value, year=2021, status="Sold").
I WANT THE MEASURE ONLY.
Please suggest. i tried every and did not get success to add multiple criteria's.
Regards,
SK
You can try this below measure-
average_ =
CALCULATE(
AVERAGE(your_table_name[value]),
FILTER(
ALL(your_table_name),
your_table_name[year] = 2021
&& your_table_name[status] = "sold"
)
)

Cumulative count of occurence on date PowerBI?

dataset resembles this:
ID Milestone
id1 01/01/2020
id2 02/01/2020
id3 04/01/2020
id4 null
id5 02/01/2020
id6 12/01/2020
I'm trying to count the number of records have reached the milestone before a certain date, in other words to count a cumulative total of occurences. I currently have this DAX measure:
#Measure =
calculate(
COUNTROWS(table),
filter(
table,
table[milestone].[Date] <= MAX(table[milestone].[Date])
)
)+0
But this doesn't seem to give me the desired result:
Picture of table
As you can see, it doesn't add the count of previous occurences to the current date count.
Thanks in advance
EDIT:
An example after using the feedback provided by msta42a :
This work for me:
Measure = calculate(COUNTROWS('Table')
,FILTER(ALL('Table'[Mileston]), not(ISBLANK('Table'[Mileston])) && 'Table'[Mileston]<=
SELECTEDVALUE('Table'[Mileston])))
Your Measure is very close to your requirement. Here below is the adjusted code-
#Measure =
calculate(
COUNTROWS(table),
filter(
ALL(table),
table[milestone] <= MAX(table[milestone])
)
)+0

Rolling 6 month Open Contracts in Power BI

I need to show how many active contracts we have open for each month in the last 6 months. I am trying to figure out a way to display this. Here is my table
Machine Enrollment# StartDate EndDate
A 1 1/2/2016 6/18/2019
B 2 12/15/2012 5/12/2034
C 3 3/25/2019 4/25/2021
D 4 1/7/2000 7/15/2019
A 5 10/1/2019 10/1/2025
I have thousands of rows. I want to be able to show a rolling 6 month visual for how many machines are under contract. So in this small example it would look like this
Apr-19 June-19 Jul-19 Aug-19 Sep-19 Oct-19
4 4 3 2 2 3
Where do I even begin in creating this? In the past, we have just looked at the numbers for the current month and tacked those results onto the end of a static table and deleted the column from over 6 months ago. I have been assigned to automate this report in Power BI. I am guessing I need to create a column/measure that looks at the EndDate and compares it to the filtered Date in the visual (ie: Aug-19) and determines if the contract was open at that time. But I do not know. Any help is much appreciated. Thanks in advance!
I think I found a solution for what you are looking for. You may find a sample pbix file here.
1. Create a calendar table
A calendar table is required to filter/slice the time periods. The calendar table needs to have a unique Date column, and optional columns such as Year, Quarter, and Month, depending on what units of period you need in the analysis.
A calendar table can be most easily created as a DAX calculated table. Here is an example of a minimal calendar table required in this use case.
Calendar =
ADDCOLUMNS(
CALENDAR( MIN( Contracts[StartDate] ), MAX( Contracts[EndDate] ) ),
"Year Month", FORMAT( [Date], "mmm-yy" ),
"Year Month Number", YEAR( [Date] ) * 100 + MONTH( [Date] )
)
2. Create a measure to calculate number of open contracts
Every numbers calculated and shown in reports need to be defined as measures.
Let's think about the number of May-19. The current filter context includes all 31 dates in the Calendar table between 2019-05-01 and 2019-05-31. In this case, how can we think of an open contract? If the contract starts after 2019-05-31, it is not open. If the contract ends before 2019-05-01, it is not open as well. Therefore the open contract meets this condition.
Starts on or before 2019-05-31 and
Ends on or after 2019-05-01
Below is the measure definition to count the number of contracts based on this condition.
# Open Contracts =
VAR MinDate = MIN( 'Calendar'[Date] )
VAR MaxDate = MAX( 'Calendar'[Date] )
RETURN COUNTROWS(
FILTER(
Contracts,
Contracts[StartDate] <= MaxDate
&& Contracts[EndDate] >= MinDate
)
)
3. Add dynamic filter for last 6 months
If I was understanding correctly, the requirement is to show monthly number of last 6 calendar months, excluding this month. I could not find a straightforward way for this. My solution may contain a bit of hacky scent.
Power BI does not have built-in filter support based on calendar months relative to now. We need to build a custom logic to achieve this. I did it by creating a measure that indicates whether current filter context is within the desired period. This measure is a flag that returns 1 if the filter context is a single calendar month which is included in the last 6 calendar months, or returns BLANK otherwise.
__Last6MonthFlag =
VAR YearMonths = CALCULATETABLE(
VALUES( 'Calendar'[Year Month] ),
REMOVEFILTERS( 'Calendar'[Year Month] ),
'Calendar'[Date] > EOMONTH( TODAY(), -7 )
&& 'Calendar'[Date] <= EOMONTH( TODAY(), -1 )
)
RETURN IF(
HASONEVALUE( 'Calendar'[Year Month] )
&& SELECTEDVALUE( 'Calendar'[Year Month] ) IN YearMonths,
1
)
Then I used this measure in the visual filter like this.
You need to do below activities to achieve your requirement.
Define a calendar table holding all the dates for your report. Define a calculated column for Month-Year. Month-Year = FORMAT('CalendarTable'[Date], "MM-YYYY")
You can define a calculated measure, which will return 1 if the end date of the contract is < 6 months from current date (you can use TODAY() function). This function will help in rolling calculation based on current date. Otherwise, this calculated measure will return NULL and SUM them.
You can drag the month-Year calculated column, defined in step no. 1, to the column axis. You can drag the calculated measure, defined in step no.2, to the values section. PowerBI control by default filters out the NULL values. So, when you use the calculated measure defined in step no. 2, you will get values only for the last 6 months. As the calculation is defined based on TODAY(), it will be a running calculation.

Calculate monthly value between 2 tables without an explicit relationship in Power BI model

I am trying to create a measure that calculates (a/qty)*100 for each month,
where qty commes from Delivery table (generated with an R script)
month qty
<date> <dbl>
1 2019-02-01 1
2 2019-03-01 162
3 2019-04-01 2142
4 2019-05-01 719
And a comes from a table TABLE_A that has been created within Power BI that looks like this :
Client Date a
x 2019-03-07 3
x 2019-04-14 7
y 2019-03-12 2
So far, I managed to calculate that value overall with the following measure formula :
MEASURE = CALCULATE( (Sum(TABLE_A[a])/sum(Delivery[qty]))*100)
The issue I have is that I would need this measure monthly (i.e. join the tables on month) without explicitly defining a link between the tables in the PowerBI model.
For each row in TABLE_A you need to look up the corresponding qty in Delivery, so try something along these lines:
MEASURE =
DIVIDE(
SUM( TABLE_A[a] ),
SUMX(
TABLE_A,
LOOKUPVALUE(
Delivery[qty],
Delivery[month], EOMONTH( TABLE_A[Date], -1 ) + 1
)
)
) * 100
The formula EOMONTH( TABLE_A[Date], -1 ) returns the end of the previous month relative to that date and adding 1 day to that to get the start of the current month for that date.