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
kralco626
Frequent Visitor

Calculate average, std for each category

I have two tables

 

Work:

Work ID (unique)

Work Type

Work Group

 

Entries:

Work ID (points to Work table)

Hours

Dollars

 

Basically, this represents work we do, and each entry that someone put towards the work.

 

Work IDWork Type
1A
2A
3

A

4

B

5

B

 

Work IDHours
18
15
2

9

2

5

3

8

3

6

3

3

4

5

5

6

5

7

5

8

 

I want to find values such as avg, std, count etc for Work Type per Work item.

 

So for A the count should be 3, not 7. The average should be the average of (13,14,17) not the average of the 7 records in the entry table. Standard devision same thing, it should be based on those three values.

Similarly, the count for B should be 2, not 4.

 

Basically, I don't care how many entires people put towards the work. I want to know the average number of hours spent on each Work item of type A, B etc.

 

Seems like this is rather basic and I'm missing something, becuase I think I thought be able to accomplish this without too much effort.

1 ACCEPTED SOLUTION
v-shex-msft
Community Support
Community Support

Hi @kralco626,

 

You can try to use below measure to get grouped average value:

 

Average = 
VAR idlist =
    CALCULATETABLE (
        VALUES ( 'Work'[Work ID] ),
        FILTER ( ALLSELECTED('Work'), 'Work'[Work Type] = MAX ( 'Work'[Work Type] ) )
    )
RETURN
    DIVIDE (
        SUMX ( FILTER (ALLSELECTED( Entries), [Work ID] IN idlist ), [Hours] ),
        COUNTROWS ( idlist ),
        0
    )

12.PNG

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

View solution in original post

10 REPLIES 10
v-shex-msft
Community Support
Community Support

Hi @kralco626,

 

You can try to use below measure to get grouped average value:

 

Average = 
VAR idlist =
    CALCULATETABLE (
        VALUES ( 'Work'[Work ID] ),
        FILTER ( ALLSELECTED('Work'), 'Work'[Work Type] = MAX ( 'Work'[Work Type] ) )
    )
RETURN
    DIVIDE (
        SUMX ( FILTER (ALLSELECTED( Entries), [Work ID] IN idlist ), [Hours] ),
        COUNTROWS ( idlist ),
        0
    )

12.PNG

 

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.
jthomson
Solution Sage
Solution Sage

I'd start by grouping the rows in your entries table in Power Query so that it sums up the hours and gives you id 1, hours 13, id 2, hours 14 etc, that should make your calculations a fair bit easier

Ya, I could do that. However, I would loose a lot of flexability. For example, what if the Entires table had another column, and I wanted to filter on that column and have the values updated accordingly?

here is link to sample pbix 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

OK, so you put the Sum of hours in the hours table instead of the Work table.

 

That does seem to help.

 

However, why is the standard devision still calculated from the Hours.Hours field? Wouldn't this still give the standard devsion of the entires, not of the work items.

 

For example, If I filter out so I only view Work ID 5, I get a standard devision of .82. With only one work item, I should get a standard devision of 0.

OK, so I think I got it to work. I created a calculated column on the Work table that just points to the Measure on the Entries table.

 

On Entries you added: Sum of Hours = SUM(Hours[Hours])

On the Work table I added: Hours = Hours[Sum of Hours]

 

Now I can use the Work.Hours field in my visualization and choose if I want sum, count, std, etc.

 

That seems to have worked nicely

 

 

Also, just FYI for anyone else tyring to do this, I changed the filkter type on the relationship to both, so that if the entries table is filtered, the calculations are updated as well.

So I have this working in my dataset. The only hurdle is that I'm required to have the filter set to Both, and in my real data model with many tables, I can't do this for all the applicable relationships.

 

So this gets me one step closer, but not all the way there.

 

glad to hear you are making progress, need to know what the relationship is with another table and how it has impact that you are not getting expected result. 

 

can you share picture of relationship model and the expected result?



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

parry2k
Super User
Super User

Did you created the relationship between tables in powerbi?



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Yes, I created a relationship between the two table in Power BI

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.