如果记录组的多个列包含特定文本,如何使用 DAX 检查?
How to check using DAX if multiple columns for group of records contain specific text?
我有一个 table,其中包含 Date、MainID、Par1、Par2 和 Par3 列,如下所示
如果对于日期和 MainID 值,Par1、Par2 和 Par3 值中的任何一个包含“UNMATCH”,那么总体上应该表示“UNMATCH”。我正在寻找“总体”列中的输出。
我尝试了 Group By、Contains 和其他选项,但无法解决此问题。
知道我可以从哪里开始吗?
你不需要使用“Groupby”或更复杂的函数,“if else statement”和“Distinct count”就可以了,唯一的区别是你需要将每个计数存储在一个变量中,然后比较值:
Overall =
var par1 = CALCULATE(DISTINCTCOUNT(Sheet1[Par1]),
FILTER(ALL(Sheet1),Sheet1[MainID]=EARLIER(Sheet1[MainID])),
FILTER(Sheet1,Sheet1[Date]=EARLIER(Sheet1[Date])),Sheet1[Par1]="UNMATCH")
var par2 = CALCULATE(DISTINCTCOUNT(Sheet1[Par2]),
FILTER(ALL(Sheet1),Sheet1[MainID]=EARLIER(Sheet1[MainID])),
FILTER(Sheet1,Sheet1[Date]=EARLIER(Sheet1[Date])),Sheet1[Par2]="UNMATCH")
var par3 = CALCULATE(DISTINCTCOUNT(Sheet1[Par3]),
FILTER(ALL(Sheet1),Sheet1[MainID]=EARLIER(Sheet1[MainID])),
FILTER(Sheet1,Sheet1[Date]=EARLIER(Sheet1[Date])),Sheet1[Par3]="UNMATCH")
return
IF(par1 + par2 + par3 >=1, "UNMATCH", "MATCH")
这是我的时髦建议:
Overall =
VAR _table =
CALCULATETABLE (
UNION (
VALUES ( 'Table'[Par1] ) ,
VALUES ( 'Table'[Par2] ) ,
VALUES ( 'Table'[Par3] )
),
ALLEXCEPT ( 'Table' , 'Table'[Date] , 'Table'[Main ID] )
)
RETURN
IF (
CONTAINS (
_table ,
'Table'[Par1] ,
"UNMATCH"
),
"UNMATCH" ,
"MATCH"
)
编辑:还有一个稍微无聊(但可能更直观)的建议:
Another Overall =
VAR _table =
CALCULATETABLE (
'Table' ,
ALLEXCEPT ( 'Table' , 'Table'[Date] , 'Table'[Main ID] )
)
RETURN
SWITCH (
TRUE ,
CONTAINS( _table , 'Table'[Par1] , "UNMATCH" ) , "UNMATCH" ,
CONTAINS( _table , 'Table'[Par2] , "UNMATCH" ) , "UNMATCH" ,
CONTAINS( _table , 'Table'[Par3] , "UNMATCH" ) , "UNMATCH" ,
"MATCH"
)
如果您愿意table在 M 查询中执行此操作,您可以按照以下说明进行操作-
转到 Power query editor,然后为您的 table 打开 Advanced Editor。现在将下面的代码添加到您现有的代码中,您的 table 中将有一个 新列 ,其中包含您预期的输出 -
let
//#"your_last_step_name",
//---- Add this below code with adjusting the last step name
#"Added Custom" = Table.AddColumn(#"your_last_step_name", "Custom", each if [Par1] = "UNMATCH" then [Par1] else if [Par2] = "UNMATCH" then [Par2] else [Par3]),
_grouped = Table.Group(#"Added Custom", {"Date", "MainID"}, {{"max", each List.Max([Custom]), type text}}),
#"Merged Queries" = Table.NestedJoin(#"Added Custom", {"Date", "MainID"}, _grouped, {"Date", "MainID"}, "_grouped", JoinKind.LeftOuter),
#"Expanded _grouped" = Table.ExpandTableColumn(#"Merged Queries", "_grouped", {"max"}, {"Overall"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded _grouped",{"Custom"})
in
#"Removed Columns"
这里是最终输出-
我有一个 table,其中包含 Date、MainID、Par1、Par2 和 Par3 列,如下所示
如果对于日期和 MainID 值,Par1、Par2 和 Par3 值中的任何一个包含“UNMATCH”,那么总体上应该表示“UNMATCH”。我正在寻找“总体”列中的输出。
我尝试了 Group By、Contains 和其他选项,但无法解决此问题。
知道我可以从哪里开始吗?
你不需要使用“Groupby”或更复杂的函数,“if else statement”和“Distinct count”就可以了,唯一的区别是你需要将每个计数存储在一个变量中,然后比较值:
Overall =
var par1 = CALCULATE(DISTINCTCOUNT(Sheet1[Par1]),
FILTER(ALL(Sheet1),Sheet1[MainID]=EARLIER(Sheet1[MainID])),
FILTER(Sheet1,Sheet1[Date]=EARLIER(Sheet1[Date])),Sheet1[Par1]="UNMATCH")
var par2 = CALCULATE(DISTINCTCOUNT(Sheet1[Par2]),
FILTER(ALL(Sheet1),Sheet1[MainID]=EARLIER(Sheet1[MainID])),
FILTER(Sheet1,Sheet1[Date]=EARLIER(Sheet1[Date])),Sheet1[Par2]="UNMATCH")
var par3 = CALCULATE(DISTINCTCOUNT(Sheet1[Par3]),
FILTER(ALL(Sheet1),Sheet1[MainID]=EARLIER(Sheet1[MainID])),
FILTER(Sheet1,Sheet1[Date]=EARLIER(Sheet1[Date])),Sheet1[Par3]="UNMATCH")
return
IF(par1 + par2 + par3 >=1, "UNMATCH", "MATCH")
这是我的时髦建议:
Overall =
VAR _table =
CALCULATETABLE (
UNION (
VALUES ( 'Table'[Par1] ) ,
VALUES ( 'Table'[Par2] ) ,
VALUES ( 'Table'[Par3] )
),
ALLEXCEPT ( 'Table' , 'Table'[Date] , 'Table'[Main ID] )
)
RETURN
IF (
CONTAINS (
_table ,
'Table'[Par1] ,
"UNMATCH"
),
"UNMATCH" ,
"MATCH"
)
编辑:还有一个稍微无聊(但可能更直观)的建议:
Another Overall =
VAR _table =
CALCULATETABLE (
'Table' ,
ALLEXCEPT ( 'Table' , 'Table'[Date] , 'Table'[Main ID] )
)
RETURN
SWITCH (
TRUE ,
CONTAINS( _table , 'Table'[Par1] , "UNMATCH" ) , "UNMATCH" ,
CONTAINS( _table , 'Table'[Par2] , "UNMATCH" ) , "UNMATCH" ,
CONTAINS( _table , 'Table'[Par3] , "UNMATCH" ) , "UNMATCH" ,
"MATCH"
)
如果您愿意table在 M 查询中执行此操作,您可以按照以下说明进行操作-
转到 Power query editor,然后为您的 table 打开 Advanced Editor。现在将下面的代码添加到您现有的代码中,您的 table 中将有一个 新列 ,其中包含您预期的输出 -
let
//#"your_last_step_name",
//---- Add this below code with adjusting the last step name
#"Added Custom" = Table.AddColumn(#"your_last_step_name", "Custom", each if [Par1] = "UNMATCH" then [Par1] else if [Par2] = "UNMATCH" then [Par2] else [Par3]),
_grouped = Table.Group(#"Added Custom", {"Date", "MainID"}, {{"max", each List.Max([Custom]), type text}}),
#"Merged Queries" = Table.NestedJoin(#"Added Custom", {"Date", "MainID"}, _grouped, {"Date", "MainID"}, "_grouped", JoinKind.LeftOuter),
#"Expanded _grouped" = Table.ExpandTableColumn(#"Merged Queries", "_grouped", {"max"}, {"Overall"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded _grouped",{"Custom"})
in
#"Removed Columns"
这里是最终输出-