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
Sibrulotte
Helper III
Helper III

Rows to column: is unpivot realy the solution?

Hi, 

I have a crappy text report... 😉

and in that text report I have something like this 

 

Account        Registration    Date of activity 

624564         John Doe          2019-11-30

                     30 Slipery rd

                      Denver, Co

                     10012, US

 

623134        Jane Smith          2019-11-30

                    58 Poplar st

                    apt 202

                    Crownville, Ut

                     15420, US

 

 

And so forth...

Did you see the catch there too? John has 3 adress lines, Jane has 4.

Can you see me coming? 

How do I get these adresses all ligned up in seperate columns for street + apt number, city, state, zip and country?

 

 

 

1 ACCEPTED SOLUTION

Hi @Sibrulotte ,

 

You can try grouping the rows in [Registration] column by [Account] and [Date] using Table.Group function. The aggregation parameter is modified to combine the rows instead of counting or summing them up. Once combined, you then need create custom columns by extracting the data from the grouped rows. Here's a sample code:

let
  Source = Excel.Workbook("file.xlsx", null, true),
  Navigation = Source{[Item = "table", Kind = "Sheet"]}[Data],
  #"Promoted headers" = Table.PromoteHeaders(Navigation, [PromoteAllScalars = true]),
  #"Changed column type" = Table.TransformColumnTypes(#"Promoted headers", {{"Account", type text}, {"Registration", type text}, {"Date of activity", type date}}),
  #"Filled down" = Table.FillDown(#"Changed column type", {"Account", "Date of activity"}),
  #"Grouped rows" = Table.Group(#"Filled down", {"Account", "Date of activity"}, {{"Registration", each Text.Combine([Registration], "__"), type text}}),
  #"Inserted text after delimiter" = Table.AddColumn(#"Grouped rows", "Zip and Country", each Text.AfterDelimiter([Registration], "__", {0, RelativePosition.FromEnd}), type text),
  #"Inserted text between delimiters" = Table.AddColumn(#"Inserted text after delimiter", "City and State", each Text.BetweenDelimiters([Registration], "__", "__", {0, RelativePosition.FromEnd}, {0, RelativePosition.FromEnd}), type text),
  #"Inserted text before delimiter" = Table.AddColumn(#"Inserted text between delimiters", "Name", each Text.BeforeDelimiter([Registration], "__", 0), type text),
  #"Inserted conditional column" = Table.AddColumn(#"Inserted text before delimiter", "Street Address", each  Text.BetweenDelimiters([Registration], [Name] & "__", "__" & [City and State]), type text),
  #"Replaced value" = Table.ReplaceValue(#"Inserted conditional column", "__", ", ", Replacer.ReplaceText, {"Street Address"})
in
  #"Replaced value"

 










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


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

10 REPLIES 10
v-lili6-msft
Community Support
Community Support

hi @Sibrulotte 

Usually, convert row to column, you need use pivot or unpivot column.

https://radacad.com/pivot-and-unpivot-with-power-bi

but for your case, it seems that you could use split column in power bi

https://www.tutorialgateway.org/how-to-split-columns-in-power-bi/

If not your case, please share the sample data in table format and your expected output.

 

Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

AccountResgistrationDate of activity
624564John Doe2019-11-29
 30 Slipery rd 
 Denver, Co 
 10012, US 
623134Jane Smith2019-11-30
 58 Poplar st 
 apt 202 
 Crownville, Ut
 
 15420, US
 

 

 

 

Result would be:

AccountNameAdressCityStateZipCountry
624564John Doe30 Slipery rdDenverCo10012US
623134Jane Smith58 Poplar st, apt 202CrownvilleUt15420US

hi @Sibrulotte 

For your case, you could try this way:

Step1:

You need add a column tothat is used for classify the [Resgistration] column

for example:

2.JPG

Step2:

Then use Pivot column and split column as these steps:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dY49C4MwFEX/SsgcMB8qdSw6dSiF4FJxCBgwEJMQXy3++walnVJ4w72cy+MMA655WdUlJvjmZ4c6r1NkrOBNwSlrUrmrReORDDhlQZG0Jui4ozilnu46Rb2u30Gn3aYjaf0JWwM7kaDg94FRyjjp5cmfJqTty0Hcj0HNBROHjHIaycXAfOoImtOpLujhg1URrZCzUQEQpzyH2ujfbjPWatLDX9mq5DQvO34A", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Account = _t, Resgistration = _t, #"Date of activity" = _t, Type = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Account", Int64.Type}, {"Resgistration", type text}, {"Date of activity", type date}, {"Type", type text}}),
    #"Filled Down" = Table.FillDown(#"Changed Type",{"Account", "Date of activity"}),
    #"Grouped Rows" = Table.Group(#"Filled Down", {"Account","Date of activity","Type"}, {{"Persons", each Text.Combine([Resgistration], ","), type text}}),
    #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Type]), "Type", "Persons"),
    #"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"Date of activity"}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Removed Columns", "City,State", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"City,State.1", "City,State.2"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"City,State.1", type text}, {"City,State.2", type text}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"City,State.1", "City"}, {"City,State.2", "State"}}),
    #"Split Column by Delimiter1" = Table.SplitColumn(#"Renamed Columns", "Zip,Country", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Zip,Country.1", "Zip,Country.2"}),
    #"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"Zip,Country.1", Int64.Type}, {"Zip,Country.2", type text}}),
    #"Renamed Columns1" = Table.RenameColumns(#"Changed Type2",{{"Zip,Country.1", "Zip"}, {"Zip,Country.2", "Country"}})
in
    #"Renamed Columns1"

Result:

3.JPG

 

and here is sample pbix file, please try it.

 

Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

well it was nice to try. My problem is exactly that: these rows do not have labels on them. And to make it worst, some have extra rows for appatment number, so I can't start an index and make a mathematical equation to derive a fix number for these rows...

Hi @Sibrulotte ,

 

You can try grouping the rows in [Registration] column by [Account] and [Date] using Table.Group function. The aggregation parameter is modified to combine the rows instead of counting or summing them up. Once combined, you then need create custom columns by extracting the data from the grouped rows. Here's a sample code:

let
  Source = Excel.Workbook("file.xlsx", null, true),
  Navigation = Source{[Item = "table", Kind = "Sheet"]}[Data],
  #"Promoted headers" = Table.PromoteHeaders(Navigation, [PromoteAllScalars = true]),
  #"Changed column type" = Table.TransformColumnTypes(#"Promoted headers", {{"Account", type text}, {"Registration", type text}, {"Date of activity", type date}}),
  #"Filled down" = Table.FillDown(#"Changed column type", {"Account", "Date of activity"}),
  #"Grouped rows" = Table.Group(#"Filled down", {"Account", "Date of activity"}, {{"Registration", each Text.Combine([Registration], "__"), type text}}),
  #"Inserted text after delimiter" = Table.AddColumn(#"Grouped rows", "Zip and Country", each Text.AfterDelimiter([Registration], "__", {0, RelativePosition.FromEnd}), type text),
  #"Inserted text between delimiters" = Table.AddColumn(#"Inserted text after delimiter", "City and State", each Text.BetweenDelimiters([Registration], "__", "__", {0, RelativePosition.FromEnd}, {0, RelativePosition.FromEnd}), type text),
  #"Inserted text before delimiter" = Table.AddColumn(#"Inserted text between delimiters", "Name", each Text.BeforeDelimiter([Registration], "__", 0), type text),
  #"Inserted conditional column" = Table.AddColumn(#"Inserted text before delimiter", "Street Address", each  Text.BetweenDelimiters([Registration], [Name] & "__", "__" & [City and State]), type text),
  #"Replaced value" = Table.ReplaceValue(#"Inserted conditional column", "__", ", ", Replacer.ReplaceText, {"Street Address"})
in
  #"Replaced value"

 










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


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

That's a very functional solution, and helps with 90% of the problem.

 

But I still don't have a clean delimiter for the rows that are appartment numbers and not the city. 

Remember, some rows are created for appartement numbers, and when there are no appartment numbers, that row is not created.

 

@Sibrulotte 

I know very little about M, but it sounds like the very first step that needs to be done is to ensure that each value in the first column has “n”  number of rows beneath (to cover the total rows possible per address). So adding one or two rows to reach “n”. And carry on from there...I guess...but I haven’t a clue if this is possible.





Did I answer your question? Mark my post as a solution!
In doing so, you are also helping me. Thank you!

Proud to be a Super User!
Paul on Linkedin.






@PaulDBrown 

 

The M code I posted extracts the necessary info following this logic. 

  • Row 1 = Name
  • Last Row = Zip & Country
  • 2nd to the last row = City & State
  • Rows in between = Street, etc.









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


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Oh that's a good move. 

Trying to recreate it in french. 

Sorry you're M code couldn't be copy pasted in my french version...

hi @Sibrulotte 

You must define the labels for Registration columns, otherwise, it will be difficult to determine which rows is Name/Adress/City ,etc.

Then the following steps are simple.

So i think you'd better do it in the datasource.

 

Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

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.