Power BI/M/Power Query——用换行符将列连接成新列

Power BI / M / Power Query -- Concatenate columns into new column with line breaks

我想将 4 列合并为一个带有换行符的列,以便在 table 可视化中保存 space。

我有一个数据 table,其中包含 4 列,其中包含字符串。

Col1 Col2 Col3 Col4
dogs cats mice fish

我想用 headers 和换行符将每条记录连接到一个列中,因此它变成:

Col1 Col2 Col3 Col4 Col5
dogs cats mice fish Col1: dogs
Col2: cats
Col3: mice
Col4: fish

然后我会把 Col5 单独放在 table 中以在可视化中保存 space。

我看到过关于使用“#(cr)”或“#(lf)”或 &Character.FromNumber(10)& 的引用,但我无法使任何工作正常进行。

当前代码:

ALLDetails = 
IF(
    ISBLANK([Col1])
    ,"Col1: N/A" 
    ,"Col1: " & [Col1]
)
& --Need linebreak here
IF(
    ISBLANK([Col2])
    ,"Col2: N/A" 
    ,"Col2: " & [Col2]
)

TIA!

编辑因为总是有编辑...

如果可能,将 N/A 用于空白条目的逻辑会有所帮助。

如果您将 4 列加载到 powerquery 中,您可以使用 M 代码公式添加列...自定义列..,如下所示。适用于任意数量的列,该部分未硬编码

保持空值:

= Text.Combine(
let contents=List.Transform(Record.ToList(_), each if _ = null then "N/A" else _),
columns=Table.ColumnNames(Source)  in 
List.Transform(
List.Positions(contents),
each columns{_} & ": " &contents{_}
),"#(lf)" )

删除空值:

= Text.Combine(
List.Transform(    
let contents=List.Transform(Record.ToList(_), each if _ = null then "N/A" else _),
columns=Table.ColumnNames(Source)  in 
List.Transform(
List.Positions(contents),
each columns{_} & ": " &contents{_})
,  each if Text.Contains(_,"N/A") then null else _)
,"#(lf)" )

注意,加载回数据时,需要开启自动换行,否则无法正常显示

你可以这样做(转换选项卡中的 M 语言):

= Table.AddColumn(#"Changed Type", "Custom", each "Col1:" & [Col1] &Character.FromNumber(10)& "Col2:" & [Col2]&Character.FromNumber(10)& "Col3:" & [Col3])

let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSslPL1bSUUpOLAFRuZnJqUAqLbM4Qyk2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Col1 = _t, Col2 = _t, Col3 = _t, Col4 = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Col1", type text}, {"Col2", type text}, {"Col3", type text}, {"Col4", type text}}), #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each "Col1:" & [Col1] &Character.FromNumber(10)& "Col2:" & [Col2]&Character.FromNumber(10)& "Col3:" & [Col3]) in #"Added Custom"

更新

使用 IF 语句:

= Table.AddColumn(#"Changed Type", "Custom", each 
let 
c1 = if [Col1] = "" then "" else "Col1:" & [Col1] &Character.FromNumber(10) ,
c2 = if [Col2] = "" then "" else "Col2:" & [Col2]&Character.FromNumber(10) ,
c3 = if [Col3] = "" then "" else "Col3:" & [Col3]&Character.FromNumber(10)
in c1 & c2 & c3 )