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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Syndicate_Admin
Administrator
Administrator

Extracción de subcadenas de texto libre

Hola

Búsqueda de ayuda para codificar lo siguiente en Power Query (preferido) o DAX.

Tabla de entrada:

Identificación; Texto libre

Orden123; PN113 y PN224 se utilizaron para cumplir con el pedido. Sin embargo, no había stock de PN113. PN224 estaba bien.

Orden124; Había un problema. No se encontró PN445. Para resolver esto, se utilizó PN556 en lugar de PN445.

Tabla de salida:

Orden123; PN113

Orden123; PN224

Orden124; PN445

Orden124; PN556

El objetivo es buscar "PN" y listar el PN completo que aparece en el texto libre. Cada PN está estandarizado para tener siempre 5 caracteres.

1 ACCEPTED SOLUTION
Syndicate_Admin
Administrator
Administrator

Aquí hay una forma cruda de hacer esto. Se supone que la cadena "PN" no aparece en ningún otro lugar del texto. Así que "El conductor estaba hipnotizado" no funcionaría.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("PY47DoMwEESvMqK2kACTM6QiFHSIwoK1QFjeyB+4fmyS0O7OvDfjWLzcQq6qm0IUfVdVDZRd0Hd1LXGSI0RPCwJDR6M3g7ASOFdKPPmkg5zItxQ8lYdl+MDzDta4aOUflZ68l8UkbqNMxuFuKovN+0i5IGX7owVojnYpMTAceTZHHrNuXqRY2z6u2LVws3gbNdPXnAjJNX0A", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, #"Free Text" = _t]),
    #"Replaced Value" = Table.ReplaceValue(Source,"."," ",Replacer.ReplaceText,{"Free Text"}),
    #"Added Custom1" = Table.AddColumn(#"Replaced Value", "Custom.1", each List.Distinct(List.FindText(Text.Split([Free Text]," "),"PN"))),
    #"Expanded Custom.1" = Table.ExpandListColumn(#"Added Custom1", "Custom.1")
in
    #"Expanded Custom.1"

View solution in original post

5 REPLIES 5
Syndicate_Admin
Administrator
Administrator

Aquí hay una forma cruda de hacer esto. Se supone que la cadena "PN" no aparece en ningún otro lugar del texto. Así que "El conductor estaba hipnotizado" no funcionaría.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("PY47DoMwEESvMqK2kACTM6QiFHSIwoK1QFjeyB+4fmyS0O7OvDfjWLzcQq6qm0IUfVdVDZRd0Hd1LXGSI0RPCwJDR6M3g7ASOFdKPPmkg5zItxQ8lYdl+MDzDta4aOUflZ68l8UkbqNMxuFuKovN+0i5IGX7owVojnYpMTAceTZHHrNuXqRY2z6u2LVws3gbNdPXnAjJNX0A", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, #"Free Text" = _t]),
    #"Replaced Value" = Table.ReplaceValue(Source,"."," ",Replacer.ReplaceText,{"Free Text"}),
    #"Added Custom1" = Table.AddColumn(#"Replaced Value", "Custom.1", each List.Distinct(List.FindText(Text.Split([Free Text]," "),"PN"))),
    #"Expanded Custom.1" = Table.ExpandListColumn(#"Added Custom1", "Custom.1")
in
    #"Expanded Custom.1"

Esto funciona, muchas gracias.

Sin embargo, había algunas entradas que necesitaban limpieza, por ejemplo, (PN789) donde se debían quitar los soportes. Agregué dos líneas más de código para limpiarlas.

#"Added Custom.2" = Table.AddColumn(#"Expanded Custom.1", "Custom.2", each Text.PositionOf([Custom.1],"P")),
#"Added Custom.3" = Table.AddColumn(#"Added Custom.2", "Custom.3", each Text.Middle([Custom.1], [Custom.2], 6))

Te recomendaría que agregues dos sustitutos más en su lugar.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("PY47DoMwEESvMnKVSJYlwOQMqQgFHaJAsAiE5Y38gevHkIR2d+a9aVvxciO5LC+EFHWVZQV6O6Ku8lxjJ0eInkYExhTNtBiEmcBHReHJO23k5HFLwb33sAwfeFjBE06a+qPSk1clOnkZdTI2V7O3WLyPpHCrK63L+48XMHG0o0LDcOTZbMecefEykcvyccbOjYvF2/QDfd0JkWzdBw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, #"Free Text" = _t]),
    #"Replaced Value" = Table.ReplaceValue(Source,"."," ",Replacer.ReplaceText,{"Free Text"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","("," ",Replacer.ReplaceText,{"Free Text"}),
    #"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1",")"," ",Replacer.ReplaceText,{"Free Text"}),
    #"Added Custom1" = Table.AddColumn(#"Replaced Value2", "Custom.1", each List.Distinct(List.FindText(Text.Split([Free Text]," "),"PN"))),
    #"Expanded Custom.1" = Table.ExpandListColumn(#"Added Custom1", "Custom.1")
in
    #"Expanded Custom.1"

En realidad (PN789) fue solo un ejemplo. Los caracteres adicionales podrían venir en otras formas como -PN789. De todos modos, usé "PN" como maqueta, mi caso de uso real es un código de 3 letras que no se usa comúnmente en palabras en inglés.

Bastante justo. Parece que recuerdo que hay un par de funciones de reemplazo difuso más elegantes disponibles, tal vez algo a considerar. O simplemente dejar en blanco todo lo que no es una letra o un número.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors