Loop on M query

851 views Asked by At

My table looks like this:

my table looks like this

I want it to look like this:

I wanted it to look like:

I did it with :

let    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfL0s/b0sQ4NBjIDnK1DEpNyUkusAzLy81KBIkZAbKwUqxMNZoUGW3v6AWk0RaZghbGxAA==",    BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text)    meta [Serialized.Text = true]) in type table [Column1 = _t, market =    _t, device = _t, userCount = _t, Num = _t]),    #"Changed Type" = Table.TransformColumnTypes(Source,{{"market", type text}, {"device", type text}, {"userCount", Int64.Type}}),    #"Transposed Table" = Table.Transpose(#"Changed Type"),    #"Split Column by Delimiter" = Table.SplitColumn(Table.TransformColumnTypes(#"Transposed Table",    {{"Column1", type text}},    "en-US"),"Column1",Splitter.SplitTextByDelimiter(";",    QuoteStyle.Csv),{"Column1.1", "Column1.2", "Column1.3"}),    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Column1.1", type text}, {"Column1.2", type text},    {"Column1.3", type text}}),    #"Split Column by Delimiter1" = Table.SplitColumn(Table.TransformColumnTypes(#"Changed Type1",    {{"Column2", type text}},    "en-US"),"Column2",Splitter.SplitTextByDelimiter(";",    QuoteStyle.Csv),{"Column2.1", "Column2.2"}),    #"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"Column2.1", type text}, {"Column2.2", type text}}),    #"Transposed Table1" = Table.Transpose(#"Changed Type2"),    #"Filled Down" = Table.FillDown(#"Transposed Table1",{"Column3", "Column4", "Column5", "Column1"}),    #"Changed Type3" = Table.TransformColumnTypes(#"Filled Down",{{"Column4", Int64.Type}, {"Column5", Int64.Type}}),    #"Renamed Columns" = Table.RenameColumns(#"Changed Type3",{{"Column1", "ID"}, {"Column2", "Market"}, {"Column3",    "Device"}, {"Column4", "UserCount"}, {"Column5", "Num"}}) in    #"Renamed Columns"

I wanted to split the multiple columns, by saving the order of each value with the semicolon:

USERCOUNT of IN and PC is 2
USERCOUNT of Us and Tablet is 5
USERCOUNT US and PC is blank

This code is easy when you have 2 rows transposed to 2 columns but what if I don't know how many rows I have?

I want to create a loop to do the function on each column (after first transposed).

1

There are 1 answers

0
ImkeF On

you can use this code which provides a dynamic solution:

let    
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUfL0s/b0sQ4NBjIDnK1DEpNyUkusAzLy81KBIkZAbKwUqxMNZoUGW3v6AWk0RaZghbGxAA==",    BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text)    meta [Serialized.Text = true]) in type table [Column1 = _t, market =    _t, device = _t, userCount = _t, Num = _t]),    #"Changed Type" = Table.TransformColumnTypes(Source,{{"market", type text}, {"device", type text}, {"userCount", Int64.Type}}),
    ToLists = Table.TransformColumns(#"Changed Type",{{"market", each Text.Split(_, ";")}, {"device", each Text.Split(_, ";")}}),
    #"Added Custom" = Table.AddColumn(ToLists, "Custom", each Table.FromColumns({[market], [device]})),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"market", "device"}),
    #"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns", "Custom", {"Column1", "Column2"}, {"Market", "Device"})
in
    #"Expanded Custom"