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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
reporter9
Frequent Visitor

Average per month group by

Hello,

I need the average of duration per month for the colors. It should be possible to filter the id. To calculate the average I need the sum for each month and color divide by distinctcount from the id for each month. This is my table (on the left). The expected result is the right table.

I created 2 measures to calculate the duration, but i get always the number of all id's not only the relevant od's (with duration in the selected month).

Can someone please help me?

 

Measure 3 = calculate(DISTINCTCOUNT(source[ID]),ALL(source, source[month_date]))

Measure 4 = calculate(sum(source[Duration])/source[Measure 3])

 

reporter9_0-1668579900995.png

 

1 ACCEPTED SOLUTION

Hi, @reporter9 

Hi,

Thank you for your quick response, I check your dax and find that the reason that causes this issue is that “format” funmction.

Because the “format” function return the “Text” type data , we can not compare the “Text” type data to the “Date” type data. And the other error is that “ _date < _quarter_end”. We can not use the _date as the condition, we need use the ‘Table’[month_date] because we are filtering the ‘Table’.

So in the end , you can try to user this dax:

Average2 = var _phase = SELECTEDVALUE('Table'[Phase])

var _date =VALUES('Table'[Month_Date])

var _quarter_end = DATE( YEAR( TODAY() ), QUARTER( TODAY() ) * 3 + 1, 1 ) - 1

var _duration =SUMX(FILTER( ALLSELECTED('Table'), 'Table'[month_date] in _date && 'Table'[Phase]=_phase) , [Duration])

var _count =COUNTROWS(DISTINCT(SELECTCOLUMNS( FILTER(ALLSELECTED('Table'),

'Table'[month_date] in _date

&& 'Table'[Month_Date]< _quarter_end

) ,"ID",[ID],"Month_date",[Month_Date])))

return

DIVIDE(_duration,_count)

vyueyunzhmsft_0-1668993428921.png

 

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

 

View solution in original post

12 REPLIES 12
v-yueyunzh-msft
Community Support
Community Support

Hi , @reporter9 

Here are the steps you can refer to :
(1)This is my test data :

vyueyunzhmsft_0-1668655578959.png

(2)We can create a measure :

 

Average = var _phase = SELECTEDVALUE('Table'[Phase])
var _date =SELECTEDVALUE('Table'[month_date])
var _duration =SUMX(FILTER( ALLSELECTED('Table'), 'Table'[month_date]=_date && 'Table'[Phase]=_phase) , [Duration])
var _count =COUNTROWS(DISTINCT(SELECTCOLUMNS( FILTER(ALLSELECTED('Table'),'Table'[month_date]=_date) ,"ID",[ID])))
return
DIVIDE(_duration,_count)

 

(3)Then we can put the fields we need on the visual and we will meet your need:

vyueyunzhmsft_1-1668655627601.png

 

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

 

 

First of all thank You. It seems it works for the month ( test are not finished) but if I drill up to the quarter and select serveral id's then I get sometimes a blank visual

Hi, @reporter9 

For your needs, can you describe in detail how you " drill up to the quarter " and what fields you put on the visual and can you provide the end result you want ?

 

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

Hi @v-yueyunzh-msft ,

Thank you for your reply. I have added the file with the example. Here you can see my problem.

Sorry, it is not possible for me to add the example. Here ist the screenshot and the table.

 

reporter9_0-1668753769710.png

 

ID      Phase  Duration Month_Date

55brown412022-01-31
11green202022-02-28
22green202022-02-28
33green202022-02-28
44green202022-02-28
55green42022-02-28
55brown452022-02-28
55yellow72022-02-28
11green432022-03-31
22green432022-03-31
33green432022-03-31
44green432022-03-31
11green542022-04-30
11yellow112022-04-30
22green542022-04-30
22yellow112022-04-30
33green642022-04-30
44green452022-04-30
44brown192022-04-30
5brown192022-05-31
11yellow332022-05-31
22yellow142022-05-31
22yellow32022-05-31
22brown162022-05-31
33green672022-05-31
33brown162022-05-31
33yellow32022-05-31
44brown222022-05-31
44yellow192022-05-31
5brown412022-06-30
11yellow552022-06-30
22yellow252022-06-30
33yellow252022-06-30
44yellow412022-06-30
1green212022-07-31
2green212022-07-31
3green212022-07-31
4green212022-07-31
5green22022-07-31
5brown432022-07-31
5yellow92022-07-31
22yellow362022-07-31
33yellow362022-07-31
44yellow522022-07-31
1green442022-08-31
2green442022-08-31
3green442022-08-31
4green442022-08-31
1green552022-09-30
1yellow122022-09-30
2green552022-09-30
2yellow122022-09-30
3green662022-09-30
4green462022-09-30
4brown202022-09-30
1yellow332022-10-31
2yellow142022-10-31
2yellow32022-10-31
2brown162022-10-31
3green682022-10-31
3brown162022-10-31
3yellow32022-10-31
4brown222022-10-31
4yellow192022-10-31
1yellow552022-11-30
2yellow252022-11-30
3yellow252022-11-30
4yellow412022-11-30
55brown202021-12-31

 

Best Regards,

reporter9

Hi , @reporter9 

For your need , we just need to update the measure to this and we will meet your need:

Average = var _phase = SELECTEDVALUE('Table'[Phase])
var _date =VALUES('Table'[Month_Date])
var _duration =SUMX(FILTER( ALLSELECTED('Table'), 'Table'[month_date] in _date && 'Table'[Phase]=_phase) , [Duration])
var _count =COUNTROWS(DISTINCT(SELECTCOLUMNS( FILTER(ALLSELECTED('Table'),'Table'[month_date] in _date) ,"ID",[ID])))
return
DIVIDE(_duration,_count)

The result is as follows:

vyueyunzhmsft_0-1668763527140.png

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

Hi @v-yueyunzh-msft ,
Thank you. If I drill to the quarter I see the sum for the phases, not the average.

reporter9_0-1668767449451.png

 

Hi , @reporter9 

Thank you for your quick feedback. Based on the screenshot you provided , i can understand that the issue happens when you try to dill to the quater and show the "sum" not the "average" . The  root cause is in my "distinctcount" is wrong .

You can just also update the dax to this:

Average = var _phase = SELECTEDVALUE('Table'[Phase])
var _date =VALUES('Table'[Month_Date])
var _duration =SUMX(FILTER( ALLSELECTED('Table'), 'Table'[month_date] in _date && 'Table'[Phase]=_phase) , [Duration])
var _count =COUNTROWS(DISTINCT(SELECTCOLUMNS( FILTER(ALLSELECTED('Table'),'Table'[month_date] in _date) ,"ID",[ID],"Month_date",[Month_Date])))
return
DIVIDE(_duration,_count)

Then we can meet your need in the end:

vyueyunzhmsft_0-1668823055048.png

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

 

Hi @v-yueyunzh-msft ,

loooks good. Additionally always the current quarter (var _quarter_end) should not be visable. I have 'update' your proposal and I have the issue that with this the visual is blank. Where is my fault?

Thank you.


Average = var _phase = SELECTEDVALUE('Table'[Phase])
var _date =VALUES('Table'[Month_Date])
var _quarter_end = format(DATE( YEAR( TODAY() ), QUARTER( TODAY() ) * 3 + 1, 1 ) - 1,"DD/MM/YYYY")
var _duration =SUMX(FILTER( ALLSELECTED('Table'), 'Table'[month_date] in _date && 'Table'[Phase]=_phase) , [Duration])
var _count =COUNTROWS(DISTINCT(SELECTCOLUMNS( FILTER(ALLSELECTED('Table'),
'Table'[month_date] in _date
&& _date < _quarter_end
) ,"ID",[ID],"Month_date",[Month_Date])))
return
DIVIDE(_duration,_count)

Hi, @reporter9 

Hi,

Thank you for your quick response, I check your dax and find that the reason that causes this issue is that “format” funmction.

Because the “format” function return the “Text” type data , we can not compare the “Text” type data to the “Date” type data. And the other error is that “ _date < _quarter_end”. We can not use the _date as the condition, we need use the ‘Table’[month_date] because we are filtering the ‘Table’.

So in the end , you can try to user this dax:

Average2 = var _phase = SELECTEDVALUE('Table'[Phase])

var _date =VALUES('Table'[Month_Date])

var _quarter_end = DATE( YEAR( TODAY() ), QUARTER( TODAY() ) * 3 + 1, 1 ) - 1

var _duration =SUMX(FILTER( ALLSELECTED('Table'), 'Table'[month_date] in _date && 'Table'[Phase]=_phase) , [Duration])

var _count =COUNTROWS(DISTINCT(SELECTCOLUMNS( FILTER(ALLSELECTED('Table'),

'Table'[month_date] in _date

&& 'Table'[Month_Date]< _quarter_end

) ,"ID",[ID],"Month_date",[Month_Date])))

return

DIVIDE(_duration,_count)

vyueyunzhmsft_0-1668993428921.png

 

Thank you for your time and sharing, and thank you for your support and understanding of PowerBI! 

 

Best Regards,

Aniya Zhang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

 

Hi @v-yueyunzh-msft ,

great support from you. I will test it and reply.
Thank you.

 

Shaurya
Memorable Member
Memorable Member

Hi @reporter9,

 

You can use:

 

Result Table = SUMMARIZE('Table','Table'[Phase],'Table'[Month Date],"Duration",SUM('Table'[Duration]),"Count",COUNT('Table'[ID]))

 

Works for you? Mark this post as a solution if it does!
Check out this blog of mine: How to Export Telemetry Data from Azure IoT Central into Power BI

Thanks for your reply. With this soloution it is not possible to filter the id

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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