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

Usable query from a XML with just two columns for both data rows and column names

Hi,

 

I need help trasforming the table below. As you can see, the rows and column data is similar with how it looks when unpivoting a table. I thought pivot is just undoing unpivot but I was wrong. All I can think of now is creating multiple queries for each unique value in name  column, adding index and then merging them -- which is very manual. 

 

fieldname
Åland IslandsCountry or Area
2015Year
TotalArea
Both SexesSex
MARIEHAMNCity
City properCity type
Estimate - de jureRecord Type
Final figure, completeReliability
2016Source Year
Åland IslandsCountry or Area
2015Year
TotalArea
MaleSex
MARIEHAMNCity
City properCity type
Estimate - de jureRecord Type
Final figure, completeReliability
2016Source Year
Åland IslandsCountry or Area
2015Year
TotalArea
FemaleSex
MARIEHAMNCity
City properCity type
Estimate - de jureRecord Type
Final figure, completeReliability
2016Source Year
Åland IslandsCountry or Area
2014Year
TotalArea
Both SexesSex
MARIEHAMNCity
City properCity type
Estimate - de jureRecord Type
Final figure, completeReliability









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.
1 ACCEPTED SOLUTION

You first need to add an Index starting at 0 and then Integer-Divide this by 9, so you get 9x 0, 9x 1 etcetera, i.e. each set of 9 fields gets the same value.

Now you can pivot with advanced option "Don't Aggregate".

All steps are created using standard menu options.

 

let
    Source = Excel.Workbook(File.Contents("C:\Users\Marcel\Documents\Forum bijdragen\Power BI Community\Usable query from a XML with just two columns for both data rows and column names.xlsx"), null, true),
    Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"field", type any}, {"name", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
    #"Integer-Divided Column" = Table.TransformColumns(#"Added Index", {{"Index", each Number.IntegerDivide(_, 9), Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Integer-Divided Column", List.Distinct(#"Integer-Divided Column"[name]), "name", "field")
in
    #"Pivoted Column"

 

Edit: now you can remove the Index column (I forgot). 

Specializing in Power Query Formula Language (M)

View solution in original post

4 REPLIES 4
Interkoubess
Solution Sage
Solution Sage

Hi @danextian,

 

I don,t know if I figured out what you are requesting.

I exported your data and created a Table ( report) as you can see on the image below:

 

Also in a excel file, you can create a pivot table with your 2 columns in Rows( repeated items).

 

Let us know if it is not what you want...

 

Columns_Divide.PNGColumns_Divide_2.PNG

hi @Interkoubess

 

The result I'm trying to achieve is something like this:

 

Country or AreaYearAreaSexCityCity typeRecord TypeReliability
Åland Islands2015TotalBoth SexesMARIEHAMNCity properEstimate - de jureFinal figure, complete
Åland Islands2015TotalMaleMARIEHAMNCity properEstimate - de jureFinal figure, complete
Åland Islands2015TotalFemaleMARIEHAMNCity properEstimate - de jureFinal figure, complete









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.

You first need to add an Index starting at 0 and then Integer-Divide this by 9, so you get 9x 0, 9x 1 etcetera, i.e. each set of 9 fields gets the same value.

Now you can pivot with advanced option "Don't Aggregate".

All steps are created using standard menu options.

 

let
    Source = Excel.Workbook(File.Contents("C:\Users\Marcel\Documents\Forum bijdragen\Power BI Community\Usable query from a XML with just two columns for both data rows and column names.xlsx"), null, true),
    Table1_Table = Source{[Item="Table1",Kind="Table"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(Table1_Table,{{"field", type any}, {"name", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
    #"Integer-Divided Column" = Table.TransformColumns(#"Added Index", {{"Index", each Number.IntegerDivide(_, 9), Int64.Type}}),
    #"Pivoted Column" = Table.Pivot(#"Integer-Divided Column", List.Distinct(#"Integer-Divided Column"[name]), "name", "field")
in
    #"Pivoted Column"

 

Edit: now you can remove the Index column (I forgot). 

Specializing in Power Query Formula Language (M)

Thanks @MarcelBeug,

 

That works. I didnt know I could do that with index and pivot. 










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.

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.