Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
tomperro
Helper III
Helper III

Calculated column countrows in another table

Is there a way to create a calclulated column that will get a count of records from another table but keeping the report filters?
I have two tables
Employee table with an employee id and a test table with testing information with an employee id of the employee that took the tests. 
I have page leve filters and slicers on my report and I would like to add a calculated column to the employee table that counts the number of test the employee took, keeping all the filters.
I have tried keepfilters but this is not working.

Count of Tests=
CALCULATE(
    COUNTROWS (
            FILTER (
                TestTable,
                TestTable[EmployeeId__c] = SELECTEDVALUE( 'Employee'[Employee_ID__c])
                )
            )
        )
5 REPLIES 5
sergej_og
Super User
Super User

Hey @tomperro ,
since you have (I guess) a 1:n relationship between the tables you can use RELATEDTABLE (to calculate value from the n-side of the relationship) and store the result in a calculated column.
like so:

COUNTROWS(RELATEDTABLE(Sales)).

You have to put the formula on the 1-side of the relationship.

Regards

That does give me the count but it is not applying the filters.
For example, I do not want to count the test number 123 for the employees.
I do not want to put the filter in the formula, I would like to use the page level and slicers already applied.

Calculated column works with row context.
If you would like to tweek your results you have to use CALCULATE.

What about get all data (countrows) into calculated column and then work with measures and visualizations to get your goal? You can exlude every thing you want.
It's a bit hard to understand your scenario without seeing any sample data/results.

Regards

I need a calculated column. I am able to get a count by using RELATED but that does not include the slicers and report filters. I need to include the report filters.

For example:

Contact table

Contact ID

 

111

 

222

 

333

 

 

Evaluation Table

Test Number

Test Category

Contact ID

1

Category 1

111

2

Category 2

111

3

Category 3

111

1

Category 1

222

1

Category 1

222

2

Category 2

333

2

Category 2

333

1

Category 1

333

 

my report filters out counting category 2 test so here are my expected results

 

Contact ID

count of tests (calculated column-needs to be created)

111

2

222

2

333

1

 

using COUNTROWS(RELATEDTABLE(Evaluation)) in the contact table the page level filters are not applied, so this is what that calculated column returns

 

If you have a calculated column (physical in your data model) you can use this values to create measures for your dashboards or use it as filter...beacause your data model gives you this opportunity now.
Tell me pls, how would you like to provide your filters made on vizualization layer to your calculated column? It's just the other way around.
Calc column uses row context, not filter context.
You can tweek the results for reasons but do you really need to this in this case?

Have you tried to create this column and use the result for your dashboards?

Regards

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.