How to get DateTime difference in Dax? - dax

I have table which has an a DateTime column, status column and an index column, I am trying to get a column which shows the time difference between the previous row and the current row and for it to ignore the previous row status column is blank.
This is what the table looks like, currently:
Time ID status. timesinceprev(seconds) index
22.1.21 04:02:04 12 low 0 1
22.1.21 04:24:07 12 low 1320 2
22.1.21 04:26:04 12 medium 120 3
22.1.21 04:29:04 12 180 4
22.1.21 04:30:05 12 61 5
I want to change the timeSinceprev to show the time difference in the format HH:MM:SS when the previous row status column is blank, Here is what I have currently with the query:
timeCol =
var tempcol=
MINX(FILTER('Table',
'Table'[ID]=EARLIER('Table'[ID])
),'Table'[Time])
var filtertemp =
EARLIER('Table'[status])
RETURN IF(filtertemp<>BLANK(),FORMAT('Table'[Time]-tempcol,"HH:MM:SS"))

Use EARLIER like following in combination with ID and Index to get the previous row value when status=blank, grouped by ID
Column =
VAR _1 =
SWITCH (
TRUE (),
'Table'[status] = BLANK (),
MAXX (
FILTER (
'Table',
'Table'[ID] = EARLIER ( 'Table'[ID] )
&& 'Table'[Index] < EARLIER ( 'Table'[Index] )
),
'Table'[Time]
)
)
VAR _2 =
SWITCH (
TRUE (),
'Table'[status] = BLANK (), CALCULATE ( MAX ( 'Table'[Time] ) )
)
VAR _3 =
FORMAT ( _2 - _1, "HH:MM:SS" )
RETURN
_3

Related

Creating calculated table with MAX and MIN dates in DAX Power BI

I have the following table, imported in Power BI - QOL_Exp (see screenshot example below)
I need to create a calculated table which will filter out values, where Rating = 999 and,
at the same time, will pick only the highest and the lowest Date values from Date column, based on ClientID (see highlighted grey and peach colored areas).
I highlighted in red font - the values that I expect to see in my calculated table
For example, for ClientID = 3052 I will need the records where Date = 11/20/2020 (lowest date for this ClientID) and Date = 5/17/2021 (highest date for this ClientID)
For ClientID = 2666 I will not need the record where Rating = 999 (one of the conditions)
I managed to filter out (to exclude Rating = 999) but struggling with including only Max and MIN date in the new calculated table
This is my DAX:
QOL = CALCULATETABLE(QOL_Exp, QOL_Exp[Rating]<>999)
How should I modify it in order to only leave Max(Date) and Min(Date) records, based on ClientID?
UPD:
Based on the answer given, slightly updated (see below):
QOL =
FILTER (QOL_Exp, QOL_Exp[Rating] <> 999
&&
(( QOL_Exp[Date] = CALCULATE (MIN ( QOL_Exp[Date] ),
ALLEXCEPT(QOL_Exp,QOL_Exp[ClientID])))
|| QOL_Exp[Date] = CALCULATE (MAX ( QOL_Exp[Date] ),
ALLEXCEPT(QOL_Exp, QOL_Exp[ClientID]))))
QOL =
FILTER (
QOL_Exp,
QOL_Exp[Rating] <> 999
&& (
QOL_Exp[Date]
= CALCULATE (
MIN ( QOL_Exp[Date] ),
FILTER (
QOL_Exp,
QOL_Exp[Rating] <> 999
&& QOL_Exp[ClientID] = EARLIER ( QOL_Exp[ClientID] )
)
)
|| QOL_Exp[Date]
= CALCULATE (
MAX ( QOL_Exp[Date] ),
FILTER (
QOL_Exp,
QOL_Exp[Rating] <> 999
&& QOL_Exp[ClientID] = EARLIER ( QOL_Exp[ClientID] )
)
)
)
)

Dax calculated column for holding max value grouped by date

I have a table that holds ten days of values broken out by hour:
date hour sales
11/20/2019 1 10
11/20/2019 2 20
11/20/2019 3 30
...
11/20/2019 23 230
this pattern is repeated for each date until the current date where a row is inserted on the hour with latest sales
I would like to take the last sales amount for each date and divide the prior rows by that value using DAX. I am first trying to create a calculated column to just hold the max sales value, but for some reason I am getting the max value for the table instead of the group:
Estimated[SalesPct] =
var maxSales = MAXX('Estimated'[Exp_Sales])
return
CALCULATE(divide('Estimated'[Exp_Sales], maxSales)
For calculated column following DAX will do:
Estimated[SalesPct] =
VAR maxSales =
CALCULATE (
MAX ( 'Estimated'[Exp_Sales] ),
ALLEXCEPT ( 'Estimated', 'Estimated'[DateColumn] ) //change to your date column
)
RETURN
DIVIDE ( 'Estimated'[Exp_Sales], maxSales )
Thank

Difference between current week of year and previous weeks

I have a SQL query which calculated the difference between the current weekday and previous weekdays in terms of weeks and I need to translate this to DAX query.
SQL :
DATEDIFF(Week,Getdate(),
DATEADD (WEEK, CAST (RIGHT(CAST ([Code] AS nvarchar),2) AS int),
DATEADD (YEAR, ([Code] / 100)-1900, 0)) - 4 -
DATEPART(dw,
DATEADD (WEEK, CAST (RIGHT(CAST ([Code] AS nvarchar),2) AS int),
DATEADD (YEAR, ([Code] / 100)-1900, 0)) - 4) + 1)
AS [WeekIndex]
enter code here
Expected Result: If code = 201821 then WeekIndex has to be -53 as the current week is week 22
I think you are looking for something like this:
[Measure] :=
VAR code = "201821"
VAR code_year = LEFT ( code, 4 )
VAR code_week = RIGHT ( code, 2 )
VAR code_date = DATE ( code_year, 1, -2 ) - WEEKDAY ( DATE ( code_year, 1, 3 ) )
+ code_week * 7
RETURN
DATEDIFF ( TODAY (), code_date, WEEK )
VAR [code_date] will return the monday of the given VAR [code] as a datevalue

Count how many sub-activities were created based on an activity

I have a dimension that stores workflows(cases, subcases). I would like to do a count of how many subcases are created for each case.
Workflow Dimension
Workflow
------------------------------
Case Number WorkflowType
------------------------------
10 Case
20 Case
30 Case
20-1 Subcase
20-2 Subcase
20-3 Subcase
10-1 Subcase
The desire output I would like is, for every case count how many subcases were created.
Workflow
------------------------------------------------
Case Number WorkflowType CountOfSubcases
------------------------------------------------
10 Case 1
20 Case 3
30 Case 0
------------------------------------------------
Total 4
I have a current dax measure that works, but the total at the bottom does not show when looking at multiple rows, only display when one case is selected.
Total Subcases =
VAR CC = FIRSTNONBLANK ( Workflow[Case Number], 1 )
RETURN
COUNTX (
FILTER (
ALL( Workflow ),
SUBSTITUTE ( Workflow[Case Number], RIGHT ( Workflow[Case Number], 2
), "" )
= CC
&& Workflow[WorkflowType] = "SubCase"
),
Workflow[WorkflowID]
)
If anybody could help me tweak my measure or present with a new measure, that would be great.
Note: I'm pointing my report to Analysis Services.
Thanks in advance.
You can fix your measure as follows:
Total Subcases = 0 +
COUNTX (
FILTER (
ALL( Workflow ),
SUBSTITUTE ( Workflow[Case Number], RIGHT ( Workflow[Case Number], 2 ), "" )
IN VALUES( Workflow[Case Number] )
&& Workflow[WorkflowType] = "SubCase"
),
Workflow[WorkflowID]
)
The VALUES function returns a list of all the values in the current filter context instead of just the one you were picking before.
Note: To make things easier to work with, I'd suggest splitting the Case Number column into two columns in the query editor stage. Then you don't have to work with all the string manipulation.
Edit: Note that x IN <Table[column]> is equivalent to the older CONTAINS syntax:
CONTAINS(Table, [column], x)
So if you can't use IN then try this formulation:
Total Subcases = 0 +
COUNTX (
FILTER (
ALL( Workflow ),
CONTAINS(
VALUES( Workflow[Case Number] ),
Workflow[Case Number],
SUBSTITUTE ( Workflow[Case Number],
RIGHT ( Workflow[Case Number], 2 ), "" )
)
&& Workflow[WorkflowType] = "SubCase"
),
Workflow[WorkflowID]
)

Powerpivot: Retrieve max value for a group in a related table

I have 2 tables with a one-to-many relationship.
-TableGroup: table with groupletter
-TableAll: table with unique identifier, groupletter, a date
Problem: I want to get the max value of the date from TableAll into a new column in TableGroup. See below.
Question: What is the formula for column MAXdate?
TableAll:
ID | Group | date
1 A 4/01/2017
2 A 2/10/2016
3 A 2/06/2016
4 B 2/12/2016
5 B 15/12/2016
6 B 2/03/2017
7 C 5/02/2016
8 C 16/01/2016
TableGroup:
Group | MAXdate
A 4/01/2017
B 2/03/2017
C 5/02/2016
The below formula doesn't work:
See here
The answer is:
CALCULATE (
MAX ( TableAll[Date] ),
FILTER ( TableAll, TableAll[Group] = EARLIER ( TableGroup[Group] ) )
)
Try:
CALCULATE (
MAX ( TableAll[Date] ),
FILTER ( TableGroup, TableGroup[Group] = EARLIER ( TableGroup[Group] ) )
)
How it works:
EARLIER ( TableGroup[Group] ) expression essentially means "current row". Filter function goes row by row over TableGroup table, filters it by current row's group, and then finds max date for that group.

Resources