Forum Discussion
Help to solve the below logic using power bi
If I interpret the logic correctly, then you need:
all records with status "Completed", and
all records with status "Failed"
if there is no record for the same person with status "Completed" within +/- 4 hours
all records with status "Incomplete"
if there is no record for the same person with status "Completed" or with status "Failed" within +/- 4 hours
I put your data in an Excel file and created the Power Query (M) code below.
To be honest I have my doubts about performance, but you may give it a try on some test data.
let
Source = Excel.Workbook(File.Contents("C:\Users\Marcel\Documents\Forum bijdragen\Power BI Community\Help solve logic.xlsx"), null, true),
Input_Table = Source{[Item="Input",Kind="Table"]}[Data],
Typed = Table.TransformColumnTypes(Input_Table,{{"DateTime", type datetime}, {"Name", type text}, {"Status", type text}}),
#"Added Custom" = Table.AddColumn(Typed, "Keep",
(x) => if x[Status] = "Completed"
then true
else if x[Status] = "Failed"
then 0 = Table.RowCount(Table.SelectRows(Typed, each [Name] = x[Name] and
[Status] = "Completed" and
[DateTime] >= x[DateTime] - #duration(0,4,0,0) and
[DateTime] <= x[DateTime] + #duration(0,4,0,0)))
else 0 = Table.RowCount(Table.SelectRows(Typed, each [Name] = x[Name] and
([Status] = "Completed" or [Status] = "Failed") and
[DateTime] >= x[DateTime] - #duration(0,4,0,0) and
[DateTime] <= x[DateTime] + #duration(0,4,0,0)))
),
#"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([Keep] = true)),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Keep"})
in
#"Removed Columns"
- shamsuddeenvp9 years agoPost Patron
Thanks a lot MarcelBeug
This is the logic what I am exactly looking for. Great.
However, it takes long time since my data is more (in lak).. I will have to try some alternate solution.
Br,
Shams
- shamsuddeenvp9 years agoPost Patron
I had done this using DAX queries.
Br,
Shams
- Phil_Seamark9 years agoMicrosoft Employee
To address your performace issues, you need to split your date column in to two columns.
The other should carry the Hour or Minute of the day as an integer. This will make a massive difference over the size of your model and make loading and calculations much faster.
Then you can do the rest in DAX or PQ. When the actions are within 4 hours on the same day it's easy. If they split a day, you just need a basic IF statement to handle that.
If you have millions of rows, you have to avoid having columns that are highly unique like your datetime column.