问题
Is there a specific way to conditionally add within a column in PowerBI?
Sample data:
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
------------------------
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 ()
)
)
回答1:
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)] )
)
This tells it to sum the Number column ignoring all columns except the one specified.
来源:https://stackoverflow.com/questions/64559662/is-there-a-specific-way-to-conditionally-add-within-a-column-in-powerbi