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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Priscila_Gr
Frequent Visitor

Calculate average

I have a  table date, name_id and name_id_phase. Each day represents a day for a name_id and the respective phase
I would like to calculate the average days in which only the name_id that changed phase.

My objective is to assemble a table according to the image (only considering those that are no longer in the phase).

 

 

phase.PNG

 

Here it is a data sample:

 

Name idPhaseDate
81266Phase 324-Feb-23
81266Phase 325-Feb-23
81266Phase 326-Feb-23
81266Phase 327-Feb-23
81266Phase 328-Feb-23
81266Phase 42-Mar-23
81266Phase 43-Mar-23
81266Phase 44-Mar-23
82333Phase 324-Feb-23
82333Phase 325-Feb-23
82333Phase 326-Feb-23
82333Phase 327-Feb-23
82333Phase 328-Feb-23
82333Phase 32-Mar-23
82333Phase 33-Mar-23
82333Phase 34-Mar-23
851111Phase 124-Feb-23
851111Phase 325-Feb-23
851111Phase 326-Feb-23
851111Phase 327-Feb-23
851111Phase 328-Feb-23
851111Phase 32-Mar-23
851111Phase 33-Mar-23
851111Phase 34-Mar-23
2 REPLIES 2
MAwwad
Solution Sage
Solution Sage

 

To calculate the average days it takes for a name_id to change phase, you can follow these steps:

  1. First, you need to identify the names that changed phases. You can do this by grouping the table by Name id and counting the number of distinct phases. If the count is greater than 1, it means the name changed phase.

  2. Next, you can filter the table to include only the rows where the name changed phase.

  3. For each name_id that changed phase, you need to calculate the number of days it took to change phase. To do this, you can use the DATEDIFF function to calculate the difference between the dates for the first and last row for each name_id.

  4. Finally, you can calculate the average days it took for each name_id to change phase.

Here's the DAX code to accomplish this:

 

 
Average Days = VAR ChangedNames = FILTER ( ADDCOLUMNS ( SUMMARIZE ( 'Table', 'Table'[Name id], "DistinctPhases", DISTINCTCOUNT ( 'Table'[name_id_phase] ) ), "ChangedPhase", IF ( [DistinctPhases] > 1, 1, 0 ) ), [ChangedPhase] = 1 ) RETURN AVERAGEX ( ChangedNames, DATEDIFF ( CALCULATE ( MIN ( 'Table'[Date] ), FILTER ( 'Table', 'Table'[Name id] = EARLIER ( 'Table'[Name id] ) && 'Table'[name_id_phase] <> EARLIER ( 'Table'[name_id_phase] ) ) ), CALCULATE ( MAX ( 'Table'[Date] ), FILTER ( 'Table', 'Table'[Name id] = EARLIER ( 'Table'[Name id] ) && 'Table'[name_id_phase] <> EARLIER ( 'Table'[name_id_phase] ) ) ), DAY ) )
 

This code assumes your table is named 'Table', and the columns are named 'Name id', 'name_id_phase', and 'Date'. The result will be the average number of days it took for each name_id to change phase, only considering the names that are no longer in the previous phase.

Thank you for your answer.

Unfortunately it returns an error:

A single value for column 'name_id_phase' in table 'Table' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result.

Any ideas?

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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