繁体   English   中英

Excel 电源查询和 Pivot 表

[英]Excel Power Query and Pivot Tables

在 Excel Power Query Editor 中,我有一个要转换其值的字段。 代码看起来像这样

if [Field] = "a" then "abcd" else if [Field] = "b" then "efg" 
else if [Field]="  " then "  "

问题是 [Field] 可能只有 2 个空格,例如“”,我想保留这两个空格,但是当我在 pivot 表中看到该字段时,空格消失了,字段值基本上是空白的。 Pivot 表似乎修剪了该字段的值。 如何防止修剪以保留两个空格值?

如果字符不一定需要是space字符,那么您可以使用其他看起来像space的 unicode 字符: https://www.compart.com/en/unicode/category/Zs

例如: https://www.compart.com/en/unicode/U+2000

您只需复制字符并将其粘贴到 PQ。

ANSI 字符代码 160可以为您做到这一点。 又名“邪恶的字符”,有时会通过坚持出现在不应该出现的位置来欺骗我们(代替空白)。 然后创建一个名为 CharCode160 的变量,并将字符 Chr(160) 分配给它。 这是指导您完成解决方案的 M 代码。

请注意,源表名为“Table1”,所涉及的字段的列标题为“TheField”。

添加一个例程或修改后的代码来计算字段中有多少个空格以进行正确替换将很有用。

let
CharCode160 = Character.FromNumber(160),
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"TheField", type text}}),
    #"Conditional Column Added" = Table.AddColumn(#"Changed Type", "Custom", each if [TheField] = "a" then "abcd" else if [TheField] = "b" then "efg" else if [TheField] = "  " then CharCode160 & CharCode160 else null)
in
    #"Conditional Column Added"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM