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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Soog
Frequent Visitor

Index related to categories (not serial numbers)

I have data containing a lot of text with categories such as the given data.

Soog_0-1644237166067.png

 

Then, for traceability, I want to give them addresses in early steps of power query like the desired data, not serial number like index.

Soog_1-1644237184173.png

 

Is there any way to go?  Thanks in advance!

 

Test File 

2 ACCEPTED SOLUTIONS

I misunderstood.

You can accomplish what you want with judicious grouping and adding index columns at the desired levels of each grouping.

 

To group:

  •  Duplicate your Category 1 and 2 columns
  • Ensure the "blanks" are nulls
  • Fill down so every row is appropriately filled
  • Group by Category 1 - copy
    • Within the group, group each subtable by Category2 - copy
    • Add an index column to the sub group
    • add an index column to the sub-sub group table
    • add an index column to the main group 
      • Transform address 1 from number to letter
        • If you have more than 26 category1's, you'll need a different algorithm than what I used
      • Expand the necessary columns in the various tables and subtables
      • Transform address 3 to put the number inside parentheses

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XZHdasMwDIVfxfg6L9GftWMwtjWDXoReKI2wDbYVlDjDb18lXRazCwufjyNZkptG76IL4HWlj2Qknsl3GNUFR3Y4Ietb1Wjhi6FFDhAL9A7eRXLDig4wSnxJTD1CVLUlHi24skrtIOCAK3klFlHpb7FSMrZl7Ar3VwIekZVdbBv/pJhXWVvEfm4GWZopTBcKEXMBdtGkodBXMDmVE+JE5XhvKK8+C+wdd4Og3hkUj4xKPoXWQbkNShxdNOpI01+zJ/D3JeFEjMOofvWWdQCGu5xyumxC/uf8+Jk/6cyY1XzdzHtg2dkT3h4=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Category1 = _t, Category2 = _t, Category3 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Category1", type text}, {"Category2", type text}, {"Category3", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
    #"Duplicated Column" = Table.DuplicateColumn(#"Added Index", "Category1", "Category1 - Copy"),
    #"Replaced Value" = Table.ReplaceValue(#"Duplicated Column","",null,Replacer.ReplaceValue,{"Category1 - Copy"}),
    #"Filled Down" = Table.FillDown(#"Replaced Value",{"Category1 - Copy"}),
    #"Duplicated Column1" = Table.DuplicateColumn(#"Filled Down", "Category2", "Category2 - Copy"),
    #"Replaced Value1" = Table.ReplaceValue(#"Duplicated Column1","",null,Replacer.ReplaceValue,{"Category2 - Copy"}),
    #"Filled Down1" = Table.FillDown(#"Replaced Value1",{"Category2 - Copy"}),

    #"Grouped Rows" = Table.Group(#"Filled Down1", {"Category1 - Copy"}, {
        {"all2", each
            Table.AddIndexColumn( 
                Table.Group(_,{"Category2 - Copy"},{
                    {"all",each Table.AddIndexColumn(_,"Address3",1,1)}
                })
                ,"Address2",1,1)},
        {"all", each _, type table [Category1=nullable text, Category2=nullable text, Category3=nullable text, Index=number, #"Category1 - Copy"=nullable text, #"Category2 - Copy"=nullable text]}}),

    #"Added Index1" = Table.AddIndexColumn(#"Grouped Rows", "Address1", 1, 1, Int64.Type),
    addr1ToAlpha = Table.TransformColumns(#"Added Index1",{"Address1", each Character.FromNumber(_+64)}),
    #"Removed Columns" = Table.RemoveColumns(addr1ToAlpha,{"all", "Category1 - Copy"}),
    #"Expanded all2" = Table.ExpandTableColumn(#"Removed Columns", "all2", {"all", "Address2"}, {"all", "Address2"}),

    #"Expanded all" = Table.ExpandTableColumn(#"Expanded all2", "all", 
        {"Category1", "Category2", "Category3", "Index", "Address3"}, {"Category1", "Category2", "Category3", "Index", "Address3"}),

    addr3WithParentheses = Table.TransformColumns(#"Expanded all",{"Address3",each "(" & Text.From(_) & ")"}),

    #"Reordered Columns" = Table.ReorderColumns(addr3WithParentheses,
        {"Category1", "Category2", "Category3", "Index", "Address1", "Address2", "Address3"})
in
    #"Reordered Columns"

ronrsnfld_0-1644285717964.png

 

ronrsnfld_1-1644285762968.png

 

 

 

 

View solution in original post

wdx223_Daniel
Super User
Super User

wdx223_Daniel_0-1644285691259.png

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Custom1 = #table(
                     Table.ColumnNames(Source)&{"Address1","Address2","Address3"},
                     List.Accumulate(
                                     Table.ToRows(Source),
                                     {{},"",0,0},
                                     (x,y)=>if y{0}<>null then {x{0}&{y&{Text.Start(y{0},1),1,"(1)"}},Text.Start(y{0},1),1,1}
                                            else if y{1}<>null then {x{0}&{y&{x{1},x{2}+1,"(1)"}},x{1},x{2}+1,1}
                                                 else {x{0}&{y&{x{1},x{2},Number.ToText(x{3}+1,"(0)")}},x{1},x{2},x{3}+1}
                                    ){0}
                    )
in
    Custom1

View solution in original post

6 REPLIES 6
wdx223_Daniel
Super User
Super User

wdx223_Daniel_0-1644285691259.png

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Custom1 = #table(
                     Table.ColumnNames(Source)&{"Address1","Address2","Address3"},
                     List.Accumulate(
                                     Table.ToRows(Source),
                                     {{},"",0,0},
                                     (x,y)=>if y{0}<>null then {x{0}&{y&{Text.Start(y{0},1),1,"(1)"}},Text.Start(y{0},1),1,1}
                                            else if y{1}<>null then {x{0}&{y&{x{1},x{2}+1,"(1)"}},x{1},x{2}+1,1}
                                                 else {x{0}&{y&{x{1},x{2},Number.ToText(x{3}+1,"(0)")}},x{1},x{2},x{3}+1}
                                    ){0}
                    )
in
    Custom1

It really smart code! Thank you!

ronrsnfld
Super User
Super User

add an Index Column.  You can do this right from the UI and specify to start at "1" instead of the default "0"

I know the index column.
What I want to do is adding the 3 address columns (right side of the desired data).

I misunderstood.

You can accomplish what you want with judicious grouping and adding index columns at the desired levels of each grouping.

 

To group:

  •  Duplicate your Category 1 and 2 columns
  • Ensure the "blanks" are nulls
  • Fill down so every row is appropriately filled
  • Group by Category 1 - copy
    • Within the group, group each subtable by Category2 - copy
    • Add an index column to the sub group
    • add an index column to the sub-sub group table
    • add an index column to the main group 
      • Transform address 1 from number to letter
        • If you have more than 26 category1's, you'll need a different algorithm than what I used
      • Expand the necessary columns in the various tables and subtables
      • Transform address 3 to put the number inside parentheses

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XZHdasMwDIVfxfg6L9GftWMwtjWDXoReKI2wDbYVlDjDb18lXRazCwufjyNZkptG76IL4HWlj2Qknsl3GNUFR3Y4Ietb1Wjhi6FFDhAL9A7eRXLDig4wSnxJTD1CVLUlHi24skrtIOCAK3klFlHpb7FSMrZl7Ar3VwIekZVdbBv/pJhXWVvEfm4GWZopTBcKEXMBdtGkodBXMDmVE+JE5XhvKK8+C+wdd4Og3hkUj4xKPoXWQbkNShxdNOpI01+zJ/D3JeFEjMOofvWWdQCGu5xyumxC/uf8+Jk/6cyY1XzdzHtg2dkT3h4=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Category1 = _t, Category2 = _t, Category3 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Category1", type text}, {"Category2", type text}, {"Category3", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
    #"Duplicated Column" = Table.DuplicateColumn(#"Added Index", "Category1", "Category1 - Copy"),
    #"Replaced Value" = Table.ReplaceValue(#"Duplicated Column","",null,Replacer.ReplaceValue,{"Category1 - Copy"}),
    #"Filled Down" = Table.FillDown(#"Replaced Value",{"Category1 - Copy"}),
    #"Duplicated Column1" = Table.DuplicateColumn(#"Filled Down", "Category2", "Category2 - Copy"),
    #"Replaced Value1" = Table.ReplaceValue(#"Duplicated Column1","",null,Replacer.ReplaceValue,{"Category2 - Copy"}),
    #"Filled Down1" = Table.FillDown(#"Replaced Value1",{"Category2 - Copy"}),

    #"Grouped Rows" = Table.Group(#"Filled Down1", {"Category1 - Copy"}, {
        {"all2", each
            Table.AddIndexColumn( 
                Table.Group(_,{"Category2 - Copy"},{
                    {"all",each Table.AddIndexColumn(_,"Address3",1,1)}
                })
                ,"Address2",1,1)},
        {"all", each _, type table [Category1=nullable text, Category2=nullable text, Category3=nullable text, Index=number, #"Category1 - Copy"=nullable text, #"Category2 - Copy"=nullable text]}}),

    #"Added Index1" = Table.AddIndexColumn(#"Grouped Rows", "Address1", 1, 1, Int64.Type),
    addr1ToAlpha = Table.TransformColumns(#"Added Index1",{"Address1", each Character.FromNumber(_+64)}),
    #"Removed Columns" = Table.RemoveColumns(addr1ToAlpha,{"all", "Category1 - Copy"}),
    #"Expanded all2" = Table.ExpandTableColumn(#"Removed Columns", "all2", {"all", "Address2"}, {"all", "Address2"}),

    #"Expanded all" = Table.ExpandTableColumn(#"Expanded all2", "all", 
        {"Category1", "Category2", "Category3", "Index", "Address3"}, {"Category1", "Category2", "Category3", "Index", "Address3"}),

    addr3WithParentheses = Table.TransformColumns(#"Expanded all",{"Address3",each "(" & Text.From(_) & ")"}),

    #"Reordered Columns" = Table.ReorderColumns(addr3WithParentheses,
        {"Category1", "Category2", "Category3", "Index", "Address1", "Address2", "Address3"})
in
    #"Reordered Columns"

ronrsnfld_0-1644285717964.png

 

ronrsnfld_1-1644285762968.png

 

 

 

 

Thank you. It works perfectly.

Now I have learned a lot from your code!

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

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.

Top Kudoed Authors