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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
scotjn1
Helper I
Helper I

cumulative count within DAX table variable

I have been asked for a measure to give the average number of observations of X that have occurred between observations of Y within various samples.  To do this, I'm using a DAX table variable within the measure to structure the data for counting.  I just cannot figure out how to do the "lookback" in order to do a cumulative count.

 

Here is an example of what I have so far within the table variable:

scotjn1_1-1643212576775.png

 

Each 1 in Type1 and Type2 indicates a positive observation.  I need to count the number of Type2 observations between every Type1 observation, inclusive.  I also need to "reset" any counters with changes to the SampleID.  These are the expected results of the cumulative counts:

 

scotjn1_2-1643212677390.png

 

Any guidance on how to do this is appreciated!

4 REPLIES 4
v-henryk-mstf
Community Support
Community Support

Hi @scotjn1 ,

 

Create TEMP as a calculated table and then add this column. But the results still seem to be problematic. Can you further explain the judgment condition between type1 and type2, so that I can do further testing.

vhenrykmstf_0-1643698300408.png


Looking forward to your feedback.


Best Regards,
Henry


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

Hi @v-henryk-mstf ,

This is getting very close.  The count of 17 is correct in your screenshot.  Since I only care about the count where Type1 = 1, the intermediate steps are irrelevant so it's OK that they all show the final count of 17 instead of the increments in my original post.

 

The next step to solve is that the count should "reset" each time Type1 = 1, so in cases where a given SampleID has multiple records where Type1 = 1 the incremental counter would be this:

 

scotjn1_0-1644248430282.png

 

Here is a SampleID where some of the counts would be zero:

 

scotjn1_1-1644249027821.png

 

Thank you for solving the first step!

 

Jacob

amitchandak
Super User
Super User

@scotjn1 , Try a new column as

var _max = minx(filter(table, [sampleID] = earlier([sampleID]) && [Date] >= earlier([Date]) && [Type1] =1), [Date])
return
sumx(filter(table, [sampleID] = earlier([sampleID]) && [Date] <= _max), [Type2])

 

If this does not help
Can you share sample data and sample output in table format? Or a sample pbix after removing sensitive data.

Hello @amitchandak,

Thank you for the reply.  EARLIER doesn't like that since it "refers to an earlier row context which doesn't exist."  Maybe because it's looking at a temp table?

 

Here's the .pbix with data and the DAX table in question.

 

Thank you,

Jacob

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.