我的表格如下:
我希望它看起来像这样:
我是这样做的:
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"
我希望通过使用分号保存每个值的顺序来拆分多个列:
USERCOUNT of IN and PC is 2
USERCOUNT of Us and Tablet is 5
USERCOUNT US and PC is blank
当你有2行转换为2列时,这段代码很容易,但如果我不知道我有多少行呢?
我想创建一个循环来在每个列上执行该功能(在第一次转置后)。
答案 0 :(得分:1)
您可以使用此代码提供动态解决方案:
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"