Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Syndicate_Admin
Administrator
Administrator

Se requiere ayuda con dax

Hola, tengo un informe que involucra espacios a lo largo del día escolar que incluye M1 (lección del lunes 1), M2, M3, M4, M5, M6, M7, T1, T2, T3, T4, etc. sin embargo, también tenemos otras franjas horarias que no están numeradas, como MA (llegada del lunes), MB (desayuno del lunes), ML (almuerzo del lunes), TA (llegada del martes), TB (desayuno del martes).... etcetera.

Para tenerlos en el orden correcto y crear una línea de tiempo clara, he creado lo siguiente:

= Tabla.AgregarColumna(#"Tipo cambiado", "Índice de lección", cada uno let Dígitos = {"0".." 9"},
LessonSplit = Splitter.SplitTextByCharacterTransition(cada uno no List.Contains(Digits,_), each List.Contains(Digits,_))([Lección]),
DíaDeSemana = LecciónDividida{0},
Número = Número.Desde(LecciónDividida{1}),
DayOfWeekIndex = if DayOfWeek = "M" then 0 else if DayOfWeek = "T" then 1 else if DayOfWeek = "W" then 2 else if DayOfWeek = "TH" then 4 else 5
en
DayOfWeekIndex * 100 + Número)

Thisa se aplica correctamente a todas las lecciones reales (M1, M2, M3, M4... F6) pero todas las demás ranuras vienen como error.

¿Alguien puede ayudar con esto o sugerir una solución?

Gracias

5 REPLIES 5
Syndicate_Admin
Administrator
Administrator

Hola @VDS9 ,
Creo que la lógica de la columna Índice de lecciones asume que todos los valores de las lecciones contienen un número después del identificador del día (M1, T3). Sin embargo, las ranuras como MA, MB, ML causan errores ya que no tienen una parte numérica.
Puede modificar la fórmula para manejar ranuras numeradas y no numeradas asignando un índice independiente para las ranuras no numeradas.
Por favor, pruebe el siguiente código:

= Table.AddColumn(
    #"Changed Type", 
    "Lesson Index", 
    each let 
        Digits = {"0".."9"},
        LessonSplit = Splitter.SplitTextByCharacterTransition(each not List.Contains(Digits,_), each List.Contains(Digits,_))([Lesson]),
        DayOfWeek = try LessonSplit{0} otherwise null,
        NumberPart = try Number.From(LessonSplit{1}) otherwise null,
        DayOfWeekIndex = if DayOfWeek = "M" then 0 
                         else if DayOfWeek = "T" then 1 
                         else if DayOfWeek = "W" then 2 
                         else if DayOfWeek = "TH" then 4 
                         else if DayOfWeek = "F" then 5 
                         else null,
        // Handle non-numbered slots
        SlotIndex = if NumberPart = null then 
                        if [Lesson] = "MA" then 10 
                        else if [Lesson] = "MB" then 20 
                        else if [Lesson] = "ML" then 30 
                        else if [Lesson] = "TA" then 110 
                        else if [Lesson] = "TB" then 120 
                        else null 
                    else 
                        DayOfWeekIndex * 100 + NumberPart
    in 
        SlotIndex
)

Gracias. Iba a introducir el nuevo código dax pero ha habido un ligero cambio como en las cabeceras. Los datos provienen de un archivo de Excel con diferentes hojas. Sin embargo, aunque los encabezados son casi iguales, las primeras 3 hojas tienen 1 columna menos. Por ejemplo, las primeras 3 hojas tendrán: MA, MR, M1, M2, MB, M3, M4, ML, M5, M6, ME, MD y las hojas restantes tienen MA, MR, M1, M2, M3, MB, M4, M5, ML, M6, M7, ME, MD. Además, los viernes se ven así para todos: FA, FR, F1, F2, FB, F3, F4, FL, FF, FA, FO.

¿Podré seguir creando algo así? ¿O debido a los ligeros cambios no será posible?

Gracias

Hola @VDS9 ,

La clave es normalizar los encabezados de las columnas, lo que significa que deberá manejar las tres primeras hojas (que tienen menos columnas) y las columnas específicas del viernes de una manera que garantice una indexación coherente en todas las hojas. Puede lograr esto comprobando el recuento de columnas y ajustando su lógica en consecuencia.

= Table.AddColumn(
    #"Changed Type", 
    "Lesson Index", 
    each let 
        Digits = {"0".."9"},
        // Split the Lesson column by digits and non-digits
        LessonSplit = Splitter.SplitTextByCharacterTransition(each not List.Contains(Digits,_), each List.Contains(Digits,_))([Lesson]),
        DayOfWeek = try LessonSplit{0} otherwise null,
        NumberPart = try Number.From(LessonSplit{1}) otherwise null,
        DayOfWeekIndex = if DayOfWeek = "M" then 0 
                         else if DayOfWeek = "T" then 1 
                         else if DayOfWeek = "W" then 2 
                         else if DayOfWeek = "TH" then 4 
                         else if DayOfWeek = "F" then 5 
                         else null,
        // Determine if we are on the first three sheets or the others
        ColumnCount = Table.ColumnCount(#"Changed Type"),
        SlotIndex = if ColumnCount = 12 then // Sheets with 1 less column (first 3 sheets)
                        if NumberPart = null then 
                            if [Lesson] = "MA" then 10 
                            else if [Lesson] = "MR" then 20 
                            else if [Lesson] = "M1" then 30 
                            else if [Lesson] = "M2" then 40 
                            else if [Lesson] = "MB" then 50 
                            else if [Lesson] = "M3" then 60 
                            else if [Lesson] = "M4" then 70 
                            else if [Lesson] = "ML" then 80 
                            else if [Lesson] = "M5" then 90 
                            else if [Lesson] = "M6" then 100 
                            else if [Lesson] = "ME" then 110 
                            else if [Lesson] = "MD" then 120 
                            else null
                        else 
                            DayOfWeekIndex * 100 + NumberPart
                    else if ColumnCount = 13 then // Sheets with all columns (remaining sheets)
                        if NumberPart = null then 
                            if [Lesson] = "MA" then 10 
                            else if [Lesson] = "MR" then 20 
                            else if [Lesson] = "M1" then 30 
                            else if [Lesson] = "M2" then 40 
                            else if [Lesson] = "M3" then 50 
                            else if [Lesson] = "MB" then 60 
                            else if [Lesson] = "M4" then 70 
                            else if [Lesson] = "M5" then 80 
                            else if [Lesson] = "ML" then 90 
                            else if [Lesson] = "M6" then 100 
                            else if [Lesson] = "ME" then 110 
                            else if [Lesson] = "MD" then 120 
                            else null
                        else 
                            DayOfWeekIndex * 100 + NumberPart
                    else if ColumnCount = 10 then // Fridays with special slots
                        if NumberPart = null then 
                            if [Lesson] = "FA" then 10 
                            else if [Lesson] = "FR" then 20 
                            else if [Lesson] = "F1" then 30 
                            else if [Lesson] = "F2" then 40 
                            else if [Lesson] = "FB" then 50 
                            else if [Lesson] = "F3" then 60 
                            else if [Lesson] = "F4" then 70 
                            else if [Lesson] = "FL" then 80 
                            else if [Lesson] = "FF" then 90 
                            else if [Lesson] = "FO" then 100
                            else null
                        else 
                            DayOfWeekIndex * 100 + NumberPart
                    else null // Fallback for unexpected column counts
    in 
        SlotIndex
)

Hola @Bibiano_Geraldo gracias por su ayuda. He introducido el código que me has sugerido y, aunque no se han detectado errores, el índice ahora aparece como nulo para cada ranura.

¿Cuál crees que es el problema?

Syndicate_Admin
Administrator
Administrator

@VDS9 , Intente usar

m
= Table.AddColumn(#"Tipo cambiado", "Índice de lección", cada uno
dejar
Dígitos = {"0".." 9"},
LessonSplit = Splitter.SplitTextByCharacterTransition(cada uno no List.Contains(Digits,_), each List.Contains(Digits,_))([Lección]),
DíaDeSemana = LecciónSplit,
Number = try Number.From(LessonSplit) de lo contrario, null,
DayOfWeekIndex = if DayOfWeek = "M" entonces 0
de lo contrario, si DayOfWeek = "T", entonces 1
de lo contrario, si DayOfWeek = "W", entonces 2
de lo contrario, si DayOfWeek = "TH", entonces 4
De lo contrario, 5,
SlotIndex = si Número <> nulo, entonces DayOfWeekIndex * 100 + Número
de lo contrario, si Text.EndsWith([Lección], "A") entonces DayOfWeekIndex * 100 - 3
de lo contrario, si Text.EndsWith([Lección], "B") entonces DayOfWeekIndex * 100 - 2
else if Text.EndsWith([Lección], "L") entonces DayOfWeekIndex * 100 - 1
else null
en
Índice de ranuras
)

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.