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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Mickeybo
Frequent Visitor

Creating Index by Row Value

I am importing txt files to Power BI.
The file structure is 

|-> Sales Details Header

Sales Details record

|-> Sales Details Footer

 

I am trying to add an index to these "Sales Detail Headers", so that I can group by individual Header

I would also like to give the "Sales Detail" an index pertaining to the relating "Sales Details Header".


Do any of you geniuses know how this can be done?

See below for Current vs Desired (in red/blue)

 

CURRENT

SALES DETAILS HEADER

17905621003763

SALES DETAIL

151188

SALES DETAIL

165201

SALES FOOTER

18473364
SALES DETAILS HEADER18353152754143
SALES DETAIL160572
SALES FOOTER18069906
SALES DETAILS HEADER17777935985498
SALES DETAIL172042
SALES DETAIL163052
SALES DETAIL154881
SALES DETAIL161259
SALES DETAIL170472
SALES FOOTER17052535

 

DESIRED

 

SALES DETAILS HEADER 0001

17905621003763

SALES DETAIL 0001 - 0001

151188

SALES DETAIL 0001 - 0002

165201

SALES FOOTER 0001

18473364
SALES DETAILS HEADER 000218353152754143
SALES DETAIL 0002 - 0001160572
SALES FOOTER 000218069906
SALES DETAILS HEADER 000317777935985498
SALES DETAIL 0003 - 0001172042
SALES DETAIL 0003 - 0002163052
SALES DETAIL 0003 - 0003154881
SALES DETAIL 0003 - 0004161259
SALES DETAIL 0003 - 0005170472
SALES FOOTER 000317052535

 



1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft Employee

Here is one way to do it in the query editor.  To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fY/LCoNADEV/RWbtIs9JshSc0oIgVHfi//9GLd0oTZvtfZzcbSvLMLWlG9s6PKalu7dhbM/SF7QArYQAbJXL3l+db4ciuqdKVQI8Kbd5Xj+tLsZc5St1Jjsro5KpoOTkCmqU90ONgPq3344L1nCVyP83AqGczKC5ouKOeQZJI+eA/NhhB0ZZy76/AA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Header", each if Text.Contains([Column1], "HEADER") then [Column2] else null),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"Header"}),
    #"Grouped Rows" = Table.Group(#"Filled Down", {"Header"}, {{"AllRows", each _, type table [Column1=nullable text, Column2=nullable text, Header=text]}}),
    AddHeaderIndex = Table.AddIndexColumn(#"Grouped Rows", "HeaderIndex", 1, 1, Int64.Type),
    AddDetailIndex = Table.TransformColumns(AddHeaderIndex, {{"AllRows", each Table.AddIndexColumn(_,"preDetailIndex",0,1), type table }}),
    #"Expanded AllRows" = Table.ExpandTableColumn(AddDetailIndex, "AllRows", {"Column1", "Column2", "preDetailIndex"}, {"Column1", "Column2", "preDetailIndex"}),
    #"Added Custom1" = Table.AddColumn(#"Expanded AllRows", "DetailIndex", each if Text.Contains([Column1], "HEADER") or Text.Contains([Column1], "FOOTER") then null else [preDetailIndex]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"preDetailIndex"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns",{{"Header", type text}, {"Column1", type text}, {"Column2", type text}, {"HeaderIndex", type text}, {"DetailIndex", type text}}),
    #"Added Custom2" = Table.AddColumn(#"Changed Type1", "NewIndex", each if [DetailIndex] <> null then Text.PadStart([HeaderIndex], 4, "0") & "-" & Text.PadStart([DetailIndex], 4, "0") else Text.PadStart([HeaderIndex], 4, "0"), type text)
in
    #"Added Custom2"

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

3 REPLIES 3
mahoneypat
Microsoft Employee
Microsoft Employee

Here is one way to do it in the query editor.  To see how it works, just create a blank query, go to Advanced Editor, and replace the text there with the M code below.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fY/LCoNADEV/RWbtIs9JshSc0oIgVHfi//9GLd0oTZvtfZzcbSvLMLWlG9s6PKalu7dhbM/SF7QArYQAbJXL3l+db4ciuqdKVQI8Kbd5Xj+tLsZc5St1Jjsro5KpoOTkCmqU90ONgPq3344L1nCVyP83AqGczKC5ouKOeQZJI+eA/NhhB0ZZy76/AA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Header", each if Text.Contains([Column1], "HEADER") then [Column2] else null),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"Header"}),
    #"Grouped Rows" = Table.Group(#"Filled Down", {"Header"}, {{"AllRows", each _, type table [Column1=nullable text, Column2=nullable text, Header=text]}}),
    AddHeaderIndex = Table.AddIndexColumn(#"Grouped Rows", "HeaderIndex", 1, 1, Int64.Type),
    AddDetailIndex = Table.TransformColumns(AddHeaderIndex, {{"AllRows", each Table.AddIndexColumn(_,"preDetailIndex",0,1), type table }}),
    #"Expanded AllRows" = Table.ExpandTableColumn(AddDetailIndex, "AllRows", {"Column1", "Column2", "preDetailIndex"}, {"Column1", "Column2", "preDetailIndex"}),
    #"Added Custom1" = Table.AddColumn(#"Expanded AllRows", "DetailIndex", each if Text.Contains([Column1], "HEADER") or Text.Contains([Column1], "FOOTER") then null else [preDetailIndex]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"preDetailIndex"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Removed Columns",{{"Header", type text}, {"Column1", type text}, {"Column2", type text}, {"HeaderIndex", type text}, {"DetailIndex", type text}}),
    #"Added Custom2" = Table.AddColumn(#"Changed Type1", "NewIndex", each if [DetailIndex] <> null then Text.PadStart([HeaderIndex], 4, "0") & "-" & Text.PadStart([DetailIndex], 4, "0") else Text.PadStart([HeaderIndex], 4, "0"), type text)
in
    #"Added Custom2"

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


That is super Pat...

Got me exactly where I need to... I'll should be able to figure the rest of it out from there.

 

Thank you so much for the swift response 🙂

amitchandak
Super User
Super User

@Mickeybo , Not very clear,

refer if subgroup index can help

https://www.youtube.com/watch?v=7CqXdSEN2k4

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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 Solution Authors
Top Kudoed Authors