简体   繁体   English

基于多个条件的Power bi返回值

[英]Power bi returning value based on multiple condition

I have the data below我有下面的数据

create table #data (Post_Code varchar(10),  Internal_Code varchar(10))
insert into #data values
('AB10','hb3'),('AB10','hb25'),('AB12','dd1'),('AB15','hb6'),('AB16','aa4'),('AB16','hb7'),
('AB16','aa2'),('AB16','ab9'),('AB18','rr6'),('AB18','rr9'),('AB18','hb10'),('AB20','rr15'),
('AB20','td2'),('AB21','hb8'),('AB21','cc4'),('AB21','cc4'),('AB24','td5'),('AB9','yy3'),
('RM2','CC1'),('RM6','hb6'),('RM7','cc2'),('SA24','rr1'),('SA24','hb5'),('SA24','rr2'),
('SA24','cc34'),('SE15','rr9'),('SE15','rr5'),('SE25','rr10'),('SE25','hb11'),('SE25','rr8'),
('SE25','rr1'),('LA15','rr2')

select * from #data
drop table #data

What I want to achieve is if the same post code area have “hb” or “rr” in the same post code I want to return 1 else 0 The “hb” or “rr” internal_code must be in the same post_code if they in different post code.我想要实现的是,如果同一个邮政编码区域在同一个邮政编码中有“hb”或“rr”我想返回 1 else 0 “hb”或“rr” internal_code 如果它们在同一个 post_code 中,则它们必须在同一个 post_code 中不同的邮政编码。 It should be 0它应该是 0

I wrote this DAX我写了这个 DAX

Result = IF(left(Data[Internal_Code],2)="hb" || left(Data[Internal_Code],2)="rr",1,0)

it is not returning the correct result它没有返回正确的结果

current output电流输出

在此处输入图像描述

expected output预期产出

在此处输入图像描述

I think your expected result is incorrect as SA24 should also be 1. You should definitely do a calculation like this in PQ but if you need to do it in DAX in a calculated column, then use the following code which works.我认为您的预期结果不正确,因为 SA24 也应该是 1。您绝对应该在 PQ 中进行这样的计算,但如果您需要在 DAX 中的计算列中进行计算,请使用以下有效的代码。

Result = 

VAR post_code = Data[Post_code]

RETURN

VAR hb = CALCULATE (COUNTROWS(Data),'Data'[Post_code] = post_code && left(Data[Internal_Code],2) = "hb" )
VAR rr = CALCULATE (COUNTROWS(Data),'Data'[Post_code] = post_code && left(Data[Internal_Code],2) = "rr" ) 

RETURN IF(hb>0 && rr > 0,1)

在此处输入图像描述

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

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