Last status by date - powerbi

I have the following table of task runs with timestamp and status:
Timestamp
Task
Status
2022-08-19 12:34:11
1
success
2022-08-20 18:14:33
2
failure
2022-08-21 09:03:10
1
failure
2022-08-22 22:12:02
3
failure
2022-08-23 03:52:09
2
success
2022-08-24 15:38:42
3
success
I'm looking for the latest status by timestamp for each task plus the total number of tasks that are currently in "success" status.
The expected outcome is
Task
Status
1
failure
2
success
3
success
so 2 tasks are currently in status "success".

Let's first create the table:
Now This is the code:
Result =
ADDCOLUMNS (
VALUES ( YourTbl[Task] ),
"MaxDate", CALCULATE ( VALUES ( YourTbl[Status] ), LASTDATE ( YourTbl[Timestamp] ) ),
"SuccessCount", CALCULATE ( COUNTROWS ( YourTbl ), YourTbl[Status] = "success" )
)
The result It produces :

The time intelligence function LASTDATE() returns a table (albeit with only one value) and can be directly used as a filter in CALCULATE():
Last Status =
CALCULATE (
VALUES ( Tasks[Status] ),
LASTDATE ( Tasks[Timestamp] )
)
If this measure is filtered by Task (e.g. in a table) it returns the status from the last timestamp:
Counting the successful tasks is a bit trickier, since the above measure can't be filtered directly, but an intermediate table is needed:
Count Success =
VAR tbl =
ADDCOLUMNS (
VALUES ( Tasks[Task] ),
"Status", [Last Status]
)
RETURN
COUNTROWS(
FILTER(
tbl,
[Status] = "success"
)
)

Related

DAX Measure to count Rows if relation may not exist

I am looking for a DAX measure to solve the following problem:
Count the number of rows in the dimension table where the Fact table either has no rows or the score is 0.
Table A (Dimension Table)
ID
name
1
a
2
b
3
c
Table B (Fact Table)
ID
score
1
0
1
1
1
2
2
5
Expected Result
In this example, I would expect 2, as ID=1 has one row with score=0 and ID=3 as no corresponding row in the Fact Table.
I came up with this measure which gives me the number of rows that have no corresponding row in the fact table, but I am not able to integrate the first condition:
CALCULATE(COUNTROWS('Dimension'), FILTER ('Dimension', ISBLANK ( CALCULATE ( COUNT ('Fact'[id]) ) )))
Probably much more straightforward methods, but try this measure for now:
MyMeasure =
VAR MyTable =
ADDCOLUMNS(
Table_A,
"Not in Table_B", NOT (
Table_A[ID]
IN DISTINCT( Table_B[ID] )
),
"Zero Score in Table_B",
CALCULATE(
COUNTROWS( Table_B ),
Table_B[score] = 0
) > 0
)
RETURN
SUMX(
MyTable,
[Not in Table_B] + [Zero Score in Table_B]
)
You can also try this
CountID =
VAR ScoreZero =
COUNTROWS ( FILTER ( TableB, [score] = 0 ) )
VAR NonExistentIDs =
COUNTROWS ( EXCEPT ( DISTINCT ( TableA[ID] ), DISTINCT ( TableB[ID] ) ) )
RETURN
ScoreZero + NonExistentIDs
This also works, not sure it's a good idea to nest CALCULATE:
CALCULATE(COUNTROWS('Table_A'), FILTER ('Table_A', ISBLANK ( CALCULATE ( COUNT ('Table_B '[id]) ) ) || CALCULATE(COUNTAX(Filter('Table_B ','Table_B '[score]=0),'Table_B '[id])>=1)))

DAx Formula Counting tasks

I have a question regarding Dax formula in PowerBI. All help is appreciate. I need to present some results on monday...
Table example
Task column can have different fields (Partner Acknowledgment, IPM HandOff, Pre Engagement Process...)
Task status can only have 2 values (closed or Received)
I have my formulas that work but now I need to add an extra filter and is getting error:
THe Filter part is giving me problems (WHen task= IPM HandOff is Task_status = Closed)
Total Tasks = All the tasks (rows) that contains the word "Partner Acknowledgment" in the task column.....And I try to add a filter when "IPM HandOff" in the task column has task_status closed:
Tasks =
CALCULATE (
COUNTA ( CELERGO_12062021V1[TASK] ),
CONTAINSSTRING ( CELERGO_12062021V1[TASK], "Partner Acknowledgement" ),
FILTER (
CELERGO_12062021V1,
CELERGO_12062021V1[TASK] = "IPM HandOff"
&& CELERGO_12062021V1[TASK_STATUS] = "Closed"
)
)
Total Tasks with GLD = Same as the previous one but with a schedule Date in the column Schedule.....And I try to add a filter when "IPM HandOff" in the task column is Task_status = closed.
Partner Acknowledgment with GLD = CALCULATE ( COUNTA ( CELERGO_12062021V1[SCHEDULED] ), CONTAINSSTRING ( CELERGO_12062021V1[TASK], "Partner Acknowledgement" )) ,
FILTER (
CELERGO_12062021V1,
CELERGO_12062021V1[TASK] = "IPM HandOff"
&& CELERGO_12062021V1[TASK_STATUS] = "Closed"
)
)
Total Tasks with GLD = Same as the previous ones but with a schedule Date in the column Schedule and Task Status Closed for the task "Partner Acknowledgmeent".....And I try to add a filter when "IPM HandOff" in the task column is Task_status = closed.
Partner Acknowledgment with GLD & Closed = CALCULATE ( COUNTA ( CELERGO_12062021V1[SCHEDULED] ), CONTAINSSTRING ( CELERGO_12062021V1[TASK], "Partner Acknowledgement" ) && CELERGO_12062021V1[TASK_STATUS] = "Closed" ) ,
FILTER (
CELERGO_12062021V1,
CELERGO_12062021V1[TASK] = "IPM HandOff"
&& CELERGO_12062021V1[TASK_STATUS] = "Closed"
)
)
ANy idea got to add this extra field in the formulas? "WHen task= IPM HandOff is Task_status = Closed) Thanks :)
last part... Is it possible to get the formulas + extra filter counting only the tasks owned by some people (names??) I want to know the formulas below that belong to Taskactualperformer = Roger, Abbie and Veronika"?
Same table. I need to combined the 2 formulas below...
To get all the clients with
Task = "pre engagement Team" and Tast_Status (Closed and Received) - Formula 1
&&
Task = "IPM HandOff" and Task_Status is Received - Formula 2
Tried different options but no exit.
Formula1 = VAR result =
CALCULATE (
DISTINCTCOUNT( CELERGO_12062021V1[CID] ),
FILTER (
CELERGO_12062021V1,
CELERGO_12062021V1[TASK]
= "Pre Engagement Process"
&& CELERGO_12062021V1[TASK_STATUS] IN { "Closed", “Received” }
)
)
RETURN
IF ( ISBLANK ( result ), 0, result )
FOrmula 2 =
VAR result =
CALCULATE (
COUNT ( CELERGO_12062021V1[CLIENT_NAME] ),
FILTER (
CELERGO_12062021V1,
CELERGO_12062021V1[TASK] = "IPM HandOff"
&& CELERGO_12062021V1[TASK_STATUS] = "Received"
)
)
RETURN
IF ( ISBLANK ( result ), 0, result )
last part... Is it possible to get the formulas + extra filter counting only the tasks owned by some people (names??) I want to know the formulas below that belong to Taskactualperformer = Roger, Abbie and Veronika"?
Thanks
I would recommend splitting those measures into simple ones.
As I understand, you want to count the number of rows that contain 'Partner Acknowledgment' in the task column and add the number of rows that have the value "closed" in the status column and the value 'IPM HandOff' in the task column. Then you can try this:
Count rows:
Total Tasks =
COUNTROWS ( 'TableA' )
Count rows where tasks contain 'Partner Acknowledgment':
Partner_Acknowledgment_Total =
CALCULATE (
[Total Tasks],
CONTAINSSTRING ( 'TableA'[task], "Partner Acknowledgment" )
)
Count rows where tasks are equal to 'IPM HandOff' and status = 'closed'
IPM_HandOff_closed_Total =
CALCULATE (
[Total Tasks],
'TableA'[task] = "IPM HandOff",
'TableA'[status] = "closed"
)
Final measure:
Partner_Acknowledgment+IPM_HandOff_closed =
[Partner_Acknowledgment_Total] + [IPM_HandOff_closed_Total]
Measures are for the table:

DAX subquery measure?

I'm struggling with a DAX query and wondered if you could help?
Consider this table (or visualisation) called 'Builds':
Build....App....Status
Build1...App1...UAT
Build1...App2...Complete
Build2...App1...Complete
Build2...App2...Complete
I would like to add a measure column called 'AppsOutstanding' to show a count of Apps for that Build that aren't 'Complete'. Like so:
Build....App....Status......AppsOutstanding
Build1...App1...UAT.........1
Build1...App2...Complete....1
Build2...App1...Complete....0
Build2...App2...Complete....0
I almost need to do a 'subquery' measure!? Something like:
SELECT COUNT(Status) FROM Builds
WHERE Build = [The Build In This Row]
AND Status <> 'Complete'
I'm a bit stumped how to translate this into DAX? Here is my unsuccessful attempt:
AppsUnavailable = CALCULATE (
count(Builds[Build]),
CALCULATETABLE (
SUMMARIZE ( Builds,Builds[Status] ),
Builds[Status] <> "Complete"
))
Thanks in advance!
UPDATE
I've tried this, but the count isn't working, and also this DAX filters "Complete" statuses out of my actual results! Which i don't want. I only want to filter the "Complete" statuses out of my count measure....
AppsUnavailable =
CALCULATE (
COUNT ( Builds[Build] ),
FILTER (
ALL ( Builds[Build] ),
Builds[Build] = SELECTEDVALUE ( Builds[Build] )
),
FILTER (
ALL ( Builds[Status] ),
Builds[Status] <> "Complete"
)
)
UPDATE 2
I think I'm doing something fundamentally wrong. I've really dumbed it down to just find other 'Builds' with the same name, and it still only returns 1's!
AppsUnavailable =
CALCULATE (
COUNT ( Builds[Build] ),
FILTER (
ALL ( Builds[Build] ),
Builds[Build] = SELECTEDVALUE ( Builds[Build] )
)
)
UPDATE 3
This query (when testing on a single table (no joins) sample) produces this:
Build....App....Status......AppsOutstanding
Build1...App1...UAT.........1
Build1...App2...Complete....0
Build1...App2...UAT.........1
Build2...App1...Complete....0
Build2...App2...Complete....0
But i actually need this:
Build....App....Status......AppsOutstanding
Build1...App1...UAT.........2
Build1...App2...Complete....0
Build1...App2...UAT.........2
Build2...App1...Complete....0
Build2...App2...Complete....0
So Build1 has 2 Apps that are not complete.
I then need to look into why I'm getting all 1's in the 'live' environment. It must be to do with filtering on 2 tables as opposed to 1....
You can replace [The Build In This Row] with SELECTEDVALUE ( Builds[Build] ).
Try this:
AppsUnavailable =
CALCULATE (
COUNT ( Builds[App] ),
FILTER (
ALL ( Builds[Status], Builds[Build] ),
Builds[Build] = SELECTEDVALUE ( Builds[Build] )
&& Builds[Status] <> "Complete"
)
) + 0
powerBI Table visualization internal use SUMMARIZECOLUMNS which is removed row without data from the measure. You have 2 options:
-Right click on GroupBy Column -> "Show item with no data"
-Add +0 to calculation after the last parenthesis
UPDATE:
CALCULATE (
COUNT ( YourTable[App] ),
FILTER (
ALL ( YourTable[App], YourTable[Build], YourTable[Status] ),
YourTable[Build] = SELECTEDVALUE ( YourTable[Build] )
&& YourTable[Status] <> "Complete"
)
) + 0

How to summarize data into table filtering by dates

I need to summarize dax table and filter it by date range 5 month back from last EffectiveDate, which is 7/27/2019
So my dax expression:
TestTable1 =
VAR LastEffDate = LASTDATE(fact_Premium[EffectiveDate]) // -- 7/27/2019
RETURN
SUMMARIZE(
FILTER(dim_Date, DATEDIFF(DATEADD(STARTOFMONTH(LastEffDate), -5,MONTH), ENDOFMONTH(LastEffDate), MONTH)),
dim_Date[Year Month],
"Premium", [Ttl WP]
)
But for some reason it brings me data for all years in a dataset:
I also tried:
TestTable1 =
VAR LastEffDate = LASTDATE(fact_Premium[EffectiveDate]) // -- 7/27/2019
RETURN
SUMMARIZE (
FILTER (
dim_Date,
DATESBETWEEN(dim_Date[Date],
DATE(2019,5,1),
DATE(2019,6,1)
)
),
dim_Date[Year Month],
"Premium", [Ttl WP]
)
But it gives me an error:
A table of multiple values was supplied where a single value was expected.
Am I missing something here?
Your filter expression should be using DATESBETWEEN and not the datediff:
TestTable1 =
VAR LastEffDate = LASTDATE(fact_Premium[EffectiveDate]) // -- 7/27/2019
RETURN
SUMMARIZE (
FILTER (
dim_Date,
DATESBETWEEN(
//expression for start date,
//expression for end date
)
),
dim_Date[Year Month],
"Premium", [Ttl WP]
)
I think you are still missing aggregating your Premium,
SUM(Ttl WP)
TestTable1 =
VAR LastEffDate =
LASTDATE ( fact_Premium[EffectiveDate] ) // -- 7/27/2019
RETURN
SUMMARIZE (
FILTER (
dim_Date,
DATESBETWEEN ( dim_Date[Date], DATE ( 2019, 5, 1 ), DATE ( 2019, 6, 1 ) )
),
dim_Date[Year Month],
"Premium", SUM ( [Ttl WP] ) ---- Need to aggregate here
)

How to get the difference in days(row wise) in power bi for each id per version

I need to calculate the difference between days (row wise) for each ID, each Iteration where the first record of each ID and each version is 0. I tried few code snippets, but didn't worked such as:
Calculated Time =
VAR Index = Table[Version]
VAR Reference = Table[Id]
VAR Prevtime =
CALCULATE (
FIRSTNONBLANK ( Table[Date], TRUE () ),
FILTER (Table, Table[Id] = Reference && Table[Version] = Index)
)
RETURN
Table[Date] - Prevtime
Desired outcome:
ID Version Date Calculated time Formula
12345 1 11/1/2018 8:08 0 0
12345 1 11/16/2018 8:39 15.02152778 =C3-C2
12345 1 11/16/2018 13:29 0.201388889 =C4-C3
12345 1 11/16/2018 13:32 0.002083333
12345 2 11/16/2018 8:39 0
12345 2 11/26/2018 14:24 10.23958333
12345 2 11/26/2018 14:24 0
12345 3 11/16/2018 8:39 0
12345 3 12/6/2018 12:13 20.14861111
12345 3 12/6/2018 12:20 0.004861111
Any inputs are appreciated. Please let me know if i need to do some changes to the above code or if you can assist me
I think you are looking for something like this:
It works like this:
ALLEXCEPT ( 'Table', 'Table'[ID], 'Table'[Version] ) returns the 'Table' table, filtered on the [ID] and [Version] of the current row. The CALCULATE funtion then calculates the biggest [Date] from that filtered table, that is smaller than [Date] in the current row.
The EARLIER function is used to go to the previous filtercontext, which is the rowcontext, to get the [Date] value form the current row.
So VAR prevTime is the previous [Date] with the same [ID] and [Version]. When no previous [Date] exists, prevTime is BLANK.
Calculated time =
VAR prevTime =
CALCULATE (
MAX ( 'Table'[Date] ),
ALLEXCEPT ( 'Table', 'Table'[ID], 'Table'[Version] ),
'Table'[Date] < EARLIER ( 'Table'[Date] )
)
RETURN
IF ( ISBLANK ( prevTime ), BLANK (), 'Table'[Date] - prevTime )