Forum Discussion
Sum
- Anonymous2 years ago
Hi jyaul786 ,
I suggest you to try code as below to create a calculated column.
Result = VAR CategoryA = SUMX ( FILTER ( 'Table', 'Table'[Category] in {"A","C"} ), 'Table'[value] ) RETURN IF ( [Category] = "A", CategoryA, 0 )Result is as below.
Best Regards,
Rico ZhouIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
To achieve the desired result using DAX (Data Analysis Expressions) in Power BI or another similar tool, you can create a calculated column that calculates the result for each row based on your condition. Here's a step-by-step guide on how to do this:
Open your Power BI Desktop or similar tool.
In the Fields pane, select the table where your data is located.
Click on "Modeling" in the top menu bar.
Select "New Column" to create a new calculated column.
Name the new column, for example, "Result."
Use the following DAX formula to calculate the result based on your condition:
Result =
VAR SelectedCategories = {"A", "C"}
RETURN
IF(
SUMMARIZE(FILTER(YourTableName, YourTableName[Category] IN SelectedCategories), YourTableName[Category]),
CALCULATE(SUM(YourTableName[value]))
)
Replace YourTableName with the name of your table where the data is stored.
This formula does the following:
It first defines a variable SelectedCategories that contains the categories you want to include in the calculation ("A" and "C" in this case).
Then, it uses the FILTER function to filter the table to include only rows where the Category is in the selected categories.
Next, it uses the SUMMARIZE function to create a table with distinct Category values based on the filtered data.
Finally, it calculates the sum of the "value" column for the selected categories using the CALCULATE function.
- Click the checkmark (√) to accept the formula.
Now, your table should have a new "Result" column that contains the calculated results for each row based on the condition. You can display this column in your table visual to see the results as you described in your example.
Note: Make sure to replace "YourTableName" with the actual name of your table in the DAX formula.
Also Try This:
I see that you want the "Result" column to be calculated differently. Based on your updated requirement, you want the "Result" column to be 17 for all rows in Category A and 0 for all other rows. In this case, you should use a different DAX formula for the "Result" column. Here's the updated DAX formula:
Result =
IF(
YourTableName[Category] = "A",
CALCULATE(SUM(YourTableName[value]), FILTER(YourTableName, YourTableName[Category] = "A")),
0
)
This formula does the following:
It checks if the Category is "A" for each row using the IF statement.
If the Category is "A," it calculates the sum of the "value" column for rows where the Category is "A" using the CALCULATE function with a FILTER condition.
If the Category is not "A," it assigns a value of 0 to the "Result" column for that row.
Now, your "Result" column should be calculated as 17 for rows with Category "A" and 0 for all other rows, as you've described in your expected result.
If I answered your question, please mark my post as solution, Appreciate your Kudos.