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

Cómo crear un índice usando dax comparando números en dos columnas

Hola a todos,

Tengo un problema para generar la columna calculada "INDICE4" Quiero generar la "indece4" pero solo cambiar si la columna "índice" es distinta al valor anterior. Este es el ejemplo:

indice 4.png

Los datos son:

CiudadÁreaOrganizaciónArtículoDeParaOperadorÍndiceÍndiceIndice2
N.Y.NY_B2Company_T1Cantidad37Y1011
N.Y.NY_B2Company_T1ProductoPRD03PRD06O1122
N.Y.NY_B2Company_T1ProductoPRD08PRD013O1223
N.Y.NY_B2Company_T1ProductoPRD15PRD18O1324
N.Y.NY_B2Company_B5Cantidad13Y1415
N.Y.NY_B2Company_B5ProductoPRD04PRD06O1526
N.Y.NY_B2Company_B5ProductoPRD09PRD11O1627
N.Y.NY_D4Company_F5Cantidad24Y2211
N.Y.NY_D4Company_F5ProductoPRD02PRD05O2322
N.Y.NY_D4Company_F5ProductoPRD07PRD09O2423
N.Y.NY_D4Company_B2Cantidad13Y2514

Muchas gracias por ayudarme, saludos...

1 ACCEPTED SOLUTION
Syndicate_Admin
Administrator
Administrator

En general, es más fácil crear columnas de índice en el editor de consultas en lugar de en DAX, por lo que estoy proporcionando una solución en M. Es posible en DAX, pero estoy pasando a escribir cosas de RANKX por el momento. Tal vez otro usuario salte.

Para obtener este índice, agruparía en las primeras cuatro columnas para establecer la granularidad del índice, luego agruparía en Ciudad y Área, generando un índice para cada una de estas, luego expandiría y fusionaría con la tabla original.

Aquí está la consulta M completa que puede pegar en su Editor avanzado para recorrer los pasos aplicados:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZFBD0NAEIX/i7OInd1Fj1XpUVvpRURE2otDaRoO/n1nGWIJ4jD71ka+zHsvSYzQii3DNMI48wH1Un2+edlmT4YfjyYv66Ju8cpxXJxzGODJbHV0k5rbjPuvejevWt2iwOakDuotUgD1D3RzjOSRMj6igFD8GIpJUm8kcSKJLZIv9YgYxUQRCXqSuwzdmFhEJGkb5yDpRL7YSHKI5M5IgZiQrjNffRCDL4CV6pcMfRsglcM2MKQ8r36P5E789SSx0rxG6uJa7QskPWHn6R8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [City = _t, Area = _t, Organization = _t, Item = _t, From = _t, To = _t, Operator = _t, Indice = _t, Index = _t, Indice2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"City", type text}, {"Area", type text}, {"Organization", type text}, {"Item", type text}, {"From", type text}, {"To", type text}, {"Operator", type text}, {"Indice", Int64.Type}, {"Index", Int64.Type}, {"Indice2", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"City", "Area", "Organization", "Item"},{}),
    #"Grouped Rows1" = Table.Group(#"Grouped Rows", {"City", "Area"}, {{"AllRows", each  Table.AddIndexColumn(_, "Index4", 1, 1, Int64.Type), type table [City=nullable text, Area=nullable text, Organization=nullable text, Item=nullable text, Index4=Int64.Type]}}),
    #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows1", "AllRows", {"Organization", "Item", "Index4"}, {"Organization", "Item", "Index4"}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"City", "Area", "Organization", "Item"}, #"Expanded AllRows", {"City", "Area", "Organization", "Item"}, "Expanded AllRows", JoinKind.LeftOuter),
    #"Expanded Expanded AllRows" = Table.ExpandTableColumn(#"Merged Queries", "Expanded AllRows", {"Index4"}, {"Index4"})
in
    #"Expanded Expanded AllRows"

View solution in original post

4 REPLIES 4
Syndicate_Admin
Administrator
Administrator

En general, es más fácil crear columnas de índice en el editor de consultas en lugar de en DAX, por lo que estoy proporcionando una solución en M. Es posible en DAX, pero estoy pasando a escribir cosas de RANKX por el momento. Tal vez otro usuario salte.

Para obtener este índice, agruparía en las primeras cuatro columnas para establecer la granularidad del índice, luego agruparía en Ciudad y Área, generando un índice para cada una de estas, luego expandiría y fusionaría con la tabla original.

Aquí está la consulta M completa que puede pegar en su Editor avanzado para recorrer los pasos aplicados:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZFBD0NAEIX/i7OInd1Fj1XpUVvpRURE2otDaRoO/n1nGWIJ4jD71ka+zHsvSYzQii3DNMI48wH1Un2+edlmT4YfjyYv66Ju8cpxXJxzGODJbHV0k5rbjPuvejevWt2iwOakDuotUgD1D3RzjOSRMj6igFD8GIpJUm8kcSKJLZIv9YgYxUQRCXqSuwzdmFhEJGkb5yDpRL7YSHKI5M5IgZiQrjNffRCDL4CV6pcMfRsglcM2MKQ8r36P5E789SSx0rxG6uJa7QskPWHn6R8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [City = _t, Area = _t, Organization = _t, Item = _t, From = _t, To = _t, Operator = _t, Indice = _t, Index = _t, Indice2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"City", type text}, {"Area", type text}, {"Organization", type text}, {"Item", type text}, {"From", type text}, {"To", type text}, {"Operator", type text}, {"Indice", Int64.Type}, {"Index", Int64.Type}, {"Indice2", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"City", "Area", "Organization", "Item"},{}),
    #"Grouped Rows1" = Table.Group(#"Grouped Rows", {"City", "Area"}, {{"AllRows", each  Table.AddIndexColumn(_, "Index4", 1, 1, Int64.Type), type table [City=nullable text, Area=nullable text, Organization=nullable text, Item=nullable text, Index4=Int64.Type]}}),
    #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows1", "AllRows", {"Organization", "Item", "Index4"}, {"Organization", "Item", "Index4"}),
    #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"City", "Area", "Organization", "Item"}, #"Expanded AllRows", {"City", "Area", "Organization", "Item"}, "Expanded AllRows", JoinKind.LeftOuter),
    #"Expanded Expanded AllRows" = Table.ExpandTableColumn(#"Merged Queries", "Expanded AllRows", {"Index4"}, {"Index4"})
in
    #"Expanded Expanded AllRows"

@AlexisOlson Lo intento con su código M, y fue muy difícil porque soy nuevo en Power BI, DAX y M.

pero, su explicación fue muy útil.

Cambié su código M a este:

dejar
Origen = Excel.Workbook(File.Contents("C:\Users\danie\Desktop\Criterio Matriz.xlsx"), null, true),
Criteria_Table = Origen{[Item="Criteria",Kind="Table"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"City", type text}, {"Area", type text}, {"Organization", type text}, {"Item", type text}, {"From", type text}, {"To", type text}, {"Operator", type text}}),
#"Filas agrupadas" = Table.Group(#"Changed Type", {"City", "Area", "Organization", "Item"},{}),
#"Grouped Rows1" = Table.Group(#"Grouped Rows", {"City", "Area"}, {{"AllRows", each Table.AddIndexColumn(_, "Index4", 1, 1, Int64.Type), escriba table [City=nullable text, Area=nullable text, Organization=nullable text, Item=nullable text, Index4=Int64.Type]}}),
#"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows1", "AllRows", {"Organization", "Item", "Index4"}, {"Organization", "Item", "Index4"}),
#"Consultas combinadas" = Table.NestedJoin(#"Changed Type", {"City", "Area", "Organization", "Item"}, #"Expanded AllRows", {"City", "Area", "Organization", "Item"}, "Expanded AllRows", JoinKind.LeftOuter),
#"Expanded Expanded AllRows" = Table.ExpandTableColumn(#"Consultas combinadas", "Expanded AllRows", {"Index4"}, {"Index4"})
en
#"Expandido Expandido AllRows"

La 3ª línea (#"Changed Type" = ....) me da un error pero debo eliminar ese paso y funciona perfectamente.

Realmente aprecio su explicación, ejemplo, código, tiempo, ayuda, etc.

Saludos y gracias

Da un error ya que se refiere a un paso anterior "Fuente". Cuando eliminó ese paso, volvió a vincular automáticamente la referencia a #"Tipo cambiado" en el paso #"Fila agrupada" para hacer referencia a Critera_Table en su lugar, haciendo que todo funcione. 🙂

Observe cómo cada línea hace referencia a la anterior como el primer argumento en la función de tabla y tendrá una mejor comprensión de cómo corregir errores de referencia.

Gracias de nuevo por la explicación 👍🏼

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.