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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
meiwah
Resolver I
Resolver I

How reference row context in addcolumn

I've a table as below which contains the CaseID of a case, the division of a case and the predicted division of a case. This predicted value is computed by calling a ML model. 

 

CaseID Division Division Prediction
12345AA
12346AB
12347BB
12348BC
12349CC
12350CC
12351CA

 

I would like to compute a table to summarise the results. I've tried to use ADDCOLUMN but can't get it right. Anyone, pls help to provide the correct dax syntax. Thanks! 🙂

 

Division No Cases No Predictions 
A22
B22
C33
1 ACCEPTED SOLUTION
meiwah
Resolver I
Resolver I

Thank you for all help! I've managed to find the solution.

 

ADDCOLUMNS(
   values(Table[Division]),
   "No Cases", CALCULATE(countrows(Table)),
   "No Predictions", CALCULATE(COUNTROWS(Table), Table[Division Prediction]=earlier([Division]), All(Table[Division]))
)

 

I have used the first column in AddColumns as the table I intend to loop through, which is Division.

 

Then use Calculate to do a context transition, bringing Table[Division] the row context into the filter context. Hence, "No Cases" will have the correct figure.

 

Then use Earlier to reference the filter context from outside of Calculate, to use it on Table[Division Prediction]. Remove the filter on Table[Division]

View solution in original post

6 REPLIES 6
meiwah
Resolver I
Resolver I

Thank you for all help! I've managed to find the solution.

 

ADDCOLUMNS(
   values(Table[Division]),
   "No Cases", CALCULATE(countrows(Table)),
   "No Predictions", CALCULATE(COUNTROWS(Table), Table[Division Prediction]=earlier([Division]), All(Table[Division]))
)

 

I have used the first column in AddColumns as the table I intend to loop through, which is Division.

 

Then use Calculate to do a context transition, bringing Table[Division] the row context into the filter context. Hence, "No Cases" will have the correct figure.

 

Then use Earlier to reference the filter context from outside of Calculate, to use it on Table[Division Prediction]. Remove the filter on Table[Division]

devanshi
Helper V
Helper V

divpredic =
CALCULATE(COUNTROWS('Tablename'),DISTINCT(Division),DISTINCT(Prediction))

Thanks Devanshi! But the measure didn't give the right output 🙂

johnt75
Super User
Super User

I would create a dimension table for your divisions, simply DISTINCT('Table'[Division]), create an active relationship from this to the division column in your fact table and create an inactive relationship from it to your predicted division column. You could then create measures like

No Cases = COUNTROWS( 'Table' )

No Predicted Cases = CALCULATE( COUNTROWS('Table'), USERELATIONSHIP( 'Table'[Predicted Division], Division[Division]) )
tamerj1
Super User
Super User

Hi @meiwah 

please try

NeawTable =
SUMMARIZE (
'Table',
'Table'[Division],
"No Cases", COUNTROWS ( 'Table' ),
"No Predictions", DISTINCTCOUNT ( 'Table'[Division Prediction] )
)

Thanks Tamerj1! But the measure didn't give the right output 🙂

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors