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
jagostinhoCT
Post Partisan
Post Partisan

Average from latest

Hello,

 

Say I have the following sample data for multiple audits of projects.

 

I would like to create a measure for the average of the most recent audits performed to each project. Highlighted in yellow.

 

It feels like it should be something like

Average from Latest = CALCULATE(AVERAGEA(ProjectAuditsDB[Score]),MAX(ProjectAuditsDB[AuditDate]))

 

But I am aware that this may try to pick up the last date of them all. Regardless, it is producing an error.

"A function 'MAX' has been used in a True/False expression that is used as a table filter expression. This is not allowed."

 

How can I filter out the last audit date for each project and feed that into the measure?

 

Many thanks

J

 

 

 

Average from the latest.png

3 ACCEPTED SOLUTIONS
Vvelarde
Community Champion
Community Champion

hi @jagostinhoCT

 

1: Create a Summarize Table

 

PROJECTS-LASTAUDIT = SUMMARIZE(Audits;Audits[ProjectID];"AVG-LAST AUDIT"; CALCULATE(AVERAGE(Audits[Score]);filter(Audits;Audits[AuditDate]=Max(Audits[AuditDate]))))

 

2: Create a Measure Average

 

AVG-LASTAUDIT-PROJECTS = AVERAGE('PROJECTS-LASTAUDIT'[AVG-LAST AUDIT])

 

Replace with your fields & Table name

 

The average result is different because you highlighted twice rows of 433PED 8/6 and 14/06

 

EDIT: If you want all in only a measure

 

Medida2 =
AVERAGEX (
    SUMMARIZE (
        Audits;
        Audits[ProjectID];
        "AVERAGE-LASTAUDIT"CALCULATE (
            AVERAGE ( Audits[Score] );
            FILTER ( Audits; Audits[AuditDate] = MAX ( Audits[AuditDate] ) )
        )
    );
    [AVERAGE-LASTAUDIT]
)




Lima - Peru

View solution in original post

Sean
Community Champion
Community Champion

@jagostinhoCT go with @Vvelarde solution from the EDIT:

 

Good job! @Vvelarde

 

Here you can copy this one has same table name and columns (I think) as yours... Good Luck! Smiley Happy

 

Measure =
AVERAGEX (
    SUMMARIZE (
        ProjectAuditsDB,
        ProjectAuditsDB[ProjectID],
        "Average of Last Audit", CALCULATE (
            AVERAGE ( ProjectAuditsDB[Score] ),
            FILTER (
                ProjectAuditsDB,
                ProjectAuditsDB[AuditDate] = MAX ( ProjectAuditsDB[AuditDate] )
            )
        )
    ),
    [Average of Last Audit]
)

View solution in original post

hi @jagostinhoCT

 

Summarize function like his name says create a summary of the table grouping by 1 or several columns and you can use a function to find or calculate something. In the previous measure or table you are grouping all rows by Project ID and calculate the average of every project ID.

 

Now for add a column with the last date for every project id you should add to the formula:

 

SummarizeTable=Summarize(Table; GroupField (In your case: ProjectID);Expresion(The average);"Last Date"; LASTDATE(Audits[AuditDate]))

 

 




Lima - Peru

View solution in original post

19 REPLIES 19
Vvelarde
Community Champion
Community Champion

 

hi @jagostinhoCT

 

Average from Latest = CALCULATE(AVERAGEA(ProjectAuditsDB[Score]),Filter(ProjectAuditsDB; ProjectAuditsDB(AuditDate)=MAX(ProjectAuditsDB[AuditDate]))

 

 

 




Lima - Peru

@Vvelarde

 

Many thanks for the reply

the following 

= CALCULATE(AVERAGEA(ProjectAuditsDB[BIM Quality Score],FILTER(ProjectAuditsDB,ProjectAuditsDB[AuditDate]=MAX(ProjectAuditsDB[AuditDate]))))

 

renders this error

"Too many arguments were passed to the AVERAGEA function. The maximum argument count for the function is 1"

 

What does this mean?

) is missing before the filter

 

 

= CALCULATE(AVERAGEA(ProjectAuditsDB[BIM Quality Score]),FILTER(ProjectAuditsDB,ProjectAuditsDB[AuditDate]=MAX(ProjectAuditsDB[AuditDate]))))




Lima - Peru

including the ) produces a different error

 

The syntax for ')' is incorrect. (DAX(CALCULATE(AVERAGEA(ProjectAuditsDB[BIM Quality Score]),FILTER(ProjectAuditsDB,ProjectAuditsDB[AuditDate]=MAX(ProjectAuditsDB[AuditDate])))))).

 

Delete ) at the End of the DAX Formula. This error is saying that you have too many ) at the end




Lima - Peru

MEDIDA =
CALCULATE (
    AVERAGEA ( ProjectAuditsDB[BIM Quality Score] ),
    FILTER (
        ProjectAuditsDB,
        ProjectAuditsDB[AuditDate] = MAX ( ProjectAuditsDB[AuditDate] )
    )
)



Lima - Peru

@Vvelarde

 

Thank you.

 

The formula is working. But the calculated value is not correct.

By using the sample data above, it should be 10%. It is calculating it as 14.66%.

 

I have copy/pasted the sample data in case you want to try it.

 

Also, do not know if this would be related, when I try to apply the measure to the real dataset it returns an empty value.

 

Average from the latest2.png

 

 

ProjectAuditIDProjectIDAuditDateScore
433PED_BA07433PED14/06/201615%
433PPC_BA03433PPC13/06/201610%
433PED_BA06433PED08/06/20167%
433PPC_BA02433PPC13/06/201610%
A630BDP_BA01A630BDP25/05/20169%
433PED_BA05433PED23/05/201612%
433PPC_BA01433PPC13/06/201610%
433LOLH_BA01433LOLH16/05/20169%

Hi the Value is correct because 14/06/2016 is the max date (your first row) not 13/06/2016.

 

maxdate.png




Lima - Peru

OMG!

 

Just noticed it now. I am very sorry as I phrased the question wrong.

 

My goal is to calculate the average by summing the last audit of each project (ProjectID). Highlighted in yellow in the original post.

 

Does it make sense?

 

Thank you for all of your help.

@KHorseman there has to be a way to do this without a Summary Table (will give it a go later - just not sure when??? Smiley Happy)

 

I don't feel like doing anything today (its over 100 here)

 

BTW also let us know if you do try that ImkeF (M solution) in the other post Smiley Happy

 

Picture2.png

 

Vvelarde
Community Champion
Community Champion

hi @jagostinhoCT

 

1: Create a Summarize Table

 

PROJECTS-LASTAUDIT = SUMMARIZE(Audits;Audits[ProjectID];"AVG-LAST AUDIT"; CALCULATE(AVERAGE(Audits[Score]);filter(Audits;Audits[AuditDate]=Max(Audits[AuditDate]))))

 

2: Create a Measure Average

 

AVG-LASTAUDIT-PROJECTS = AVERAGE('PROJECTS-LASTAUDIT'[AVG-LAST AUDIT])

 

Replace with your fields & Table name

 

The average result is different because you highlighted twice rows of 433PED 8/6 and 14/06

 

EDIT: If you want all in only a measure

 

Medida2 =
AVERAGEX (
    SUMMARIZE (
        Audits;
        Audits[ProjectID];
        "AVERAGE-LASTAUDIT"CALCULATE (
            AVERAGE ( Audits[Score] );
            FILTER ( Audits; Audits[AuditDate] = MAX ( Audits[AuditDate] ) )
        )
    );
    [AVERAGE-LASTAUDIT]
)




Lima - Peru

Hello @Vvelarde,

 

I opted out by using the Summarized Table as I feel it gives me more options.

 

I am having some trouble trying to understand the SUMMARIZE function syntax.

 

Is there a way that I can add a column showing the date for the last audits?

 

Many thanks for your help.

 

J

Last Audit Date.png

hi @jagostinhoCT

 

Summarize function like his name says create a summary of the table grouping by 1 or several columns and you can use a function to find or calculate something. In the previous measure or table you are grouping all rows by Project ID and calculate the average of every project ID.

 

Now for add a column with the last date for every project id you should add to the formula:

 

SummarizeTable=Summarize(Table; GroupField (In your case: ProjectID);Expresion(The average);"Last Date"; LASTDATE(Audits[AuditDate]))

 

 




Lima - Peru

So simple.

Awesome.

 

Many thanks once again @Vvelarde

@Sean I think @Vvelarde's latest edit looks like it's got you covered, unless I'm missing something. I haven't done any real testing though.

 

I didn't try ImkeF's solution yet because I found a DAX way after all. I still like my original solution. I mean it's bad, it's inefficient, it's hard to tell what the code is doing if you don't already know, and I'm definitely never using it again, but feel like it was at least a unique approach to the problem.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Sean
Community Champion
Community Champion

@jagostinhoCT go with @Vvelarde solution from the EDIT:

 

Good job! @Vvelarde

 

Here you can copy this one has same table name and columns (I think) as yours... Good Luck! Smiley Happy

 

Measure =
AVERAGEX (
    SUMMARIZE (
        ProjectAuditsDB,
        ProjectAuditsDB[ProjectID],
        "Average of Last Audit", CALCULATE (
            AVERAGE ( ProjectAuditsDB[Score] ),
            FILTER (
                ProjectAuditsDB,
                ProjectAuditsDB[AuditDate] = MAX ( ProjectAuditsDB[AuditDate] )
            )
        )
    ),
    [Average of Last Audit]
)

You guys are great!

 

Many thanks for your time and patience @Vvelarde and @Sean

 

Am taking the first steps with the software (and the whole data analitics thing, to be honest, as my degree is in architecture. I mean designing buildings and all)

 

have been learning a lot by self teaching, watching tutorials and help files. But sometimes, most of the times,...ok...90% of the times, it is very hard to figure out on how can it be done. 

 

Thanks a lot, once again.

J

Sean
Community Champion
Community Champion

@KHorseman I've been playing with the Generate Schedule Table again (pretty much all day - since I saw it) Smiley Happy

 

Somebody had a question for Owen and the answer gave me an idea

 

So I'm experimenting with week numbers now Smiley Happy not enough time for everything I want to try out Smiley Happy 

 

@jagostinhoCT The latest date in the data you posted is June 14 - 15% and its only one value.

 

You may be thinking of June 13 but that is not the latest data in the sample you posted.

 

In the original image you posted that first value was June 4 and the latest date then was June 13.

 

Does this make sense?

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.