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
arcall
Frequent Visitor

Extract data between same name row in Power Query

Hello to all,

 

I am trying to builds a category list for a future relationship in my model from the input of the first file and would like to have it as displayed in the second picture. I would like to achieve this in power query. Can someone help ?


glcategories.jpgoutput.jpg

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Thanks

1 ACCEPTED SOLUTION
MFelix
Super User
Super User

Hi @arcall,

 

Follow the steps below:

  • Add a column to get the lines with only text in them (assuming based on your sample that all other lines starts with numbers)

 

if Value.Is(Value.FromText(Text.Start([Account],1)), type number) = true then null else [Account]
  • Select the previous column and do a fill down
  • Create  validation column with similar formula than prior
if Value.Is(Value.FromText(Text.Start([Account],1)), type number) = true then [Category] else null
  • This column as the null change with the category now filter out all the null and delete this column
  • Make a split column by delimiter Space on the left most.

 

Check the complete M code below.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc4xDsIwDAXQq1iZGRxIVTgAA3PHqEOEbSQKjdRESo+PSwnqUCnT+7bzvTddlFzCxHAb7/HNpj944/CIiHCdM09jeEEXH7kGzTZIugxpjdqzbYBSGJ40pFDtpCYkRbiKQyAqLPQb2ivw/2BjLV6WUkx10aFdQA9JUlzNfpsr6ds/1X8A", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Account = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Account", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Category", each if Value.Is(Value.FromText(Text.Start([Account],1)), type number) = true then null else [Account]),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"Category"}),
    #"Added Custom1" = Table.AddColumn(#"Filled Down", "Valid", each if Value.Is(Value.FromText(Text.Start([Account],1)), type number) = true then [Category] else null),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom1", each ([Valid] <> null)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Valid"}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Removed Columns", "Account", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, false), {"Account.1", "Account.2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Account.1", Int64.Type}, {"Account.2", type text}})
in
    #"Changed Type1"

Regards,

MFelix


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



View solution in original post

1 REPLY 1
MFelix
Super User
Super User

Hi @arcall,

 

Follow the steps below:

  • Add a column to get the lines with only text in them (assuming based on your sample that all other lines starts with numbers)

 

if Value.Is(Value.FromText(Text.Start([Account],1)), type number) = true then null else [Account]
  • Select the previous column and do a fill down
  • Create  validation column with similar formula than prior
if Value.Is(Value.FromText(Text.Start([Account],1)), type number) = true then [Category] else null
  • This column as the null change with the category now filter out all the null and delete this column
  • Make a split column by delimiter Space on the left most.

 

Check the complete M code below.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bc4xDsIwDAXQq1iZGRxIVTgAA3PHqEOEbSQKjdRESo+PSwnqUCnT+7bzvTddlFzCxHAb7/HNpj944/CIiHCdM09jeEEXH7kGzTZIugxpjdqzbYBSGJ40pFDtpCYkRbiKQyAqLPQb2ivw/2BjLV6WUkx10aFdQA9JUlzNfpsr6ds/1X8A", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Account = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Account", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Category", each if Value.Is(Value.FromText(Text.Start([Account],1)), type number) = true then null else [Account]),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"Category"}),
    #"Added Custom1" = Table.AddColumn(#"Filled Down", "Valid", each if Value.Is(Value.FromText(Text.Start([Account],1)), type number) = true then [Category] else null),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom1", each ([Valid] <> null)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Valid"}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Removed Columns", "Account", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, false), {"Account.1", "Account.2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Account.1", Int64.Type}, {"Account.2", type text}})
in
    #"Changed Type1"

Regards,

MFelix


Regards

Miguel Félix


Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Check out my blog: Power BI em Português



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.