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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
MARRASARG1974
Frequent Visitor

Agregar automaticamente columnas como valores de registro haya en los valores de un campo

Hola, tengo la siguiente tabla:

NIVELDESCRIPCIONCANTIDAD
1A10
2B15
3C20
1D12
2E14
3F15

Y quisiera que mediante un loop o alguna forma automatica me agregue columnas como valores de niveles haya quedando de la siguiente manera:

NIVELDESCRIPCIONNIVEL1NIVEL2NIVEL3CANTIDAD
1AA  10
2B B 15
3C  C20
1DD  12
2E E 14
3F  F15
1 ACCEPTED SOLUTION
v-jingzhan-msft
Community Support
Community Support

Hi @MARRASARG1974 

 

Here is my solution. Steps:

1. Duplicate NIVEL column and DESCRIPCION column.

2. Add prefix string "NIVEL" to [NIVEL - Copy] column. 

3. Pivot [NIVEL - Copy] column and select [DESCRIPCION - Copy] column for values, using "Don't Aggregate".

vjingzhanmsft_1-1716432235992.png

4. Sort table by DESCRIPCION column ascendingly. 

5. Reorder columns. 

 

Full code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEYkMDpVidaCUjINMJxDUFc42BTGcgNoLIghS7gGSN4IpdQVwTuGI3qN5YAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [NIVEL = _t, DESCRIPCION = _t, CANTIDAD = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"NIVEL", Int64.Type}, {"DESCRIPCION", type text}, {"CANTIDAD", Int64.Type}}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "NIVEL", "NIVEL - Copy"),
    #"Duplicated Column1" = Table.DuplicateColumn(#"Duplicated Column", "DESCRIPCION", "DESCRIPCION - Copy"),
    #"Added Prefix" = Table.TransformColumns(#"Duplicated Column1", {{"NIVEL - Copy", each "NIVEL" & Text.From(_, "en-US"), type text}}),
    #"Pivoted Column" = Table.Pivot(#"Added Prefix", List.Distinct(#"Added Prefix"[#"NIVEL - Copy"]), "NIVEL - Copy", "DESCRIPCION - Copy"),
    #"Sorted Rows" = Table.Sort(#"Pivoted Column",{{"DESCRIPCION", Order.Ascending}}),
    #"Reordered Columns" = Table.ReorderColumns(#"Sorted Rows",{"NIVEL", "DESCRIPCION", "NIVEL1", "NIVEL2", "NIVEL3", "CANTIDAD"})
in
    #"Reordered Columns"

vjingzhanmsft_0-1716432026952.png

 

Best Regards,
Jing
If this post helps, please Accept it as Solution to help other members find it. Appreciate your Kudos!

View solution in original post

2 REPLIES 2
MARRASARG1974
Frequent Visitor

Muchas gracias por tu respuesta! Funciono perfecto!

v-jingzhan-msft
Community Support
Community Support

Hi @MARRASARG1974 

 

Here is my solution. Steps:

1. Duplicate NIVEL column and DESCRIPCION column.

2. Add prefix string "NIVEL" to [NIVEL - Copy] column. 

3. Pivot [NIVEL - Copy] column and select [DESCRIPCION - Copy] column for values, using "Don't Aggregate".

vjingzhanmsft_1-1716432235992.png

4. Sort table by DESCRIPCION column ascendingly. 

5. Reorder columns. 

 

Full code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEYkMDpVidaCUjINMJxDUFc42BTGcgNoLIghS7gGSN4IpdQVwTuGI3qN5YAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [NIVEL = _t, DESCRIPCION = _t, CANTIDAD = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"NIVEL", Int64.Type}, {"DESCRIPCION", type text}, {"CANTIDAD", Int64.Type}}),
    #"Duplicated Column" = Table.DuplicateColumn(#"Changed Type", "NIVEL", "NIVEL - Copy"),
    #"Duplicated Column1" = Table.DuplicateColumn(#"Duplicated Column", "DESCRIPCION", "DESCRIPCION - Copy"),
    #"Added Prefix" = Table.TransformColumns(#"Duplicated Column1", {{"NIVEL - Copy", each "NIVEL" & Text.From(_, "en-US"), type text}}),
    #"Pivoted Column" = Table.Pivot(#"Added Prefix", List.Distinct(#"Added Prefix"[#"NIVEL - Copy"]), "NIVEL - Copy", "DESCRIPCION - Copy"),
    #"Sorted Rows" = Table.Sort(#"Pivoted Column",{{"DESCRIPCION", Order.Ascending}}),
    #"Reordered Columns" = Table.ReorderColumns(#"Sorted Rows",{"NIVEL", "DESCRIPCION", "NIVEL1", "NIVEL2", "NIVEL3", "CANTIDAD"})
in
    #"Reordered Columns"

vjingzhanmsft_0-1716432026952.png

 

Best Regards,
Jing
If this post helps, please Accept it as Solution to help other members find it. Appreciate your Kudos!

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

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

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

Top Solution Authors
Top Kudoed Authors