Forum Discussion

huynq's avatar
huynq
Frequent Visitor
6 years ago
Solved

Convert columns to rows with dynamic columns

I've tried to find the solution for this but didn't work so I post this message for your help. I have this data:     1/1 1/2 1/3 A 1 1 2 B 0 2 3 C 1 1 null (ignore null o...
  • Jimmy801's avatar
    6 years ago

    Hallo huynq 

     

    you have to first split der your data into tables, promote headers, combine your tables and then do unpivotother

    Here the complete solution

    let
    	Source = #table
    	(
    		{"Column1","Column2","Column3","Column4"},
    		{
    			{"","1/1","1/2","1/3"},	{"A","1","1","2"},	{"B","0","2","3"},	{"C","1","1",""},	{"","2/1","2/2",""},	{"A","1","1",""}
    		}
    	),
        AddIndex = Table.AddIndexColumn(Source, "Index", 0, 1),
        AddDevideTable = Table.AddColumn(AddIndex, "Devide Table", each if [Column1]="" then [Index] else null),
        FillDownDevideTable = Table.FillDown(AddDevideTable,{"Devide Table"}),
        GroupTables = Table.Group(FillDownDevideTable, {"Devide Table"}, {{"AllRows", each _, type table [Column1=text, Column2=text, Column3=text, Column4=text, Index=number, Devide Table=number]}}),
    	MaintainTable = Table.TransformColumns
    	(
    		GroupTables,
    		{
    			{
    				"AllRows",
    				(tableint) =>
    				let 
    					DeleteIndexDevideTable = Table.RemoveColumns(tableint, {"Index", "Devide Table"}),
    					Promote = Table.PromoteHeaders(DeleteIndexDevideTable)
    				in 
    					Promote
    			}
    		}
    	),
    	Combine = Table.Combine
    	(
    		MaintainTable[AllRows]
    	),
        UnpivotOther = Table.UnpivotOtherColumns(Combine, {""}, "Attribute", "Value"),
        FilterNonEmptyValue = Table.SelectRows(UnpivotOther, each ([Value] <> ""))
    in
        FilterNonEmptyValue

     

    Copy paste this code to the advanced editor in a new blank query to see how the solution works. If this solution fits your need, copy and past a part of it and implement it in your query

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy