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:
Hi Syndicate_Admin , hope you are doing great. May we know if your issue is solved or if you are still experiencing difficulties. Please share the details as it will help the community, especially others with similar issues.
- Syndicate_Admin8 months ago
Administrator
I still can't figure out how to transfer the logic they tell me about to a power query or DAX, I've tried but without much success.
- v-hashadapu8 months ago
Community Support
Hi , Thank you for reaching out to the Microsoft Community Forum.
You are connecting to Detention sheet(data source1) and Production database distributes (data source2), i tried to replicate the scenario, but getting the below error, due to "DataSource.NotFound".
I can't connect to your data source from my end. Please refer below M code. and follow below steps.
1. In power desktop--> Query editor.
2. select New Source --> Blank Query --> In advanced editor.
Remove everything and paste the below code:
let
// BASE TABLE (reference existing Detenciones query)
Base = Detenciones,
// CREATE DATETIME COLUMNS
AddStartDateTime = Table.AddColumn(
Base,
"StartDateTime",
each DateTime.From([#"Fecha de Inicio"])
+ Duration.From([#"Hora de Inicio"]),
type datetime
),AddEndDateTime = Table.AddColumn(
AddStartDateTime,
"EndDateTime",
each DateTime.From([#"Fecha de Inicio"])
+ Duration.From([#"Hora de Finalización"]),
type datetime
),// SORT DATA (CRITICAL FOR SEQUENTIAL LOGIC)
SortedRows = Table.Sort(
AddEndDateTime,
{
{#"Fecha de Inicio", Order.Ascending},
{#"Línea", Order.Ascending},
{"StartDateTime", Order.Ascending}
}
),// ADD INDEX
AddIndex = Table.AddIndexColumn(
SortedRows,
"Index",
0,
1,
Int64.Type
),// SELF-JOIN TO PREVIOUS ROW
JoinPrevious = Table.NestedJoin(
AddIndex,
{"Index"},
AddIndex,
{"Index"},
"PrevRow",
JoinKind.LeftOuter
),ExpandPrev = Table.ExpandTableColumn(
JoinPrevious,
"PrevRow",
{"Línea", "Fecha de Inicio", "Nivel 2", "EndDateTime"},
{"PrevLínea", "PrevFecha", "PrevNivel2", "PrevEndDateTime"}
),// FLAG NEW GROUP
AddNewGroupFlag = Table.AddColumn(
ExpandPrev,
"NewGroup",
each
if [PrevLínea] = null then 1
else if [#"Línea"] <> [PrevLínea] then 1
else if [#"Fecha de Inicio"] <> [PrevFecha] then 1
else if [#"Nivel 2"] <> [PrevNivel2] then 1
else if Duration.TotalSeconds(
[StartDateTime] - [PrevEndDateTime]
) > 30 then 1
else 0,
Int64.Type
),// CREATE GROUP ID (RUNNING TOTAL)
AddGroupID = Table.AddColumn(
AddNewGroupFlag,
"GroupID",
each
List.Sum(
List.FirstN(
AddNewGroupFlag[NewGroup],
[Index] + 1
)
),
Int64.Type
),// GROUP CONSECUTIVE DETENTIONS
GroupedDetentions = Table.Group(
AddGroupID,
{"GroupID", #"Fecha de Inicio", #"Línea", #"Nivel 2"},
{
{
"Hora de Inicio",
each Time.From(
DateTime.Time(
List.Min([StartDateTime])
)
),
type time
},
{
"Hora de Finalización",
each Time.From(
DateTime.Time(
List.Max([EndDateTime])
)
),
type time
},
{
"Tiempo en Minutos",
each List.Sum([#"Tiempo en Minutos"]),
type number
}
}
)in
GroupedDetentions
I hope this information helps. Please do let us know if you have any further queries.