PowerBi - DAX - 如果字符串包含,则在新列中分配一个值
PowerBi - DAX - assign a value in a new column if string contains
我想做的是从字符串中获取计算列中的值。
第 1 列有苹果;橙子;梨;香蕉
现在,如果第 1 列包含所有这些值,则计算列中的值应为 8
如果第 1 列仅包含这些值中的 2 个,则该值应为 4
如果第 1 列仅包含 3 个值,则它应为 6,如果第 1 列仅包含 1 个值,则该列的值应为 2。
简单吧?这就是我的想法,但是因为我无法计算 out.I 尝试使用 if(or(containsstring 但它会为所有这些分配值 8。我也尝试使用 strict if =“string” 但是如果会和apple 吵起来不给赋值
希望我说得足够清楚了。
column1
calculated column
apple;orange;pear;banana
8
points / specific value - 2
apple
2
apple;orange;pear;banana - correct values
apple;orange
4
strawberry - wrong value - 0
orange;banana
4
pear;banana;apple
6
strawberry
0
strawberry
0
这让您可以为另一个查询中的每个项目分配点数(命名点数),并计算此查询的 Column1 中每一行的总点数
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Added Index", {{"Column1", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Column1"),
#"Merged Queries" = Table.NestedJoin(#"Split Column by Delimiter",{"Column1"},points,{"Column1"},"zzz",JoinKind.LeftOuter),
#"Expanded Table6" = Table.ExpandTableColumn(#"Merged Queries", "zzz", {"Column2"}, {"Column2"}),
#"Grouped Rows" = Table.Group(#"Expanded Table6", {"Index"}, {
{"Column1", each Text.Combine(List.Transform([Column1], Text.From), ";"), type text},
{"Points", each List.Sum([Column2]), type number}
}),
#"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Index"})
in #"Removed Columns"
您可以对计算列使用以下内容
Column =
VAR _1 =
ADDCOLUMNS ( 'Table', "new", SUBSTITUTE ( 'Table'[col1], ";", "|" ) )
VAR _2 =
GENERATE (
_1,
ADDCOLUMNS (
GENERATESERIES ( 1, PATHLENGTH ( [new] ) ),
"_txt", TRIM ( PATHITEM ( [new], [Value], TEXT ) )
)
)
VAR _3 =
ADDCOLUMNS (
_2,
"score1", MAXX ( FILTER ( Lookup, EARLIER ( [_txt] ) = Lookup[Column1] ), Lookup[score] )
)
RETURN
CALCULATE (
MAXX (
ADDCOLUMNS (
'Table',
"score", SUMX ( FILTER ( _3, EARLIER ( 'Table'[col1] ) = [col1] ), [score1] )
),
[score]
)
)
这会给你这个
如果你有这样的查找table
我想做的是从字符串中获取计算列中的值。 第 1 列有苹果;橙子;梨;香蕉 现在,如果第 1 列包含所有这些值,则计算列中的值应为 8 如果第 1 列仅包含这些值中的 2 个,则该值应为 4 如果第 1 列仅包含 3 个值,则它应为 6,如果第 1 列仅包含 1 个值,则该列的值应为 2。
简单吧?这就是我的想法,但是因为我无法计算 out.I 尝试使用 if(or(containsstring 但它会为所有这些分配值 8。我也尝试使用 strict if =“string” 但是如果会和apple 吵起来不给赋值
希望我说得足够清楚了。
column1 | calculated column | |
---|---|---|
apple;orange;pear;banana | 8 | points / specific value - 2 |
apple | 2 | apple;orange;pear;banana - correct values |
apple;orange | 4 | strawberry - wrong value - 0 |
orange;banana | 4 | |
pear;banana;apple | 6 | |
strawberry | 0 | |
strawberry | 0 | |
这让您可以为另一个查询中的每个项目分配点数(命名点数),并计算此查询的 Column1 中每一行的总点数
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Added Index", {{"Column1", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Column1"),
#"Merged Queries" = Table.NestedJoin(#"Split Column by Delimiter",{"Column1"},points,{"Column1"},"zzz",JoinKind.LeftOuter),
#"Expanded Table6" = Table.ExpandTableColumn(#"Merged Queries", "zzz", {"Column2"}, {"Column2"}),
#"Grouped Rows" = Table.Group(#"Expanded Table6", {"Index"}, {
{"Column1", each Text.Combine(List.Transform([Column1], Text.From), ";"), type text},
{"Points", each List.Sum([Column2]), type number}
}),
#"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"Index"})
in #"Removed Columns"
您可以对计算列使用以下内容
Column =
VAR _1 =
ADDCOLUMNS ( 'Table', "new", SUBSTITUTE ( 'Table'[col1], ";", "|" ) )
VAR _2 =
GENERATE (
_1,
ADDCOLUMNS (
GENERATESERIES ( 1, PATHLENGTH ( [new] ) ),
"_txt", TRIM ( PATHITEM ( [new], [Value], TEXT ) )
)
)
VAR _3 =
ADDCOLUMNS (
_2,
"score1", MAXX ( FILTER ( Lookup, EARLIER ( [_txt] ) = Lookup[Column1] ), Lookup[score] )
)
RETURN
CALCULATE (
MAXX (
ADDCOLUMNS (
'Table',
"score", SUMX ( FILTER ( _3, EARLIER ( 'Table'[col1] ) = [col1] ), [score1] )
),
[score]
)
)
这会给你这个
如果你有这样的查找table