简体   繁体   English

Power BI Dax - 一次尝试后试图打破循环依赖

[英]Power BI Dax - Trying to break circular dependency after one attempt

I am trying to figure out ancestor bloodline in data I have.我试图找出我拥有的数据中的祖先血统。 I feel like there is something I am missing to make this work.我觉得我缺少一些东西来完成这项工作。 This data wouldn't change so not sure if I am missing something I can write in the power query editor when loading / refreshing the data.此数据不会更改,因此不确定在加载/刷新数据时是否遗漏了可以在电源查询编辑器中编写的内容。

While technically it is circular in fields it never going to return the same row it is currently in and the first generation is hard number making a starting point.虽然从技术上讲,它在字段中是循环的,但它永远不会返回它当前所在的同一行,并且第一代是硬数字作为起点。 I only want to reference the mother and father to calculate the child's bloodline.只想参考妈妈和爸爸来推算孩子的血统。 The first generation is a basic IF() statement.第一代是基本的 IF() 语句。 Below is as far as I can get before hitting the circular dependency error.以下是我在遇到循环依赖错误之前所能得到的。 I have tried a few things to break it thinking its going to loop.我尝试了一些方法来打破它,认为它会循环。

Logic is:逻辑是:

  1. Each blood is 100% for 1st generations based on their birthplace then it is ((mother blood + father blood) / 2) for each generation after that.根据出生地,第一代的每个血统都是 100%,之后的每一代都是 ((母亲血统 + 父亲血统) / 2)。 I found I can use PATHITEM() to isolate the type of blood but errors with a circular dependency.我发现我可以使用 PATHITEM() 来隔离血液类型,但会出现循环依赖的错误。 (This is where I can't figure out how to reference the mother / father to do the calculation.) If I take this part out I get the image below working for 1st generation and correct mother / father for second generation. (这是我无法弄清楚如何参考母亲/父亲来进行计算的地方。)如果我把这部分拿出来,我会得到下面的图像适用于第一代,而正确的母亲/父亲适用于第二代。
Asisa Blood = 
VAR current_id = 'Sheet1'[ID]
VAR current_gen = 'Sheet1'[Generation]
VAR current_blood = 'Sheet1'[Birthplace]
VAR current_mother_blood = 
    PATHITEM(
    CALCULATE(
        DISTINCT('Sheet1'[Mother's Blood Mix]),
        FILTER(
            ALLNOBLANKROW('Sheet1'[ID]),
            'Sheet1'[ID] = current_id
        ),
        REMOVEFILTERS('Sheet1')
    ),1,INTEGER)
VAR current_father_blood = 
    PATHITEM(
    CALCULATE(
        DISTINCT('Sheet1'[Father's Blood Mix]),
        FILTER(
            ALLNOBLANKROW('Sheet1'[ID]),
            'Sheet1'[ID] = current_id
        ),
        REMOVEFILTERS('Sheet1')
    ),1,INTEGER)
VAR gen1_value = 100

RETURN
    IF(AND(LOWER(current_gen) = "1",LOWER(current_blood) = "asisa"),
    gen1_value,
    ((current_mother_blood + current_father_blood)/2)
    )
  1. Blood Mix concatenates the four blood types into one field for easy look up in next step. Blood Mix 将四种血型连接到一个字段中,以便在下一步中查找。
Blood mix = 
VAR current__id = 'Sheet1'[ID]
VAR current_blood_a = 'Sheet1'[Asisa Blood]
VAR current_blood_b = 'Sheet1'[Africa Blood]
VAR current_blood_c = 'Sheet1'[Europe Blood]
VAR current_blood_d = 'Sheet1'[North America Blood] 

RETURN
    current_blood_a & "|" & current_blood_b & "|" & current_blood_c & "|" & current_blood_d
  1. Mother and Father are lookups on blood mix with mother or father ids母亲和父亲正在查找带有母亲或父亲 ID 的血液混合物
Mother's Blood Mix = 
VAR current_id = 'Sheet1'[ID]
VAR current_gen = 'Sheet1'[Generation]
VAR gen_value = 'Sheet1'[Blood mix]
VAR current_parent_id = 
    IF(LOWER(current_gen) = "1",current_id,'Sheet1'[Mother ID])
VAR result = 
    CALCULATE(
        DISTINCT('Sheet1'[Blood mix]),
        FILTER(
            ALLNOBLANKROW('Sheet1'[ID]),
            'Sheet1'[ID] = current_parent_id
        ),
        REMOVEFILTERS('Sheet1')
    )
RETURN
    result

在此处输入图像描述

You can try to do this way (rather high resource consuming):您可以尝试这样做(相当高的资源消耗):

NEW COLUMN:新栏目:

heritage_Father = PATH(Blood[ID],(Blood[FatherID]))
heritage_Mother = PATH(Blood[ID],(Blood[MotherID]))
Mancestor = LOOKUPVALUE(Blood[heritage_Mother],Blood[ID],Blood[FatherID]) &"|"& Blood[heritage_Mother]
Fancestor = LOOKUPVALUE(Blood[heritage_Father],Blood[ID],Blood[MotherID])&"|"&Blood[heritage_Father]

NEW MEASURE:新措施:

ASIA_check = 
VAR Table0 =
    SELECTCOLUMNS(
    ADDCOLUMNS (
        GENERATE (
            ROW ( "Text", "0"&SELECTEDVALUE(Blood[Fancestor]) ),
            VAR TokenCount =
                PATHLENGTH ( [Text] )+1
            RETURN
                GENERATESERIES ( 1, TokenCount )
        ),
        "Word", PATHITEM ( [Text], [Value] )
    ),
    "Word",VALUE([Word]))
VAR Table1 =
    SELECTCOLUMNS(
    ADDCOLUMNS (
        GENERATE (
            ROW ( "Text", "0"&SELECTEDVALUE(Blood[Mancestor]) ),
            VAR TokenCount =
                PATHLENGTH ( [Text] )+1
            RETURN
                GENERATESERIES ( 1, TokenCount )
        ),
        "Word", PATHITEM ( [Text], [Value] )
    ),
    "Word",VALUE([Word]))
RETURN
DIVIDe(
if(SELECTEDVALUE(Blood[Generation]) <> 1,
  calculate(sum(Blood[AsiaBlood]), FILTER(ALL(Blood), Blood[ID] in  Table0 || Blood[ID] in Table1)),0
),
(POWER(2, SELECTEDVALUE(Blood[Generation])-1)))

AFRICA_check = 
VAR Table0 =
    SELECTCOLUMNS(
    ADDCOLUMNS (
        GENERATE (
            ROW ( "Text", "0"&SELECTEDVALUE(Blood[Fancestor]) ),
            VAR TokenCount =
                PATHLENGTH ( [Text] )+1
            RETURN
                GENERATESERIES ( 1, TokenCount )
        ),
        "Word", PATHITEM ( [Text], [Value] )
    ),
    "Word",VALUE([Word]))
VAR Table1 =
    SELECTCOLUMNS(
    ADDCOLUMNS (
        GENERATE (
            ROW ( "Text", "0"&SELECTEDVALUE(Blood[Mancestor]) ),
            VAR TokenCount =
                PATHLENGTH ( [Text] )+1
            RETURN
                GENERATESERIES ( 1, TokenCount )
        ),
        "Word", PATHITEM ( [Text], [Value] )
    ),
    "Word",VALUE([Word]))
RETURN
DIVIDe(
if(SELECTEDVALUE(Blood[Generation]) <> 1,
  calculate(sum(Blood[AfricaBlood]), FILTER(ALL(Blood), Blood[ID] in  Table0 || Blood[ID] in Table1)),0
),
(POWER(2, SELECTEDVALUE(Blood[Generation])-1)))

在此处输入图像描述

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

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