Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Averages by String

Hello,

 

I am trying to find an average based on a string value. Here is a little background information:

1. I am trying to find the average days residents spend during their time in a residence

2. Residents can live in apts, townhomes, villas or houses

3. I got the data to show their "path" and the "number of days" they spent at each place, so the data looks like this:

 

Resident IDPathDays in Each Path
1Apt, Townhome, House385, 400, 600
2Villa, House365, 500
3Apt, Townhome, House400, 90, 285
4Townhome, Villa, House200, 160, 390
5Apt, Townhome, House90, 180, 400

 

Now, I am trying to get an average number of days in each location, so I would want to average the days that resident 1, 3, and 5 had together. My goal table would look like this

PathNumber of Residents

Average Days in each Residence

Apt, Townhome, House3291, 233, 428
Villa, House1365, 500
Townhome, Villa, House1200, 160, 390

 

Any help would be much appreciated!!

 

Thank you!

  • Hi Anonymous 

    I have shared a sample PBIX here.

     

    I would recommend you transform your data into this form to make the calculations easier:

     

    Resident IDPath DescriptionPathItemTypeDays
    1Apt, Townhome, House1Apt385
    1Apt, Townhome, House2Townhome400
    1Apt, Townhome, House3House600
    2Villa, House1Villa365
    2Villa, House2House500
    3Apt, Townhome, House1Apt400
    3Apt, Townhome, House2Townhome90
    3Apt, Townhome, House3House285
    4Townhome, Villa, House1Townhome200
    4Townhome, Villa, House2Villa160
    4Townhome, Villa, House3House390
    5Apt, Townhome, House1Apt90
    5Apt, Townhome, House2Townhome180
    5Apt, Townhome, House3House400

     

    I have done this in Power Query in the above PBIX.

     

    Then create measures:

     

    Number of Residents = 
    DISTINCTCOUNT ( Residence[Resident ID] )
    
    Average Days = 
    AVERAGE ( Residence[Days] )
    
    Average Days Concatenated = 
    IF ( 
        HASONEVALUE ( Residence[Path Description] ), // ensure just one Path Description is selected
        CONCATENATEX ( 
            VALUES ( Residence[PathItem] ),
            ROUND( [Average Days], 0),
            ", ",
            Residence[PathItem]
        )
    )

     

     

    After doing this, you can create a table similar to the one you posted:

     

    Anyway that is how I would approach this. Hopefully that's of some use 🙂

     

    Regards,

    Owen

2 Replies

  • Hi Anonymous 

    I have shared a sample PBIX here.

     

    I would recommend you transform your data into this form to make the calculations easier:

     

    Resident IDPath DescriptionPathItemTypeDays
    1Apt, Townhome, House1Apt385
    1Apt, Townhome, House2Townhome400
    1Apt, Townhome, House3House600
    2Villa, House1Villa365
    2Villa, House2House500
    3Apt, Townhome, House1Apt400
    3Apt, Townhome, House2Townhome90
    3Apt, Townhome, House3House285
    4Townhome, Villa, House1Townhome200
    4Townhome, Villa, House2Villa160
    4Townhome, Villa, House3House390
    5Apt, Townhome, House1Apt90
    5Apt, Townhome, House2Townhome180
    5Apt, Townhome, House3House400

     

    I have done this in Power Query in the above PBIX.

     

    Then create measures:

     

    Number of Residents = 
    DISTINCTCOUNT ( Residence[Resident ID] )
    
    Average Days = 
    AVERAGE ( Residence[Days] )
    
    Average Days Concatenated = 
    IF ( 
        HASONEVALUE ( Residence[Path Description] ), // ensure just one Path Description is selected
        CONCATENATEX ( 
            VALUES ( Residence[PathItem] ),
            ROUND( [Average Days], 0),
            ", ",
            Residence[PathItem]
        )
    )

     

     

    After doing this, you can create a table similar to the one you posted:

     

    Anyway that is how I would approach this. Hopefully that's of some use 🙂

     

    Regards,

    Owen

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks Owen!! This worked perfectly!