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
xonder
Helper I
Helper I

Power Query M Function to loop though spreadsheet

I recieved a spreadsheet that has data arrange something like this:

Brand: X   
LocationUnitsPriceSales
France51050
Germany81080
Austria31030
Brand: Y   
LocationUnitsPriceSales
France22040
Germany820160
Austria42080

 

There are about 20 different brands.

 

How can I use an M Function to place the Brand in a separate colum and get a table that looks like this:

 

LocationUnitsPriceSales

Brand

France51050X
Germany81080X
Austria31030X
France22040Y
Germany820160Y
Austria42080Y

 

In excel I suppose I would use come form of looping.

 

Thanks for your help.

 

1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft Employee

Here is one way to do this 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("i45WcipKzEuxUohQ0lFSgONYnWgln/zkxJLM/DygQGheZkkxkA4oykxOBdLBiTmpxWBFbkDdYCFTIDY0ADEMwBLuqUW5iXmVQAELmIwFRMaxtLikKDMRKGAMkzGGyECdEkmhU4xAGGSsCTangGUMzdDdYgKTArkyFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if Text.Contains([Column1], "Brand") then Text.AfterDelimiter([Column1], ":") else null),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Down", each not Text.Contains([Column1], "Brand:")),
    #"Promoted Headers" = Table.PromoteHeaders(#"Filtered Rows", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Location", type text}, {"Units", type text}, {"Price", type text}, {"Sales", type text}, {" X", type text}}),
    #"Filtered Rows1" = Table.SelectRows(#"Changed Type1", each ([Price] <> "Price")),
    #"Renamed Columns" = Table.RenameColumns(#"Filtered Rows1",{{" X", "Brand"}})
in
    #"Renamed Columns"

 

 

mahoneypat_0-1604581547453.png

 

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
mussaenda
Super User
Super User

Hi @xonder ,

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcipKzEuxUohQ0lFSgONYnWgln/zkxJLM/DygQGheZkkxkA4oykxOBdLBiTmpxWBFbkDdYCFTIDY0ADEMwBLuqUW5iXmVQAELmIwFRMaxtLikKDMRKGAMkzGGyECdEkmhU4xAGGSsCTangGUMzdDdYgKTArkyFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Brand", each if Text.Contains(Text.Upper([Column1]), "BRAND") then Text.Trim(Text.AfterDelimiter([Column1], ":")) else null),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"Brand"}),
    #"Added Custom1" = Table.AddColumn(#"Filled Down", "Custom", each if Text.Contains(Text.Upper([Column1]), "LOCATION") then "Brand" else [Brand]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Brand"}),
    #"Filtered Rows" = Table.SelectRows(#"Removed Columns", each not Text.Contains([Column1], "Brand")),
    #"Promoted Headers" = Table.PromoteHeaders(#"Filtered Rows", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Location", type text}, {"Units", type text}, {"Price", type text}, {"Sales", type text}, {"Brand", type text}}),
    #"Filtered Rows1" = Table.SelectRows(#"Changed Type1", each ([Location] <> "Location"))
in
    #"Filtered Rows1"

You can try the above code.

Result is this:

 

2020_11_05_17_15_09_Remote_Desktop_Manager_Free_AEPI_PBI_.png

 

I know there are better solutions here with fewer steps.

But I hope this helps.

 

Thank you

mahoneypat
Microsoft Employee
Microsoft Employee

Here is one way to do this 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("i45WcipKzEuxUohQ0lFSgONYnWgln/zkxJLM/DygQGheZkkxkA4oykxOBdLBiTmpxWBFbkDdYCFTIDY0ADEMwBLuqUW5iXmVQAELmIwFRMaxtLikKDMRKGAMkzGGyECdEkmhU4xAGGSsCTangGUMzdDdYgKTArkyFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if Text.Contains([Column1], "Brand") then Text.AfterDelimiter([Column1], ":") else null),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Down", each not Text.Contains([Column1], "Brand:")),
    #"Promoted Headers" = Table.PromoteHeaders(#"Filtered Rows", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Location", type text}, {"Units", type text}, {"Price", type text}, {"Sales", type text}, {" X", type text}}),
    #"Filtered Rows1" = Table.SelectRows(#"Changed Type1", each ([Price] <> "Price")),
    #"Renamed Columns" = Table.RenameColumns(#"Filtered Rows1",{{" X", "Brand"}})
in
    #"Renamed Columns"

 

 

mahoneypat_0-1604581547453.png

 

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


amitchandak
Super User
Super User

@ImkeF , can you help on this

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