DAX column count latest record for each set of group with a condition - powerbi

I want to get Latest updated record which is bit tricky to retrieve using DAX column with power bi
Count -> Order Count based on Modified On(Datetime) with Ascending Order
Deleted -> a Flag set to be True for deleted record
Id
Name
Modified On
Deleted
Count
Result
1
Charles
09-11-2022 15:09:40
1
1
09-11-2022 15:46:33
True
2
1
Charles M
09-11-2022 20:39:40
3
True
2
Charles
09-11-2022 15:09:40
1
2
09-11-2022 15:46:33
True
2
2
Charles M
09-11-2022 20:39:40
3
2
09-11-2022 21:16:33
True
4
2
charles m
09-11-2022 21:18:33
5
3
Dani
09-11-2022 15:46:33
1
True
3
09-11-2022 21:16:33
True
2
4
George
09-11-2022 15:46:33
1
4
George K
09-11-2022 21:16:33
2
In the above example I wanted the Result column values as it is on above table.
explanation:
Here Id : 1, The record is two times created as well as deleted so the history of record will have four rows. I wanted the last updated record which is the 3rd row and not the last record because the is Deleted flag is set to be True so there is no Name on it.
as so on for the second but Id:2
If the last insert on the record is not deleted then whole result column of the id should not return anything
(Id: 3)
In the third set there is there is no update on the record with this history table. the first row is created and second is for the deletion. so we should have to retrieve the first record which only have that data on Name field
Id: 4
There is no deletion operation happened so we don't want to get that record. the result columns should be empty
Thanks in advance
I have tried to get the latest record with
LatestDeletedRecord =
VAR latest = CALCULATE(MAX('Table'[Column3]), ALLEXCEPT('Table','Table'[Id]))
RETURN IF('Table'[Column3] = latest && 'Table'[IsDeleted] = True,True)
Other than nothing I could, I am new to DAX calculations

Edit: If your requirements change, you should perhaps post a new question instead of editing your existing question :-)
With your altered requirements you can use this calculated column:
Result =
VAR _max =
CALCULATE (
MAX ( 'Table'[Modified On] ) ,
ALLEXCEPT ( 'Table' , 'Table'[Id] )
)
VAR _max_is_deleted =
CALCULATE (
SELECTEDVALUE ( 'Table'[Deleted] ) ,
ALLEXCEPT ( 'Table' , 'Table'[Id] ) ,
'Table'[Modified On] = _max
)
VAR _max_mod =
// Calculate the maximum modified date where name is not deleted
CALCULATE (
MAX ( 'Table'[Modified On] ) ,
ALLEXCEPT ( 'Table' , 'Table'[Id] ) ,
'Table'[Name] <> ""
)
RETURN
IF (
// For rows where ID has an associated deletion AND modified is max with name
_max_is_deleted
&& [Modified On] = _max_mod,
// Return "True"
"True"
)
Gives your desired result:

Related

RANKX in measures

I have a table with 2 columns: client and product_name.
I need to number the product_name for each client
client
product_name
rank
1
aaa
1
1
baa
2
1
cwe
3
2
te
1
3
aaa
1
3
cwq
2
I created a column
RANKX_column =
RANKX(
FILTER(Query1,Query1[client_id] = EARLIER(Query1[client_id])),
Query1[product_id],,ASC,Dense
)
but if I apply a filter, the rank is not recalculated.
I tried to rewrite this formula for measure, but it returns an error about the function EARLIER.
Try something like:
=
VAR ThisClientID =
MIN( Query1[client_id] )
RETURN
RANKX(
FILTER( ALL( Query1 ), Query1[client_id] = ThisClientID ),
CALCULATE( MIN( Query1[product_id] ) ),
,
ASC,
DENSE
)
It is better to do it using Power Query:
You need first to group by columns [Client] -- See picture:
Resulting Table:
Then create a custom column, Here is the M Code:
Table.AddIndexColumn([AllData],"Rank",1,1)
Resulting Table:
Then Combine (Union) the all tables using The M Code:
= Table.Combine(#"Added Custom"[Rank])
Then close & apply. Return to your Main menu:
If you check your data view , The table there seems:

DAX - Retrieve a value from another unrelated table

I have two tables: 'Events' and 'Occurrences'.
In Events, I have the name of the event, start date and end date.
In 'Occurrences' I have a date from occurrence, ID occurrence and description of occurrence.
Events Table
Event
Start Date
End Date
Event 1
01/01/2022
02/14/2022
Event 2
02/15/2022
03/10/2022
Event 3
02/11/2022
03/30/2022
Occurrence Table
ID Ocurrence
Occurrence Create Date
Description
1
01/10/2022
Foo 1
2
02/11/2022
Foo 2
3
02/20/2022
Foo 3
4
03/20/2022
Foo 4
5
03/30/2022
Foo 5
My Question is: How can I retrieve which event is each occurrence?
In this example, the expected result is:
ID Ocurrence
Occurrence Create Date
Description
Event Related
1
01/10/2022
Foo 1
Event 1
2
02/11/2022
Foo 2
Event 1
3
02/20/2022
Foo 3
Event 2
4
03/20/2022
Foo 4
Event 3
5
03/30/2022
Foo 5
Event 3
add this measure to your table...
Event Related =
VAR _occ =
SELECTEDVALUE ( 'Table (2)'[Occurrence Create Date] )
RETURN
CALCULATE (
FIRSTNONBLANK ( 'Table'[Event], 1 ),
FILTER (
ALL ( 'Table' ),
'Table'[Start Date] <= _occ
&& 'Table'[End Date] >= _occ
)
)
or if you want to add only as a column to the table, not the visual, you can use this calculated column (more or less the same)
Event Related 2 =
var _occ = 'Table (2)'[Occurrence Create Date]
return
CALCULATE (
FIRSTNONBLANK ( 'Table'[Event], 1 ),
FILTER (
ALL ( 'Table' ),
'Table'[Start Date] <= _occ
&& 'Table'[End Date] >= _occ
)
)

Compare 2 rows in same table of PowerBI

I am trying to create a column to see if an ID equals another ID with the same month. It should show "NO" but if an ID equals another ID with a different month. It should show "YES". Other show "NO". see as below
ID Month Duplicate
1 4 No
1 4 No
2 5 No
2 6 Yes
2 7 Yes
3 8 No
4 6 No
4 6 No
4 7 Yes
4 8 Yes
5 6 No
5 6 No
5 6 No
My code like this
Duplicate =
IF (
COUNTROWS ( FILTER ( Data, Data[Policy No] = EARLIER( Data[Policy No]) ) )
> 1
&& COUNTROWS ( FILTER ( Data, Data[Month] < EARLIER(Data[Month]) ) ),
"YES",
"NO")
but it is not correct because when I select the first month as month 4 it shows no but when I select month 5 it shows yes. Like as below
ID month Duplicate
1 4 No
1 4 No
2 5 Yes
2 5 Yes
Please help me resolve this
Thank you
Not sure I understand the logic you want to implement but i THINK this will do what you're looking for (providing I did understand...).
I prefer to use variables rather than the EARLIER function. IMHO it makes the code easier to understand and more readable, especially for others.
This is a calculated column in your 'Data' table
Check =
var _id = [ID]
var _month = [Month]
var _check_both =
CALCULATE(
COUNTROWS('Data'),
FILTER(
ALL('Data'),
'Data'[ID] = _id && 'Data'[Month] = _month
)
)
var _check_id =
CALCULATE(
COUNTROWS('Data'),
FILTER(
ALL('Data'),
'Data'[ID] = _id
)
)
return
SWITCH(
TRUE(),
_check_both > 1, "No",
_check_id > 1, "Yes",
"NO"
)

Power BI - Get date when sum is 0

I have two tables - Customer and Transaction. The transaction holds invoices and payments. I need to get the date when the transaction sum is 0 or grater.
So the expected result should be John 02-20-2021. How can I achieve this?
customerID
Name
1
John
2
Ben
customerID
value
date
1
-150
02-13-2021
1
100
02-14-2021
1
200
02-20-2021
1
10
02-23-2021
You can try this out, by adding some new measures.
First measure to calculate the account balance over dates, returning a value only for positive balance:
Account Balance :=
VAR _date = MAX ( Transaction[date] )
VAR _balance =
CALCULATE (
SUM ( Transaction[value] ) ,
Transaction[date] <= _date
)
RETURN
IF ( _balance >= 0 , _balance )
The second filters the original table based on this measure and returns the earliest date:
Break Even Date :=
MINX (
FILTER (
VALUES ( Transaction[date] ),
[Account Balance]
),
[date]
)
Result:
Do note that I have not tested this beyond your (simple) example.

Determining a Style Changeover by Machine using PowerBI

So I have a table that has the output of all machines in a department with styles. For example:
|Machine| |Style| | QTY| |Time| |Date| etc...
1 001 100 8:00AM 5/21/19
2 001 200 8:05AM 5/21/19
1 001 100 9:00AM 5/21/19
1 004 100 10:00AM 5/21/19
2 001 200 9:05AM 5/21/19
I'm looking to see the amount of times a style is changed for a machine. So in this case, for Machine 1 it was one style change and for Machine 2 it was zero.
I've tried adapting some code to no avail; mainly because I'm having trouble understanding the logic and I can't really think of a good index to work with.
Here is what I got so far:
EarliestChange Over Index =
VAR Temp =
CALCULATE (
MAX ( Table[Index] ),
FILTER (
Table,
[Index] < EARLIER ( [Index] )
&& [Style] <> EARLIER ( [Style])
&& Table[Date] = today()-1
)
)
VAR Temp1 =
CALCULATE (
MIN ( [Index] ),
FILTER (
Table,
[Index] > EARLIER ( [Index] )
&& [Style] <> EARLIER ( [Style])
&& Table[Date] = today()-1
)
)
RETURN
IF ( [Index] > temp && OR ( [Index] < temp1, ISBLANK ( temp1 ) ), temp + 1, 0 )
I tried to restrict it to just one day so that I could evaluate the results so that portion can be dropped. I've tried two different indexes, one was the machine number and the other was the difference in time from today and the min date on the table. In a visual, I've been taking a distinct count of the EarliestChange Over Index and subtracted one since it didn't constitute a "change over."
EDIT:
Issue where multiple styles are logged at the same time causing false change overs.
|Machine| |Style| | QTY| |Time| |Date| etc...
1 001 100 8:00AM 5/21/19
1 001 100 9:00AM 5/21/19
1 004 100 10:00AM 5/21/19
1 004 100 10:00AM 5/21/19
1 004 100 10:00AM 5/21/19
In one department a time would never be duplicated. However, in another department (for whatever reason) might log 3 rolls at the same time. This would cause the equation to log 10:00am as 3 change overs. It might be a system glitch why there isn't unique time stamps per roll but this is the case unfortunately.
One way of doing it:
First, I modified your data as follows:
Added a record for Machine 1 at 11:00AM to capture a situation when a style reverts to the old one;
Added a column for Date-Time (simply Date + Time), to make life easier;
Named the table as "Data"
Measure:
Style Change Count
=
SUMX (
Data,
VAR Current_DateTime = Data[Date-Time]
VAR Current_Style = Data[Style]
VAR Previous_DateTime =
CALCULATE (
MAX ( Data[Date-Time] ),
FILTER ( ALLEXCEPT ( Data, Data[Machine] ), Data[Date-Time] < Current_DateTime )
)
VAR Previous_Style =
CALCULATE (
VALUES ( Data[Style] ),
FILTER ( ALLEXCEPT ( Data, Data[Machine] ), Data[Date-Time] = Previous_DateTime )
)
RETURN
IF ( Current_Style = Previous_Style || ISBLANK ( Previous_Style ), 0, 1 )
)
Result:
How it works:
We need to use SUMX to make sure that our subtotals and totals are correct;
SUMX iterates over Data table and for each record computes "Previous date-time", which is simply the max datetime less than the current datatime, per machine (hence ALLEXCEPT);
Then, we can calculate Previous Style, which is a style where date-time = previous date-time;
Finally, we compare current style and previous style. If they are not the same, we add 1;
In addition, I added a test for the starting condition - first occurrence of a machine, for which previous style does not exist yet. I did not treat such records as "style change". If you want to count initial records as style change, remove ISBLANK() part.