Forum Discussion
Dynamically compute a calculated column based on date slicer
I have two tables namely category and sales. The category table contains categoryName, categoryID, and subcategoryIDs (which is unique). The sales table contanis the subcategoryID and sales date.
The sales tables is connected to the category table via subcategoryid. ( One to many relation)
1) I have created a calculated column called count of subcategoryID in the category table which basically counts the occurence of each subcategory ID in the sales table.
count of subcategoryID = COUNTROWS(RELATEDTABLE(sales))
2) I have created another calculated column called New Category Name which sums up the values from the column created above based on category ID . If SUM is < 5, then the value is "Other", else we retain the Category Name.
Here's the code:
New Category Name = IF(CALCULATE (
SUM ('category'[count of subcategoryID] ),
FILTER (
ALL ( 'category' ),
'category'[categoryID] = EARLIER('category'[categoryID])
)
) < 5, "Other", 'category'[category name])
I have a pie chart which displays the New Category Name based on the sales dates. I also have a date slicer for sales date. The problem is, when I change the date slider, the pie chart displays the CategoryName of categories whose count is less than 5 instead of labelling them as "other" and counting them in the "Other" bucket.
I am assuming the issue is because the calculated column New Category Name has the calculation for the entire span of sales dates instead of the date range selected in the slider. How can I dynamically change New Category Name based on slider dates ?
6 Replies
- AnonymousNot applicable
What happens if you use ALLEXCEPT like this:
New Category Name = IF(CALCULATE (
SUM ('category'[count of subcategoryID] ),
FILTER (
ALLEXCEPT ( 'category', 'category'[Date] ),
'category'[categoryID] = EARLIER('category'[categoryID])
)
) < 5, "Other", 'category'[category name])
Where 'category'[Date] is your date field and the date used in your slicer.- sieedHelper II
Anonymous, there is no category date in the category table. There i showever sales_date in the sales table. Do you want me to use that instead?
- Eric_ZhangMicrosoft Employee
sieed wrote:
I have two tables namely category and sales. The category table contains categoryName, categoryID, and subcategoryIDs (which is unique). The sales table contanis the subcategoryID and sales date.
The sales tables is connected to the category table via subcategoryid. ( One to many relation)
1) I have created a calculated column called count of subcategoryID in the category table which basically counts the occurence of each subcategory ID in the sales table.
count of subcategoryID = COUNTROWS(RELATEDTABLE(sales))
2) I have created another calculated column called New Category Name which sums up the values from the column created above based on category ID . If SUM is < 5, then the value is "Other", else we retain the Category Name.
Here's the code:New Category Name = IF(CALCULATE (
SUM ('category'[count of subcategoryID] ),
FILTER (
ALL ( 'category' ),
'category'[categoryID] = EARLIER('category'[categoryID])
)
) < 5, "Other", 'category'[category name])I have a pie chart which displays the New Category Name based on the sales dates. I also have a date slicer for sales date. The problem is, when I change the date slider, the pie chart displays the CategoryName of categories whose count is less than 5 instead of labelling them as "other" and counting them in the "Other" bucket.
I am assuming the issue is because the calculated column New Category Name has the calculation for the entire span of sales dates instead of the date range selected in the slider. How can I dynamically change New Category Name based on slider dates ?Yes, the calculated column won't vary according to slicer selection. You can take some tricks by using measures to show [category name] or "Other" values, but you can't use that as a legend/details in a pie chart.
- sieedHelper II
Eric_Zhang, what are the tricks? Wold appreciate if you can share some more info.