Forum Discussion
Help with DAX power BI
- 7 months ago
Hi Syndicate_Admin , Thank you for reaching out to the Microsoft Community Forum.
Please refer below sample spreadsheet snap:
Detenciones_Limpias snap:
Detenciones_Con_Producción snap:
Please refer attached .pbix file and output snaps and share your thoughts:
@Syndicate_Admin wrote:Hello @Syndicate_Admin , I hope you're okay! Can we tell if it worked for you or are you still struggling? Tell us: your feedback can help others in the same situation a lot.
Hello, I have modified and applied the code in M language that a colleague gave me which is the following:Let me
// 1. Reference to your original table
Base = Arrests,// 2. Create combined Date and Time columns
AddStartDateTime = Table.AddColumn(
Base,
"StartDateTime",
each DateTime.From([#"Start Date"]) + Duration.From([#"Start Time"]),
type datetime
),
AddEndDateTime = Table.AddColumn(
AddStartDateTime,
"EndDateTime",
each DateTime.From([#"Start Date"]) + Duration.From([#"End Time"]),
type datetime
),// 3. Sort Data (Vital for Previous Row Logic)
SortedRows = Table.Sort(
AddEndDateTime,
{
{"Start Date", Order.Ascending},
{"Line", Order.Ascending},
{"StartDateTime", Order.Ascending}
}
),// 4. Add Table of Contents to compare with the previous row
AddIndex = Table.AddIndexColumn(SortedRows, "Index", 0, 1, Int64.Type),// 5. Create the Group ID using a custom function to avoid slow "Join"
This part identifies whether the current row belongs to the previous group or is a new one
AddGroupFlag = Table.AddColumn(AddIndex, "NewGroupFlag", each
Let me
CurrentRow = AddIndex{[Index]},
PreviousRow = if [Index] > 0 then AddIndex{[Index]-1} else null,
IsNewGroup =
if PreviousRow = null then 1
else if CurrentRow[Line] <> PreviousRow[Line] then 1
else if CurrentRow[#"Start Date"] <> PreviousRow[#"Start Date"] then 1
else if CurrentRow[#"Level 2"] <> PreviousRow[#"Level 2"] then 1
else if Duration.TotalSeconds(CurrentRow[StartDateTime] - PreviousRow[EndDateTime]) > 30 then 1
else 0
in
IsNewGroup, Int64.Type
),// 6. Create a Running Total ID for Flags
This groups consecutive rows under a single ID
AddGroupID = Table.AddColumn(AddGroupFlag, "GroupID", each List.Sum(List.FirstN(AddGroupFlag[NewGroupFlag], [Index] + 1)), Int64.Type),// 7. Group by GroupID
GroupedStops = Table.Group(
AddGroupID,
{"GroupID", "Start Date", "Line", "Level 2"},
{
{"Start Time", each List.Min([#"Start Time"]), type time},
{"End Time", each List.Max([#"End Time"]), type time},
{"Time in Minutes", each List.Sum([#"Time in Minutes"]), type number}
}
),Final cleanup: Remove the group ID column if you don't need it
RemovedGroupID = Table.RemoveColumns(GroupedStops,{"GroupID"})
in
RemovedGroupID
However, I have not been successful, I do not know if I translated the code correctly so that it is used correctly. I remain attentive
Hi Syndicate_Admin , can you confirm if you tried the suggestions in my last reply on 17th December? that would help us understand the issue.
- Syndicate_Admin8 months ago
Administrator
The code I sent was what I used that I think is what you have suggested, without much success (if I had translated the code well since it was not arrive and paste in my case)
- v-hashadapu8 months ago
Community Support
Hi Syndicate_Admin , Thank you for reaching out to the Microsoft Community Forum.
Since your report uses two data sources that we cannot access, we’re unable to reproduce the scenario successfully. Based on the details you shared, we suggested a solution using M code, but if that approach is not working for you, we need sample data to investigate further.
To proceed, please provide two separate Excel files that we can download and use as data sources. Include only the columns and rows required to demonstrate the issue and exclude anything confidential. Please also show what the expected outcome should be based on this sample data.
- Syndicate_Admin8 months ago
Administrator
https://drive.google.com/drive/folders/1QAOyxxynKT1_dznhEGEUgz2olknCJ6J1?usp=drive_link
Generate this folder in drive with all the files to be used (no confidential information exists)
Now, I upload two example images as to what I'm looking for (they're just examples)
It happens that on several occasions I have imputations (of different types) which are cut and generate two "inputs" or inputs to which I would like to include a way that in its logic can join these imputations into a single one if it detects for example that it is a continuous failure (first case goes from 21:32 to 22:40, then from 22:40 to 23:06 but it was not imputed as set up and so on until we reach again an imputation that is typed as set up at 23:10) So in principle I don't know if you can make a kind of sandwich in those cases when I detect a linearity in the data.
The second scenario shows two continuous detentions separated by only a couple of minutes but it is the same imputation, the desire is the same... that, for example, if there is a difference of 5 minutes (at most) between each detention that unifies them, this criterion will be passed that they remain separate.
Finally, another goal I have with this is to be able to relate the database to the arrests. As you can see in all productions there are hours as in the stop sheet, so depending on the stop time I want you to identify in the other sheet the SKU that was being produced in that time interval.
I remain attentive to your comments, thank you very much in advance.
Best regards