是否有特定的方法可以在 PowerBI 的列中有条件地添加? [英] Is there a specific way to conditionally add within a column in PowerBI?

查看:17
本文介绍了是否有特定的方法可以在 PowerBI 的列中有条件地添加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有特定的方法可以在 PowerBI 的列中进行有条件地添加?

Is there a specific way to conditionally add within a column in PowerBI?

样本数据:

Lang|Book_Type|Number|Book_Type (groups)
------------------------
A   |  B1     |  2   | B1
------------------------
B   |  B1     |  2   | B1
------------------------
C   |  B1     |  3   | B1
------------------------
A   |  B2     |  4   | B2
------------------------
B   |  B2     |  2   | B2
------------------------
A   |  B3     |  2   | B3
------------------------
A   |  B4     |  2   | B4
------------------------
B   |  B4     |  5   | B4
------------------------

所以,我想要做的是我想要一个第 5 列,其中对应于每一行,我有该类型的书籍总数,即在第 1,2 和 3 行旁边的第 5 列,我想要:7(=2+2+3),在第 4 行和第 5 行的第 5 列,我想要:6(=4+2).我也尝试过分组,但还是一样.有没有办法在 DAX 中做到这一点?我尝试了以下代码,但它给出了循环依赖错误.

So, what I want to do is that I want a 5th column, wherein corresponding to each row, I have the total number of books in that type, i.e., alongside row 1,2 and 3, in the 5th column, I would want: 7(=2+2+3), and alongside row 4 and 5, in the 5th column, I would want: 6(=4+2). I also tried grouping, but it's still the same. Is there a way to do this in DAX? I tried the following code, but it gives a circular dependency error.

SUMX (
    'Book_store',
    IF (
         ( Book_store[Book_Type] = Book_store[Book_Type (groups)] ),
        CALCULATE ( SUM ( 'Book_store'[Number] ) ),
        BLANK ()
    )
)

推荐答案

这是一个循环依赖的原因是你在 SUMX 的第一个参数(包括您正在添加的列...但您无法引用尚未创建的内容).

The reason this is a circular dependency is that you are calling the entire table as part of the definition of the new column in the first argument of SUMX (which includes the column you're adding... but you can't reference something you haven't yet created).

更好的方法可能如下所示

A better approach might look like this

CALCULATE (
    SUM ( Book_store[Number] ),
    ALLEXCEPT ( Book_store, Book_store[Book_type (groups)] )
)

这告诉它对 Number 列求和,忽略所有列除了指定的列.

This tells it to sum the Number column ignoring all columns except the one specified.

这篇关于是否有特定的方法可以在 PowerBI 的列中有条件地添加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆