Power BI:计算列中的多个值

时间:2020-01-14 11:40:05

标签: powerbi powerquery

我正在使用数据集,其中所有参与者的性别都在同一列中描述。我想为性别创建两个新列,并在其中填充所涉及的男性/女性人数。

一栏中使用的语法如下:

0::Male||1::Male||3::Male||4::Female

(所以我们有4位参与者,“ col”栏的值为3,而“ Female”栏的值为1)

您会这么友善并帮助我提取此信息吗? ♥

很抱歉问您,因为我知道我最终可以自己找到解决方案,但是我现在真的很受压力。 :/

This is the screenshot of the column I wanna extract values from.

非常感谢所有尝试提供帮助的人! :)

1 个答案:

答案 0 :(得分:0)

在powerquery中,您需要两个分割(一个在::上,一个在||上,然后是将数据转换为正确格式的枢轴)

选择您的数据,包括participant_gender的标题列,然后使用Data ...将表加载到powerquery中。从Table / Range ... [x]我的数据包含标题。我在下面假设这是文件的第一个表,名为Table1

添加列..索引列...

右键单击participant_gender列...按分隔符将列拆分...定制... || [x]每次出现分隔符,高级选项[x]行

右键单击participant_gender列...按分隔符分隔列...自定义... :: [x]每次出现分隔符(忽略高级选项)

单击以选择participant_gender.2和Index列..右键单击“ group by ... group by ...(group by(participant_gender.2)(index)”)新列名(count),操作(sum)列(“ participant_gender.1”)

单击以选择participant_Gender.2列... Transform..pivot列... Values列(计数)

文件...关闭并加载到...表

产生了代码,您可以将其粘贴到Home .. Advanced Editor ...

let  Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"participant_gender", type text}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Added Index", {{"participant_gender", Splitter.SplitTextByDelimiter("||", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "participant_gender"),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"participant_gender", type text}}),
#"Split Column by Delimiter1" = Table.SplitColumn(#"Changed Type1", "participant_gender", Splitter.SplitTextByDelimiter("::", QuoteStyle.Csv), {"participant_gender.1", "participant_gender.2"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter1",{{"participant_gender.1", Int64.Type}, {"participant_gender.2", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type2", {"participant_gender.2", "Index"}, {{"Count", each List.Sum([participant_gender.1]), type number}}),
#"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[participant_gender.2]), "participant_gender.2", "Count", List.Sum)
in  #"Pivoted Column"