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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
Anonymous
Not applicable

Count the consecutive instances when a certain criteria is met using power query editor

I want to include a custom column that counts how many weekendings that products have resulted in poor performance starting with the most recent weekending period: 12.22.2019. If the poor performance didn't occur on W.E. 12.22.2019 then the result would be 0.

 

Below is a small sample of a much larger set of data where the result would be the "Count Poor Performance" column

 

ProductPerformanceWeekendingCount Poor Performance
XYZPoor12.15.20192
XYZPoor12.22.20192
ABCGood12.08.20190
ABCGood12.15.20190
CDFPoor12.22.20191
4 REPLIES 4
Jimmy801
Community Champion
Community Champion

Hello @Anonymous ,

 

If you would like to use Power Query for this, you can use this query.

It does a grouping and filtering on "poor" and using this result to join the initial table. Here the code to check if this could fit

let
	Source = #table
	(
		{"Product","Performance","Weekending"},
		{
			{"XYZ","Poor","43814"},	{"XYZ","Poor","43821"},	{"ABC","Good","43807"},	{"ABC","Good","43814"},	{"CDF","Poor","43821"}
		}
	),
    Date = Table.TransformColumns
	(
		Source,
		{{"Weekending", each Date.From(Number.From(_)), type date}}
	),
    Group = Table.Group
	(
		Date, 
		{"Product", "Performance"}, 
		{{"CountRows", each Table.RowCount(_), type number}}
	),
    FilterForPoor = Table.SelectRows
	(
		Group, 
		each ([Performance] = "Poor")
	),
	Join = Table.NestedJoin
	(
		Date,
		{"Product", "Performance"}, 
		FilterForPoor, 
		{"Product", "Performance"}, 
		"CountPoor"
	),
    Expand = Table.ExpandTableColumn
	(
		Join, 
		"CountPoor", 
		{"CountRows"}, 
		{"CountRows"}
	)
in
	Expand

Copy paste this code to the advanced editor to see how the solution works. You can afterwards apply this steps to your original query by copy pasting a part of it. Otherwise I could create a function out of it and you can implement it with the custom function in a easier way.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

Anonymous
Not applicable

@Jimmy801,

 

Thank you for looking into this so far.

One element I want to make sure is fulfilled is the count function only applies when the current most weekending (max weekending) is poor performance.

 

Consider that a trigger to start counting the consecutive instances of previous weeks.

 

See the clarified dataset below with a more clear result in mind. The most recent weekending being: 12/29/2019

 

WeekendingsProductPerformanceResult (Consecutive WEs)
12/15/2019ABCPoor3
12/22/2019ABCPoor3
12/29/2019ABCPoor3
12/15/2019XYZPoor1
12/29/2019XYZPoor1 because not consecutive (12/22/2019) wasn't active
12/15/2019FGHPoor0
12/22/2019FGHPoor0 because 12/29/2019 wasn't active
12/22/2019BNMPoor 
12/29/2019BNMGood0 because 12/29/2019 is Good performance

 

Hello @Anonymous 

 

this deviates a lot from what you have asked in the first post. Remember to post such important things always in the beginning.

 

Jimmy

edhans
Super User
Super User

The following measure will do this I believe. I'd have to tinker with it a bit to get a calculated column to work, but if the goal is to report a table/visual with the info you want, a measure is a better bet.

 

Count of Poor Performance = 
VAR LatestWeekEnding = MAX('Product Performance'[Week Ending])
VAR PoorPerformancecount =
        CALCULATE(
            COUNTA('Product Performance'[Product]),
            FILTER(
                ALLEXCEPT('Product Performance','Product Performance'[Product]),
                MAX('Product Performance'[Performance]) = "Poor"
            )
        )
VAR PoorPerformanceList = 
    CALCULATETABLE(
        DISTINCT('Product Performance'[Product]),
        'Product Performance'[Performance] = "Poor",
        'Product Performance'[Week Ending] = LatestWeekEnding
    )
RETURN
IF(MAX('Product Performance'[Product]) in PoorPerformanceList,PoorPerformancecount,BLANK())

 

I get a table that looks like this:

20191231 09_57_50-Untitled - Power BI Desktop.png



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors