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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
PaulHallam
Helper III
Helper III

Average of a value by date in one table between two dates in an unrelated table

Hi,

 

I have two tables

 

Table 1 
description, datetime and value

Table 2
description, location, owner, startdatetime, enddatetime

 

The tables are linked on the description field.

I want to return a table visualisation in PowerBI desktop showing
description, location, owner, startdatetime, enddatetime, averagevalue


I cant get a measure to calculate the averagevalue between the startdatetime and enddatetime. Can anyone help please? (there are millions of rows of data)

Table 1

descriptiondatetimevalue
A01/01/2022 09:001
A01/01/2022 09:101
A01/01/2022 09:201
A01/01/2022 09:301
A01/01/2022 09:401
A01/01/2022 09:501
A01/01/2022 10:001
A02/01/2022 09:001
A02/01/2022 09:000
A02/01/2022 09:000
A02/01/2022 09:000
A02/01/2022 09:001
A02/01/2022 09:001
A02/01/2022 09:001
A03/01/2022 09:001
A03/01/2022 09:001
A03/01/2022 09:001
A03/01/2022 09:000
A03/01/2022 09:000
A03/01/2022 09:000
A03/01/2022 09:001
A04/01/2022 09:001
A04/01/2022 09:001
A04/01/2022 09:001
A04/01/2022 09:001
A04/01/2022 09:001
A04/01/2022 09:001
A04/01/2022 09:001
A05/01/2022 09:001
A05/01/2022 09:001
A05/01/2022 09:001
A05/01/2022 09:000
A05/01/2022 09:000
A05/01/2022 09:000
A05/01/2022 09:000

 

Table 2

descriptionlocationownerstartdatetimeenddatetime
ABox1John01/01/2022 00:0002/01/2022 22:00
ABox2John02/01/2022 22:0003/01/2022 22:00
ABox2Dave03/01/2022 22:0005/01/2022 22:00

 

Result wanted in table visualisation in PowerBi Desktop

descriptionlocationownerstartdatetimeenddatetimeAverage Value
ABox1John01/01/2022 00:0002/01/2022 22:0078.6%
ABox2John02/01/2022 22:0003/01/2022 22:0057.1%
ABox2Dave03/01/2022 22:0005/01/2022 22:0071.4%
1 ACCEPTED SOLUTION

Hi, @PaulHallam 

Personally, I like to calculate the results step by step. Because once the result is wrong, I can easily find the problem.

I think you can add  the following measures to my original model to correct the total values.

 

Total1 = 
SUMX('Table 2','Table 2'[Total])
Count1 = SUMX('Table 2',[Count] )
Average1 = [Total1]/[Count1]

 

 Result:

veasonfmsft_0-1654063007235.png

Best Regards,
Community Support Team _ Eason

 

View solution in original post

4 REPLIES 4
v-easonf-msft
Community Support
Community Support

Hi, @PaulHallam 

Try these formulas below:
Measue:

Count = 
VAR a =
    MAX ( 'Table 2'[startdatetime] )
VAR b =
    MAX ( 'Table 2'[enddatetime] )
RETURN
    CALCULATE (
        COUNT ( 'Table 1'[datetime] ),
        FILTER ( 'Table 1', 'Table 1'[datetime] >= a && 'Table 1'[datetime] < b )
    )
Total = 
VAR a =
    MAX ( 'Table 2'[startdatetime] )
VAR b =
    MAX ( 'Table 2'[enddatetime] )
RETURN
    CALCULATE (
        SUM ( 'Table 1'[value] ),
        FILTER ( 'Table 1', 'Table 1'[datetime] >= a && 'Table 1'[datetime] < b )
    )
Average Value = [Total]/[Count] 

veasonfmsft_0-1653645973236.png

 

Best Regards,
Community Support Team _ Eason

Thanks 'v-easonf-msft'

PREVIEW
I have used the logic that you have provided but changed to one calculation as
 
Average =
VAR starttime = MAX(SensorInfo[SysStartTime])
VAR endtime = MAX(SensorInfo[SysEndTime])
RETURN
CALCULATE(
AVERAGE(Data[Value]),
FILTER(data,Data[DateTime]>=starttime && Data[DateTime]<endtime))
This works for the above query, thanks again BUT with totals on the totals dont return the count, total and average of the whole model it just returns the bottom row in the table. Is there a way to get the table totals working?
 

Hi, @PaulHallam 

Personally, I like to calculate the results step by step. Because once the result is wrong, I can easily find the problem.

I think you can add  the following measures to my original model to correct the total values.

 

Total1 = 
SUMX('Table 2','Table 2'[Total])
Count1 = SUMX('Table 2',[Count] )
Average1 = [Total1]/[Count1]

 

 Result:

veasonfmsft_0-1654063007235.png

Best Regards,
Community Support Team _ Eason

 

v-easonf-msft

This works a treat for my demo data, thank you so much.

I need to add to the real report and hopefully there are no issues. If you get a minute can you explain the logic behind the Measures.

Thanks Again

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

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.