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
nericksx
Regular Visitor

Using DAX to get a counted expression grouped by quarter

Suppose acedemic workflows, in different subjects, are perioically tested & retested. You can see in the dataset below: the subject, the workflow name, the quarter it was tested & the score. I have been able to create measures that return the previous test quarter and score (see the variable in the code), if the workflow has been tested more than once. What I need is to count up all the workflows that have improved (i.e. the most recent score is higher than the last score).


The exact table visual that I would expect is:

Year QuarterCount of WorkflowsCount of Improved Workflows
2021 Q25 
2021 Q33 
2021 Q44 
2022 Q1115
2022 Q222

 

I'm getting is a visual that looks like:

Year QuarterCount of WorkflowsCount of Improved Workflows
2021 Q25 
2021 Q33 
2021 Q444
2022 Q11111
2022 Q22 

As you can see, it is aggregating by quarter, but not in the way I would expect (if I add the "subject" column, it aggregates correctly, but I don't *want* the subject column there).
The Data:
Scores By Workflow =

ProductWorkflowWorkflow Score AvgYear Quarter
MathChapter Test6.382022 Q1
MathChapter Test5.672021 Q2
MathRegister Account6.502021 Q4
MathCreate Report7.002022 Q1
MathCreate Report6.502021 Q2
MathGet Help6.252021 Q4
MathGet Help (TSA)4.502021 Q4
MathCreate Graph6.902022 Q1
MathCreate Graph6.632021 Q2
MathCorrections7.002022 Q1
MathCorrections6.602021 Q2
MathChapter Review6.502022 Q1
MathChapter Review6.002021 Q2
ScienceIdentify Variables5.332022 Q2
ScienceCommunicate VM Risk6.082022 Q2
ScienceGet Help6.002021 Q3
ScienceIdentify Variables4.352021 Q3
EnglishGet Help4.672022 Q1
EnglishBiography6.402022 Q1
EnglishComplete Reviews6.252022 Q1
EnglishEditing5.102022 Q1
MathSorting4.002021 Q4
ScienceCommunication3.202021 Q3
EnglishChapter Test6.002022 Q1
EnglishGroup Work5.802022 Q1


The code:

 

 

VAR _mostRecentTestDate = 
     MAX( 'Scores by Workflow'[Year Quarter] )

VAR _mostRecentScore = 
     AVERAGE('Scores by Workflow'[Workflow Score Avg]) 
   
VAR _prevTestDate = 
     CALCULATE( 
          MAX( 'Scores by Workflow'[Year Quarter] ), 
          'Scores by Workflow'[Year Quarter] < _mostRecentTestDate 
     )

VAR _prevScore = 
     CALCULATE( 
          AVERAGE('Scores by Workflow'[Workflow Score Avg]), 
          'Scores by Workflow'[Year Quarter] = _prevTestDate 
     )

VAR _improvedCount = 
    IF(
        NOT(ISBLANK(_prevTestDate)),
        CALCULATE(
            COUNT('Scores by Workflow'[Workflow]),
            FILTER('Scores by Workflow',_mostRecentScore > _prevScore)  
        )
    )
    
RETURN
_improvedCount 

 

 

Other info:

I have a date table that has a 1:many relationship with the Scores By Workflow table.

5 REPLIES 5
nericksx
Regular Visitor

Good morning, @Anonymous  thank you for trying to help with this!
I'm not looking for a comparison of an average or max score for quarters or years. I'm looking to see individual workflow score improvement from the previous testing session, which most likley won't be the last quarter. Check out this table (note - since there are multiple tasks within the same workflow during a testing session, workflow scores are averaged within a subject and quarter):

SubjectYear QuarterWorkflow Score AvgPrev Workflow Score AvgPrev Test DateWorkflow
Math2022 Q16.385.672021 Q2Chapter Test
Math2022 Q176.52021 Q2Create Report
Math2022 Q16.96.632021 Q2Create Graph
Math2022 Q176.62021 Q2Corrections
Math2022 Q16.562021 Q2Chapter Review
Science2022 Q26.083.22021 Q3Communicate VM Risk
Science2022 Q25.334.352021 Q3Identify Variables

These are the only subjects and workflows that have been retested. Every one of them improved. I'd like to count how many improved per quarter *only if they've been retested*. Based on the table above I would expect a count of 5 for 2022 Q1, and 2 for 2022 Q2 (see my first example table at the top). Any result that has a count in 2021 Q4 I know is not what I want, because there were no restests done in 2021 Q4.

Anonymous
Not applicable

Hi @nericksx ,

I create two measure for Maximum and Average

 

Count of Improved Workflows by Avg Score =
VAR cur_date =
    SELECTEDVALUE ( 'Scores By Workflow'[Year Quarter] )
VAR tmp1 =
    FILTER ( ALL ( 'Scores By Workflow' ), [Year Quarter] < cur_date )
VAR pre_date =
    MAXX ( tmp1, [Year Quarter] )
VAR tmp2 =
    FILTER ( ALL ( 'Scores By Workflow' ), [Year Quarter] = pre_date )
VAR pre_score =
    AVERAGEX ( tmp2, [Workflow Score Avg] )
VAR tmp3 =
    CALCULATETABLE (
        VALUES ( 'Scores By Workflow'[Workflow] ),
        FILTER ( 'Scores By Workflow', [Workflow Score Avg] > pre_score )
    )
VAR ctn =
    COUNTROWS ( tmp3 )
RETURN
    IF ( ISEMPTY ( tmp2 ), BLANK (), ctn )
Count of Improved Workflows by Max Score =
VAR cur_date =
    SELECTEDVALUE ( 'Scores By Workflow'[Year Quarter] )
VAR tmp1 =
    FILTER ( ALL ( 'Scores By Workflow' ), [Year Quarter] < cur_date )
VAR pre_date =
    MAXX ( tmp1, [Year Quarter] )
VAR tmp2 =
    FILTER ( ALL ( 'Scores By Workflow' ), [Year Quarter] = pre_date )
VAR pre_score =
    MAXX ( tmp2, [Workflow Score Avg] )
VAR tmp3 =
    CALCULATETABLE (
        VALUES ( 'Scores By Workflow'[Workflow] ),
        FILTER ( 'Scores By Workflow', [Workflow Score Avg] > pre_score )
    )
VAR ctn =
    COUNTROWS ( tmp3 )
RETURN
    IF ( ISEMPTY ( tmp2 ), BLANK (), ctn )

 

vbinbinyumsft_0-1663816761866.png

Please refer the attached .pbix file.

 

Best regards,
Community Support Team_ Binbin Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Hi @nericksx ,

I'm a little confused about your needs, Could you please explain them further? It would be good to provide a calculate logic.

Thanks for your efforts & time in advance.

 

Best regards,
Community Support Team_ Binbin Yu

Good evening. I've edited my original question to provide more information. Does that help?

Anonymous
Not applicable

Hi @nericksx ,

"the most recent score is higher than the last score" means current Year Quarte Workflow Score Avg is higher than the previous Year Quarte Workflow Score Avg ?  Whether this comparison is the average value of the base and the previous Year Quarte or the maximum value of the previous Year Quarte ?

Thanks for your efforts & time in advance.

 

Best regards,
Community Support Team_ Binbin Yu

 

 

 

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

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.